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.
111 lines
2.1 KiB
C
111 lines
2.1 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 "app_args.h"
|
|
#include "http_server/http_server.h"
|
|
#include "http_server/http_request.h"
|
|
|
|
#include "controller/main.h"
|
|
#include "controller/inbox.h"
|
|
#include "controller/outbox.h"
|
|
#include "controller/test.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 );
|
|
#define RENDER
|
|
#include "view/404.html.inc"
|
|
#undef RENDER
|
|
}
|
|
}
|
|
|
|
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, true );
|
|
|
|
while(!terminate) {
|
|
http_server_process( srv );
|
|
usleep(1);
|
|
}
|
|
|
|
http_server_release( srv );
|
|
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);
|
|
|
|
struct app_args* args = app_args_new( argc, argv );
|
|
if( !args ) {
|
|
printf( "Error processing argument\n" );
|
|
return 1;
|
|
}
|
|
|
|
if( argc > 1 ) {
|
|
int section = 0;
|
|
sscanf(argv[1],"--section=%d",§ion);
|
|
switch( section ) {
|
|
case 1: process_inbox(); goto exit;
|
|
case 2: process_outbox(); goto exit;
|
|
case 3: built_in_test(); goto exit;
|
|
|
|
case 100: develop(); return 0;
|
|
}
|
|
}
|
|
|
|
/*
|
|
int inbox_handler_pid = -1;
|
|
|
|
// Process inbox
|
|
if( !( inbox_handler_pid = fork() ) ) {
|
|
prctl(PR_SET_PDEATHSIG, SIGHUP);
|
|
process_inbox();
|
|
}
|
|
|
|
// Process outbox
|
|
if( !( inbox_handler_pid = fork() ) ) {
|
|
prctl(PR_SET_PDEATHSIG, SIGHUP);
|
|
process_outbox();
|
|
}*/
|
|
|
|
int code = 0;
|
|
if( !run_webserver(args) ) { code = 1; }
|
|
|
|
exit:
|
|
app_args_release(args);
|
|
|
|
return code;
|
|
}
|
|
|