2023-04-03 08:57:51 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <httplib/httplib.h>
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "routes/home.h"
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
if (argc != 2) {
|
|
|
|
fprintf(stderr, "Usage: %s <path/to/config/file.json>\n", argc > 0 ? argv[0] : "pixwhile");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Config config;
|
|
|
|
try {
|
|
|
|
config = load_config(argv[1]);
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
fprintf(stderr, "Failed to load config: %s\n", e.what());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
httplib::Server server;
|
2023-04-03 09:32:26 +00:00
|
|
|
server.Get("/", [&](const httplib::Request& req, httplib::Response& res) {
|
|
|
|
home_route(req, res, config);
|
|
|
|
});
|
2023-04-03 08:57:51 +00:00
|
|
|
|
|
|
|
if (config.bind_port != 0) {
|
|
|
|
if (!server.bind_to_port(config.bind_host, config.bind_port)) {
|
|
|
|
fprintf(stderr, "Failed to bind to %s:%d\n", config.bind_host.c_str(), config.bind_port);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int port = server.bind_to_any_port(config.bind_host);
|
|
|
|
if (port == -1) {
|
|
|
|
fprintf(stderr, "Failed to bind to %s:<any>\n", config.bind_host.c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
config.bind_port = port;
|
|
|
|
}
|
|
|
|
printf("Listening on %s:%d...\n", config.bind_host.c_str(), config.bind_port);
|
|
|
|
|
|
|
|
if (!server.listen_after_bind()) {
|
|
|
|
fprintf(stderr, "Failed to listen on %s:%d\n", config.bind_host.c_str(), config.bind_port);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|