150 lines
5.9 KiB
C++
Raw Normal View History

2022-08-28 17:37:09 +02:00
#include <filesystem>
2022-08-31 14:28:19 +02:00
#include <fstream>
2022-08-28 17:37:09 +02:00
#include <drogon/drogon.h>
#include "dto/dto.h"
2022-08-31 14:28:19 +02:00
bool dev_mode = false;
2022-08-28 17:37:09 +02:00
void cleanup() {
std::cout << "Stopping..." << std::endl;
drogon::app().quit();
std::cout << "Cleanup up uploads...";
std::filesystem::remove_all("uploads");
std::cout << " [Done]" << std::endl;
2022-09-03 23:32:20 +02:00
std::cout << "Removing temp folder..." << std::flush;
std::filesystem::remove_all("temp");
std::cout << " [Done]" << std::endl;
2022-08-28 17:37:09 +02:00
std::cout << "Goodbye!" << std::endl;
}
2022-08-31 14:28:19 +02:00
std::string get_index_content() {
std::ifstream file("./static/index.html");
return {std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()};
}
void default_handler(const drogon::HttpRequestPtr& req, std::function<void(const drogon::HttpResponsePtr&)>&& cbk) {
static std::string index_html = get_index_content();
if (req->path().starts_with("/api")) {
std::cout << "Unknown api request: " << req->getMethodString() << " " << req->path() << std::endl;
cbk(drogon::HttpResponse::newNotFoundResponse());
} else {
if (dev_mode) cbk(drogon::HttpResponse::newFileResponse("./static/index.html"));
else cbk(drogon::HttpResponse::newFileResponse((unsigned char*)index_html.data(), index_html.size()));
}
}
int main(int argc, char* argv[]) {
std::vector<std::string> args(argv+1, argv+argc);
if (std::find(args.begin(), args.end(), "--dev") != args.end()) dev_mode = true;
if (dev_mode) std::cout << "Starting in development mode" << std::endl;
2022-08-28 17:37:09 +02:00
std::cout << "Setting up..." << std::endl;
if (!std::filesystem::exists("files")) {
std::cout << "Creating files..." << std::flush;
std::filesystem::create_directory("files");
std::cout << " [Done]" << std::endl;
}
if (!std::filesystem::exists("logs")) {
std::cout << "Creating logs..." << std::flush;
std::filesystem::create_directory("logs");
std::cout << " [Done]" << std::endl;
}
2022-09-03 23:32:20 +02:00
if (std::filesystem::exists("temp")) {
std::cout << "Removing existing temp folder..." << std::flush;
std::filesystem::remove_all("temp");
std::cout << " [Done]" << std::endl;
}
std::cout << "Creating temp folder..." << std::flush;
std::filesystem::create_directory("temp");
std::cout << " [Done]" << std::endl;
2022-08-28 17:37:09 +02:00
auto* loop = drogon::app().getLoop();
loop->queueInLoop([]{
std::cout << "Starting..." << std::endl;
std::cout << "Creating db tables..." << std::flush;
auto db = drogon::app().getDbClient();
db->execSqlSync("CREATE TABLE IF NOT EXISTS 'tokens' (\n"
" 'id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n"
" 'owner_id' INTEGER NOT NULL,\n"
" 'exp' INTEGER NOT NULL\n"
")");
db->execSqlSync("CREATE TABLE IF NOT EXISTS 'user' (\n"
" 'id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n"
" 'gitlab' INTEGER NOT NULL,\n"
" 'name' TEXT NOT NULL,\n"
" 'password' TEXT NOT NULL,\n"
" 'role' INTEGER NOT NULL,\n"
" 'root_id' INTEGER NOT NULL,\n"
" 'tfa_type' INTEGER NOT NULL,\n"
" 'tfa_secret' BLOB,\n"
" 'gitlab_at' TEXT,\n"
" 'gitlab_rt' TEXT\n"
")");
db->execSqlSync("CREATE TABLE IF NOT EXISTS 'inode' (\n"
" 'id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n"
" 'is_file' INTEGER NOT NULL,\n"
" 'name' TEXT,\n"
" 'parent_id' INTEGER,\n"
" 'owner_id' INTEGER NOT NULL,\n"
2022-09-03 23:32:20 +02:00
" 'size' INTEGER,\n"
" 'has_preview' INTEGER NOT NULL\n"
2022-08-28 17:37:09 +02:00
")");
std::cout << " [Done]" << std::endl;
std::cout << "Started!" << std::endl;
std::cout << "Registered paths: " << std::endl;
auto handlers = drogon::app().getHandlersInfo();
for (const auto& handler : handlers) {
std::cout << " ";
if (std::get<1>(handler) == drogon::HttpMethod::Post) std::cout << "POST ";
else std::cout << "GET ";
std::string func = std::get<2>(handler).substr(16);
func.resize(30, ' ');
std::cout << '[' << func << "] ";
std::cout << std::get<0>(handler) << std::endl;
}
std::cout << "Listening on:" << std::endl;
auto listeners = drogon::app().getListeners();
for (const auto& listener : listeners) {
std::cout << " " << listener.toIpPort() << std::endl;
}
});
Json::Value access_logger;
access_logger["name"] = "drogon::plugin::AccessLogger";
2022-09-03 23:32:20 +02:00
Json::Value smtp_mail;
smtp_mail["name"] = "SMTPMail";
2022-08-28 17:37:09 +02:00
Json::Value config;
config["plugins"].append(access_logger);
2022-09-03 23:32:20 +02:00
config["plugins"].append(smtp_mail);
2022-08-28 17:37:09 +02:00
drogon::app()
2022-08-31 14:28:19 +02:00
.setClientMaxBodySize(std::numeric_limits<size_t>::max())
2022-08-28 17:37:09 +02:00
.loadConfigJson(config)
.createDbClient("sqlite3", "", 0, "", "", "", 1, "sqlite.db")
2022-08-31 14:28:19 +02:00
.setDefaultHandler(default_handler)
2022-08-28 17:37:09 +02:00
.setDocumentRoot("./static")
.setBrStatic(true)
2022-08-31 14:28:19 +02:00
.setStaticFilesCacheTime(dev_mode ? -1 : 0)
2022-08-28 17:37:09 +02:00
.setLogPath("./logs")
.setLogLevel(trantor::Logger::LogLevel::kDebug)
.setIntSignalHandler(cleanup)
.setTermSignalHandler(cleanup)
2022-09-03 23:32:20 +02:00
.enableRelaunchOnError()
.addListener("0.0.0.0", 2345)
.setThreadNum(8);
2022-08-28 17:37:09 +02:00
std::cout << "Setup done!" << std::endl;
drogon::app().run();
}