#include #include #include #include #include #include #include #include #include #include "headers/authorization.hpp" #include "headers/xforwardfor.hpp" #include "headers/cfHeader.hpp" #include "headers/gitProtocolHeader.hpp" #include "headers/acceptLanguageHeader.hpp" #include "headers/setCookieHeader.hpp" #include "debug/log.hpp" #include "core/Handler.hpp" #include "core/Crypto.hpp" #include "config/Config.hpp" #include "GlobalState.hpp" #include int main(int argc, char** argv, char** envp) { if (argc < 2) { Debug::log(CRIT, "Missing param for websites storage"); return 1; } std::vector ARGS{}; ARGS.resize(argc); for (int i = 0; i < argc; ++i) { ARGS[i] = std::string{argv[i]}; } std::vector command; g_pGlobalState->cwd = std::filesystem::current_path(); for (int i = 1; i < argc; ++i) { if (ARGS[i].starts_with("-")) { if (ARGS[i] == "--help" || ARGS[i] == "-h") { std::cout << "-h [html_root] -p [port]\n"; return 0; } else if ((ARGS[i] == "--config" || ARGS[i] == "-c") && i + 1 < argc) { g_pGlobalState->configPath = ARGS[i + 1]; i++; } else { std::cerr << "Unrecognized / invalid use of option " << ARGS[i] << "\nContinuing...\n"; continue; } } else command.push_back(ARGS[i]); } g_pConfig = std::make_unique(); if (g_pConfig->m_config.html_dir.empty() || g_pConfig->m_config.data_dir.empty()) return 1; sigset_t signals; if (sigemptyset(&signals) != 0 || sigaddset(&signals, SIGTERM) != 0 || sigaddset(&signals, SIGINT) != 0 || sigaddset(&signals, SIGQUIT) != 0 || sigaddset(&signals, SIGPIPE) != 0 || sigaddset(&signals, SIGALRM) != 0 || sigprocmask(SIG_BLOCK, &signals, nullptr) != 0) return 1; int threads = 1; Pistache::Address address = {Pistache::Ipv4::any(), (uint16_t)g_pConfig->m_config.port}; Debug::log(LOG, "Starting the server on {}:{}\n", address.host(), address.port().toString()); Pistache::Http::Header::Registry::instance().registerHeader(); Pistache::Http::Header::Registry::instance().registerHeader(); Pistache::Http::Header::Registry::instance().registerHeader(); Pistache::Http::Header::Registry::instance().registerHeader(); Pistache::Http::Header::Registry::instance().registerHeader(); g_pCrypto = std::make_unique(); auto endpoint = std::make_unique(address); auto opts = Pistache::Http::Endpoint::options().threads(threads).flags(Pistache::Tcp::Options::ReuseAddr | Pistache::Tcp::Options::ReusePort); opts.maxRequestSize(g_pConfig->m_config.max_request_size); endpoint->init(opts); auto handler = Pistache::Http::make_handler(); endpoint->setHandler(handler); endpoint->serveThreaded(); bool terminate = false; while (!terminate) { int number = 0; int status = sigwait(&signals, &number); if (status != 0) { Debug::log(CRIT, "sigwait threw {} :(", status); break; } Debug::log(LOG, "Caught signal {}", number); switch (number) { case SIGINT: terminate = true; break; case SIGTERM: terminate = true; break; case SIGQUIT: terminate = true; break; case SIGPIPE: break; case SIGALRM: break; } } sigprocmask(SIG_UNBLOCK, &signals, nullptr); Debug::log(LOG, "Shutting down, bye!"); endpoint->shutdown(); endpoint = nullptr; return 0; }