annotate server/main.cpp @ 0:a4671277546c tip

created the repository for the thymian project
author ferencd
date Tue, 17 Aug 2021 11:19:54 +0200
parents
children
rev   line source
ferencd@0 1 #include <tnt/tntnet.h>
ferencd@0 2 #include <tntdb.h>
ferencd@0 3
ferencd@0 4 #include "web_logmachine.h"
ferencd@0 5
ferencd@0 6 #include <logger.h>
ferencd@0 7 #include <config.h>
ferencd@0 8
ferencd@0 9 #include <templater.h>
ferencd@0 10 #include <csignal>
ferencd@0 11
ferencd@0 12 void exit_work(const char* source)
ferencd@0 13 {
ferencd@0 14 log_critical() << "WORK SOURCE:" << source;
ferencd@0 15 }
ferencd@0 16
ferencd@0 17 void exit_on_error()
ferencd@0 18 {
ferencd@0 19 exit_work("exit_on_error");
ferencd@0 20 }
ferencd@0 21
ferencd@0 22 void signal_handler(int signum)
ferencd@0 23 {
ferencd@0 24 log_warning() << "Signal caught " << signum;
ferencd@0 25 exit_work("signal_handler");
ferencd@0 26 _Exit(1);
ferencd@0 27 }
ferencd@0 28
ferencd@0 29 void atexit_handler()
ferencd@0 30 {
ferencd@0 31 std::cerr << "Atexit handler" << std::endl;
ferencd@0 32 exit_work("atexit_handler");
ferencd@0 33 }
ferencd@0 34
ferencd@0 35 int main(int argc, char* argv[])
ferencd@0 36 {
ferencd@0 37 std::cout << "thymian 0.1 starting" << std::endl;
ferencd@0 38 (void)argc;(void)argv;
ferencd@0 39
ferencd@0 40 unafrog::logger::instance().add_log_machine(new web_logmachine);
ferencd@0 41
ferencd@0 42 info() << "--------------------- App starting [PID:" << getpid() << "] ---------------------";
ferencd@0 43
ferencd@0 44 std::set_unexpected (exit_on_error);
ferencd@0 45 std::set_terminate(exit_on_error);
ferencd@0 46 atexit(atexit_handler);
ferencd@0 47
ferencd@0 48 struct sigaction new_action, old_action;
ferencd@0 49
ferencd@0 50 /* Set up the structure to specify the new action. */
ferencd@0 51 new_action.sa_handler = signal_handler;
ferencd@0 52 sigemptyset (&new_action.sa_mask);
ferencd@0 53 new_action.sa_flags = 0;
ferencd@0 54
ferencd@0 55 sigaction (SIGINT, nullptr, &old_action);
ferencd@0 56 if (old_action.sa_handler != SIG_IGN)
ferencd@0 57 {
ferencd@0 58 sigaction (SIGINT, &new_action, nullptr);
ferencd@0 59 }
ferencd@0 60
ferencd@0 61 sigaction (SIGHUP, nullptr, &old_action);
ferencd@0 62 if (old_action.sa_handler != SIG_IGN)
ferencd@0 63 {
ferencd@0 64 sigaction (SIGHUP, &new_action, nullptr);
ferencd@0 65 }
ferencd@0 66
ferencd@0 67 sigaction (SIGTERM, nullptr, &old_action);
ferencd@0 68 if (old_action.sa_handler != SIG_IGN)
ferencd@0 69 {
ferencd@0 70 sigaction (SIGTERM, &new_action, nullptr);
ferencd@0 71 }
ferencd@0 72
ferencd@0 73 sigaction (SIGSEGV, nullptr, &old_action);
ferencd@0 74 if (old_action.sa_handler != SIG_IGN)
ferencd@0 75 {
ferencd@0 76 sigaction (SIGSEGV, &new_action, nullptr);
ferencd@0 77 }
ferencd@0 78
ferencd@0 79 sigaction (SIGKILL, nullptr, &old_action);
ferencd@0 80 if (old_action.sa_handler != SIG_IGN)
ferencd@0 81 {
ferencd@0 82 sigaction (SIGKILL, &new_action, nullptr);
ferencd@0 83 }
ferencd@0 84
ferencd@0 85 srand(static_cast<unsigned int>(::time(nullptr)));
ferencd@0 86
ferencd@0 87 try
ferencd@0 88 {
ferencd@0 89
ferencd@0 90 if(!config::instance().valid())
ferencd@0 91 {
ferencd@0 92 log_critical() << "configuration failure, exiting";
ferencd@0 93 return 1;
ferencd@0 94 }
ferencd@0 95
ferencd@0 96 // check that all the templates are where they are supposed to be
ferencd@0 97 if(!template_warehouse::instance().checkTemplates())
ferencd@0 98 {
ferencd@0 99 log_critical() << "Cannot find some templates. Giving up";
ferencd@0 100 return 1;
ferencd@0 101 }
ferencd@0 102
ferencd@0 103 // start the web server
ferencd@0 104 tnt::Tntnet app;
ferencd@0 105 app.listen("*", 8999);
ferencd@0 106
ferencd@0 107 auto pngMap = app.mapUrl("^/img/(.*).png$", "filer"); // the supported images
ferencd@0 108 auto filer1 = app.mapUrl("^/img/(.*).ico$", "filer");
ferencd@0 109 auto filer2 = app.mapUrl("^/img/(.*).gif$", "filer");
ferencd@0 110 auto filer3 = app.mapUrl("^/img/(.*).jpg$", "filer");
ferencd@0 111 auto filer10Global = app.mapUrl("/(.*).js$", "filer"); // javascript handler
ferencd@0 112 auto filerPdf = app.mapUrl("^/pdfs/(.*).pdf$", "filer"); // the PDF downloader
ferencd@0 113
ferencd@0 114 auto pngMapCurrentTheme = app.mapUrl("^/theme/current/img/(.*).png$", "filer"); // the supported images from the current theme
ferencd@0 115 auto filer1CurrentTheme = app.mapUrl("^/theme/current/img/(.*).ico$", "filer");
ferencd@0 116 auto filer2CurrentTheme = app.mapUrl("^/theme/current/img/(.*).gif$", "filer");
ferencd@0 117 auto filer3CurrentTheme = app.mapUrl("^/theme/current/img/(.*).jpg$", "filer");
ferencd@0 118 auto filer10 = app.mapUrl("^/theme/current/(.*).js$", "filer"); // javascript handler for current theme
ferencd@0 119 auto filer10scriptDirTheme = app.mapUrl("^/theme/current/script/(.*).js$", "filer"); // javascript handler for current theme
ferencd@0 120 auto filer12CurrentTheme = app.mapUrl("/theme/current/(.*).css$", "filer"); // the css files regardless ofwhere they are
ferencd@0 121 auto filer12DirTheme = app.mapUrl("/theme/current/css/(.*).css$", "filer"); // the css files regardless ofwhere they are
ferencd@0 122
ferencd@0 123 auto filer4 = app.mapUrl("/(.*).png$", "filer");
ferencd@0 124 auto filer5 = app.mapUrl("/(.*).ico$", "filer");
ferencd@0 125 auto filer6 = app.mapUrl("/(.*).gif$", "filer");
ferencd@0 126 auto filer7 = app.mapUrl("/(.*).jpg$", "filer");
ferencd@0 127 auto filer8 = app.mapUrl("/(.*).html$", "filer");
ferencd@0 128 auto filer9 = app.mapUrl("/(.*).htm$", "filer");
ferencd@0 129 auto filer10ScriptDirGlobal = app.mapUrl("/js/(.*).js$", "filer"); // javascript handler
ferencd@0 130 auto filer12 = app.mapUrl("/(.*).css$", "filer"); // the css files regardless ofwhere they are
ferencd@0 131 auto filer12StyleDirGlobal = app.mapUrl("/css/(.*).css$", "filer"); // the css files in a global directory
ferencd@0 132 auto filer13 = app.mapUrl("/robots.txt$", "filer");
ferencd@0 133
ferencd@0 134 auto homepage = app.mapUrl("^/$", "root"); // / (ie: homepage)
ferencd@0 135
ferencd@0 136 auto langNo = app.mapUrl("^/no$", "main_root_sender"); // / (ie: homepage in norwegian)
ferencd@0 137 auto langGb = app.mapUrl("^/gb$", "main_root_sender"); // / (ie: homepage in english)
ferencd@0 138 auto langRo = app.mapUrl("^/ro$", "main_root_sender"); // / (ie: homepage in romanian)
ferencd@0 139 auto langHu = app.mapUrl("^/hu$", "main_root_sender"); // / (ie: homepage in hungarian)
ferencd@0 140
ferencd@0 141 auto categoryLister = app.mapUrl("^/l/(.*)", "category_root_sender"); // / (ie: lists of various food categories)
ferencd@0 142
ferencd@0 143 auto recipeSender = app.mapUrl("^/r/(.*)", "r"); // / (ie: the recipes themselves)
ferencd@0 144
ferencd@0 145
ferencd@0 146 app.run();
ferencd@0 147 }
ferencd@0 148 catch (const std::exception& e)
ferencd@0 149 {
ferencd@0 150 emergency() << e.what();
ferencd@0 151 exit(1);
ferencd@0 152 }
ferencd@0 153 log_critical() << "bye bye cruel world";
ferencd@0 154 exit(0);
ferencd@0 155 }