Added dev mode for backend

This commit is contained in:
Mutzi 2022-08-31 14:28:19 +02:00
parent 8669eed13c
commit df93c5e091
2 changed files with 29 additions and 6 deletions

View File

@ -87,7 +87,7 @@ namespace api {
user_mapper.insert(new_user);
generate_root(new_user);
cbk(dto::Responses::get_success_res());
} catch (const std::exception& e) {
} catch (const std::exception&) {
cbk(dto::Responses::get_badreq_res("Validation error"));
}
}

View File

@ -1,10 +1,13 @@
#include <filesystem>
#include <fstream>
#include <drogon/drogon.h>
#include <curl/curl.h>
#include "dto/dto.h"
bool dev_mode = false;
void cleanup() {
std::cout << "Stopping..." << std::endl;
drogon::app().quit();
@ -14,7 +17,26 @@ void cleanup() {
std::cout << "Goodbye!" << std::endl;
}
int main() {
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;
std::cout << "Setting up..." << std::endl;
std::cout << "Initializing curl..." << std::flush;
curl_global_init(CURL_GLOBAL_ALL);
@ -87,16 +109,17 @@ int main() {
config["plugins"].append(access_logger);
drogon::app()
.setClientMaxBodySize(1024L * 1024L * 1024L * 1024L) // 1 TB
.setClientMaxBodySize(std::numeric_limits<size_t>::max())
.loadConfigJson(config)
.createDbClient("sqlite3", "", 0, "", "", "", 1, "sqlite.db")
.setCustom404Page(drogon::HttpResponse::newFileResponse("./static/index.html"), false)
.setDefaultHandler(default_handler)
.setDocumentRoot("./static")
.setBrStatic(true)
.setStaticFilesCacheTime(0)
.setStaticFilesCacheTime(dev_mode ? -1 : 0)
.setLogPath("./logs")
.setLogLevel(trantor::Logger::LogLevel::kDebug)
@ -104,7 +127,7 @@ int main() {
.setIntSignalHandler(cleanup)
.setTermSignalHandler(cleanup)
.addListener("0.0.0.0", 1234)
.addListener("0.0.0.0", 5678)
.setThreadNum(2);
std::cout << "Setup done!" << std::endl;