core-legacy/main.cc

75 lines
1.6 KiB
C++
Raw Normal View History

2019-07-12 19:05:56 +00:00
#include <drogon/drogon.h>
#include <sqlite3.h>
2019-07-22 20:06:13 +00:00
#include <models/controller_dbo.h>
2019-07-19 09:41:39 +00:00
#include <csignal>
2019-07-12 19:05:56 +00:00
#include "globals.h"
2019-08-22 19:38:23 +00:00
#include "config.h"
2019-07-12 19:05:56 +00:00
static void
add_cors_headers(const drogon::HttpRequestPtr &requestPtr, const drogon::HttpResponsePtr &responsePtr)
{
responsePtr->addHeader("Access-Control-Allow-Origin", "*");
}
2019-07-19 12:42:36 +00:00
static void
terminate(int signum)
{
LOG_INFO << "Terminating Server (" << signum << ")";
sqlite3_close(globals::db);
quick_exit(signum);
}
2019-07-19 09:41:39 +00:00
/*static void test()
{
LOG_DEBUG << "LOOP";
}*/
2019-07-12 19:05:56 +00:00
2019-07-19 12:42:36 +00:00
int
2019-10-27 00:07:01 +00:00
main(int argc, char** argv)
2019-07-19 09:41:39 +00:00
{
2019-07-19 12:42:36 +00:00
signal(SIGINT, terminate);
signal(SIGABRT, terminate);
signal(SIGTERM, terminate);
2019-10-27 00:07:01 +00:00
const char* config_file_name = "config.json";
if(argc == 2)
{
config_file_name = argv[1];
}
//Load config file
drogon::app().loadConfigFile(config_file_name);
const char* db_name = drogon::app().instance().getCustomConfig()["db_name"].asCString();
2019-07-12 19:05:56 +00:00
/* Open database */
2019-10-27 00:07:01 +00:00
int rc = sqlite3_open(db_name, &globals::db);
2019-07-12 19:05:56 +00:00
2019-10-27 00:07:01 +00:00
if(rc) {
2019-08-06 22:11:48 +00:00
LOG_FATAL << "Can't open database: " << sqlite3_errmsg(globals::db);
2019-07-12 19:05:56 +00:00
return 1;
}
2019-10-27 00:07:01 +00:00
if(helpers::migrate_sql())
{
terminate(1);
}
drogon::app().registerPostHandlingAdvice(add_cors_headers);
2019-09-17 11:26:51 +00:00
drogon::app().instance().setCustom404Page(drogon::HttpResponse::newFileResponse("./static/index.html", "", drogon::CT_TEXT_HTML));
2019-07-19 09:41:39 +00:00
//drogon::app().getLoop()->runEvery(1, &test);
2019-07-12 19:05:56 +00:00
//Run HTTP framework,the method will block in the internal event loop
2019-08-22 19:38:23 +00:00
LOG_INFO << "Starting Emgauwa Core (" << config::version << ")";
2019-07-12 19:05:56 +00:00
drogon::app().run();
return 0;
}