2024-07-27 11:27:52 +00:00
|
|
|
#include <engine_engine_number_9/baza_throw.h>
|
|
|
|
#include <engine_engine_number_9/running_mainloop.h>
|
|
|
|
#include <engine_engine_number_9/http_structures/response_gen.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <engine_engine_number_9/connecting_assets/static_asset_manager.h>
|
|
|
|
#include <assert.h>
|
2024-08-05 00:37:56 +00:00
|
|
|
#include <sqlite3.h>
|
2024-08-11 21:12:30 +00:00
|
|
|
#include <jsonincpp/string_representation.h>
|
2024-08-05 00:37:56 +00:00
|
|
|
#include <libregexis024vm/vm_opcodes.h>
|
2024-08-06 08:53:33 +00:00
|
|
|
#include <engine_engine_number_9/form_data_structure/urlencoded_query.h>
|
2024-08-12 16:43:35 +00:00
|
|
|
#include <new_york_transit_line/templater.h>
|
2024-07-27 11:27:52 +00:00
|
|
|
|
|
|
|
bool termination = false;
|
|
|
|
|
2024-08-05 00:37:56 +00:00
|
|
|
void sigterm_action(int) {
|
2024-07-27 11:27:52 +00:00
|
|
|
termination = true;
|
|
|
|
}
|
|
|
|
|
2024-08-05 00:37:56 +00:00
|
|
|
void usage(char** argv) {
|
2024-08-14 18:16:56 +00:00
|
|
|
printf("Usage: %s <file with settings>\n", argv[0]);
|
2024-08-05 00:37:56 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string unsafe_client_request_stringification(const een9::ClientRequest& req) {
|
|
|
|
std::string text = "\n\nGot some cool stuff\n";
|
|
|
|
text += (req.method + " " + req.uri_path + " " + req.http_version + "\n");
|
|
|
|
for (auto& p: req.headers) {
|
|
|
|
text += p.first; text += ": "; text += p.second; text += "\n";
|
2024-07-27 11:27:52 +00:00
|
|
|
}
|
2024-08-05 00:37:56 +00:00
|
|
|
text += "Body\n"; text += req.body; text += "\n";
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv){
|
|
|
|
try {
|
|
|
|
een9_ASSERT_pl(argc > 0);
|
2024-08-14 18:16:56 +00:00
|
|
|
if (argc < 1 + 1)
|
2024-08-05 00:37:56 +00:00
|
|
|
usage(argv);
|
|
|
|
if (!een9::isRegularFile(argv[1]) || !een9::endsIn(argv[1], ".json")) {
|
|
|
|
printf("\"%s\" is not a json file\n", argv[1]);
|
|
|
|
usage(argv);
|
2024-07-27 11:27:52 +00:00
|
|
|
}
|
2024-08-05 00:37:56 +00:00
|
|
|
std::string config_file = argv[1];
|
|
|
|
|
|
|
|
std::string config_text;
|
|
|
|
een9::readFile(config_file, config_text);
|
|
|
|
json::JSON config = json::parse_str_flawless(config_text);
|
|
|
|
een9_ASSERT(config.isDictionary(), "config root is not dictionary");
|
2024-08-14 18:16:56 +00:00
|
|
|
een9_ASSERT(config["assets"].g().isString(), "config[\"\assets\"] is not string");
|
|
|
|
std::string assets_dir = config["assets"].g().asString();
|
|
|
|
een9_ASSERT(een9::isDirectory(assets_dir), "\"" + assets_dir + "\" is not a directory");
|
2024-08-05 00:37:56 +00:00
|
|
|
|
|
|
|
een9::StaticAssetManagerSlaveModule samI;
|
|
|
|
samI.update({
|
|
|
|
een9::StaticAssetManagerRule{assets_dir + "/css", "/assets/css", {{".css", "text/css"}} },
|
|
|
|
een9::StaticAssetManagerRule{assets_dir + "/js", "/assets/js", {{".js", "text/js"}} },
|
2024-08-06 09:33:20 +00:00
|
|
|
een9::StaticAssetManagerRule{assets_dir + "/img", "/assets/img", {
|
|
|
|
{".jpg", "image/jpg"}, {".png", "image/png"}, {".svg", "image/svg+xml"}
|
|
|
|
} },
|
2024-08-05 00:37:56 +00:00
|
|
|
});
|
2024-07-27 11:27:52 +00:00
|
|
|
|
2024-08-14 18:16:56 +00:00
|
|
|
json::JSON& config_presentation = config["presentation"].g();
|
|
|
|
|
|
|
|
/* Because templaters use libjsonincpp, they can't be READ by two thread simultaneously */
|
|
|
|
std::vector<std::unique_ptr<nytl::Templater>> templaters_copies(8);
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
templaters_copies[i] = std::make_unique<nytl::Templater>(
|
|
|
|
nytl::TemplaterSettings{nytl::TemplaterDetourRules{assets_dir + "/HypertextPages"}});
|
|
|
|
templaters_copies[i]->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
// printf("%s\n", templaters_copies[0]->render("list-rooms", {&config_presentation}).c_str());
|
|
|
|
// return 0;
|
|
|
|
|
2024-08-05 00:37:56 +00:00
|
|
|
een9::MainloopParameters params;
|
2024-08-14 18:16:56 +00:00
|
|
|
params.guest_core = [&samI, &templaters_copies, config_presentation]
|
|
|
|
(const een9::SlaveTask& task, const een9::ClientRequest& req, een9::worker_id_t worker_id) -> std::string {
|
|
|
|
een9_ASSERT_pl(0 <= worker_id && worker_id < templaters_copies.size());
|
|
|
|
nytl::Templater& templater = *templaters_copies[worker_id];
|
2024-08-05 00:37:56 +00:00
|
|
|
een9::StaticAsset sa;
|
|
|
|
int ret;
|
2024-08-06 08:53:33 +00:00
|
|
|
// printf("%s", unsafe_client_request_stringification(req).c_str());
|
2024-08-14 18:16:56 +00:00
|
|
|
// if (req.uri_path == "/output") {
|
|
|
|
// std::string text = unsafe_client_request_stringification(req);
|
|
|
|
// return een9::form_http_server_response_200("text/plain", text);
|
|
|
|
// }
|
|
|
|
auto rteee = [&](const std::string& el_name, bool pass_phr) -> std::string {
|
|
|
|
std::string page = templater.render(el_name,
|
|
|
|
pass_phr ? std::vector<const json::JSON*>{&config_presentation} : std::vector<const json::JSON*>{});
|
|
|
|
return een9::form_http_server_response_200("text/html", page);
|
2024-08-06 09:33:20 +00:00
|
|
|
};
|
|
|
|
if (req.uri_path == "/" || req.uri_path == "/list-rooms") {
|
2024-08-14 18:16:56 +00:00
|
|
|
return rteee("list-rooms", true);
|
2024-08-05 00:37:56 +00:00
|
|
|
}
|
2024-08-06 09:33:20 +00:00
|
|
|
if (req.uri_path == "/chat") {
|
2024-08-14 18:16:56 +00:00
|
|
|
return rteee("chat", false);
|
2024-08-06 09:33:20 +00:00
|
|
|
}
|
|
|
|
if (req.uri_path == "/profile") {
|
2024-08-14 18:16:56 +00:00
|
|
|
return rteee("profile", false);
|
2024-08-06 09:33:20 +00:00
|
|
|
}
|
|
|
|
if (req.uri_path == "/registration") {
|
2024-08-14 18:16:56 +00:00
|
|
|
return rteee("registration", false);
|
2024-08-06 09:33:20 +00:00
|
|
|
}
|
|
|
|
/* Trying to interpret request as asset lookup */
|
2024-08-05 00:37:56 +00:00
|
|
|
ret = samI.get_asset(req.uri_path, sa);
|
|
|
|
if (ret >= 0) {
|
|
|
|
return een9::form_http_server_response_200(sa.type, sa.content);
|
|
|
|
}
|
|
|
|
return een9::form_http_server_response_404("text/html", "<h1> Not found! </h1>");
|
|
|
|
};
|
2024-08-14 18:16:56 +00:00
|
|
|
|
2024-08-05 00:37:56 +00:00
|
|
|
params.ports_to_listen = {1025};
|
|
|
|
params.slave_number = 8;
|
|
|
|
params.open_admin_listener = false;
|
2024-07-27 11:27:52 +00:00
|
|
|
|
2024-08-05 00:37:56 +00:00
|
|
|
signal(SIGINT, sigterm_action);
|
|
|
|
signal(SIGTERM, sigterm_action);
|
|
|
|
|
|
|
|
een9::electric_boogaloo(params, termination);
|
|
|
|
} catch (std::exception& e) {
|
|
|
|
printf("System failure\n%s\n", e.what());
|
|
|
|
}
|
2024-07-27 11:27:52 +00:00
|
|
|
return 0;
|
2024-08-05 00:37:56 +00:00
|
|
|
}
|