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