iu9-ca-web-chat/src/web_chat/main.cpp

94 lines
3.5 KiB
C++
Raw Normal View History

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>
#include <sqlite3.h>
#include <libjsonincpp/string_representation.h>
#include <libregexis024vm/vm_opcodes.h>
2024-07-27 11:27:52 +00:00
bool termination = false;
void sigterm_action(int) {
2024-07-27 11:27:52 +00:00
termination = true;
}
void usage(char** argv) {
printf("Usage: %s <file with settings> <assets folder>\n", argv[0]);
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
}
text += "Body\n"; text += req.body; text += "\n";
return text;
}
int main(int argc, char** argv){
printf("%s\n", regexis024::opcode_to_str(regexis024::opcode_t::DIE));
try {
een9_ASSERT_pl(argc > 0);
if (argc < 1 + 2)
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
}
std::string config_file = argv[1];
if (!een9::isDirectory(argv[2])) {
printf("\"%s\" is not a directory\n", argv[2]);
usage(argv);
2024-07-27 11:27:52 +00:00
}
std::string assets_dir = argv[2];
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");
een9::StaticAssetManagerSlaveModule samI;
samI.update({
een9::StaticAssetManagerRule{assets_dir + "/html", "/assets/html", {{".html", "text/html"}} },
een9::StaticAssetManagerRule{assets_dir + "/css", "/assets/css", {{".css", "text/css"}} },
een9::StaticAssetManagerRule{assets_dir + "/js", "/assets/js", {{".js", "text/js"}} },
});
2024-07-27 11:27:52 +00:00
een9::MainloopParameters params;
params.guest_core = [&samI](const een9::SlaveTask& task, const een9::ClientRequest& req) -> std::string {
een9::StaticAsset sa;
int ret;
printf("%s", unsafe_client_request_stringification(req).c_str());
if (req.uri_path == "/output") {
std::string text = unsafe_client_request_stringification(req);
return een9::form_http_server_response_200("text/plain", text);
}
if (req.uri_path == "/" || req.uri_path == "/index.html") {
ret = samI.get_asset("/assets/html/test.html", sa);
een9_ASSERT_pl(ret == 0);
return een9::form_http_server_response_200(sa.type, sa.content);
}
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>");
};
params.ports_to_listen = {1025};
params.slave_number = 8;
params.open_admin_listener = false;
2024-07-27 11:27:52 +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;
}