You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
2.2 KiB
C

#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <stdbool.h>
#include <unistd.h>
#include <signal.h>
#include <sys/prctl.h>
#include "http/server/server.h"
#include "http/server/request.h"
#include "model/server.h"
#include "controller/main.h"
#include "controller/inbox.h"
#include "controller/outbox.h"
#include "controller/test.h"
#include "controller/indexer.h"
#include <curl/curl.h>
bool terminate = false;
void handle_ctrl_c(int)
{
terminate = true;
}
void handle_request( struct http_request* req, void* )
{
printf( "Handling request from %s\n", http_request_get_remote_host_address( req ) );
if( !route_request( req ) ) {
FILE* f = http_request_get_response_body( req );
http_request_send_headers( req, 404, "text/html", true );
#include "view/404.html.inc"
}
fflush(stdout);
}
bool run_webserver( struct app_args* args )
{
struct http_server* srv = http_server_new( args, handle_request, NULL );
if( !srv ) {
printf( "Error setting up server\n" );
app_args_release(args);
return false;
}
http_server_set_debug( srv, args->debug );
while(!terminate) {
http_server_process( srv );
usleep(25000);
}
http_server_release( srv );
return true;
}
bool run_everything( struct app_args* args )
{
// Process inbox
//*
int inbox_handler_pid = -1;
if( !( inbox_handler_pid = fork() ) ) {
prctl(PR_SET_PDEATHSIG, SIGHUP);
process_inbox();
}
//*/
//*
// Process outbox
int outbox_handler_pid = -1;
if( !( outbox_handler_pid = fork() ) ) {
prctl(PR_SET_PDEATHSIG, SIGHUP);
process_outbox();
}
//*/
if( !run_webserver(args) ) { return false; }
return true;
}
void develop();
int main( int argc, char* argv[] )
{
curl_global_init(CURL_GLOBAL_DEFAULT);
signal(SIGINT, handle_ctrl_c);
signal(SIGPIPE, SIG_IGN);
if( !app_args_new( argc, argv ) ) {
return 1;
}
int code = 0;
printf( "section = %d\n", g_server->section );
fflush(stdout);
switch(g_server->section) {
case 0: code = !run_webserver(g_server); break;
case 1: process_inbox(); break;
case 2: process_outbox(); break;
case 3: built_in_test(); break;
case 4: reindex(); break;
case 100: develop(); break;
default: code = !run_everything(g_server); break;
}
curl_global_cleanup();
app_args_release(g_server);
return code;
}