From a6b29c04a429db83d2381ec9934df088858aeda4 Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 8 Dec 2020 16:22:18 +0100 Subject: [PATCH] Started working on settings. --- .gitignore | 2 ++ core/application.cpp | 3 +++ core/application.h | 1 + core/settings.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++ core/settings.h | 16 ++++++++++++++- main.cpp | 41 ++++++++++++++++++++++++++++++++++----- settings.json.example | 13 +++++++++++++ 7 files changed, 115 insertions(+), 6 deletions(-) create mode 100644 settings.json.example diff --git a/.gitignore b/.gitignore index 691f43d..fc698b6 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ bin/** *.o *.a *.pyc + +settings.json diff --git a/core/application.cpp b/core/application.cpp index e949917..40886aa 100644 --- a/core/application.cpp +++ b/core/application.cpp @@ -12,6 +12,9 @@ #include #include +void Application::load_settings() { +} + void Application::setup_routes() { default_error_handler_func = Application::default_fallback_error_handler; diff --git a/core/application.h b/core/application.h index 5342d2f..9374eaf 100644 --- a/core/application.h +++ b/core/application.h @@ -23,6 +23,7 @@ public: static void default_fallback_error_handler(int error_code, Request *request); static void default_404_error_handler(int error_code, Request *request); + virtual void load_settings(); virtual void setup_routes(); virtual void setup_middleware(); diff --git a/core/settings.cpp b/core/settings.cpp index 6816e88..c262d54 100644 --- a/core/settings.cpp +++ b/core/settings.cpp @@ -1,2 +1,47 @@ #include "settings.h" +#include +#include + +void Settings::parse_file(const std::string &path) { + FILE *f = fopen(path.c_str(), "r"); + + if (!f) { + printf("Settings::parse_file: Error opening file!"); + return; + } + + fseek(f, 0, SEEK_END); + long fsize = ftell(f); + fseek(f, 0, SEEK_SET); /* same as rewind(f); */ + + std::string config_str; + config_str.resize(fsize); + + fread(&config_str[0], 1, fsize, f); + fclose(f); + + settings.Parse(config_str.c_str()); +} + +Settings *Settings::get_singleton() { + return _singleton; +} + +Settings::Settings(const bool singleton) { + if (singleton) { + if (_singleton) { + printf("Settings singleton overridden!\n"); + } + + _singleton = this; + } +} + +Settings::~Settings() { + if (_singleton == this) { + _singleton = nullptr; + } +} + +Settings *Settings::_singleton = nullptr; \ No newline at end of file diff --git a/core/settings.h b/core/settings.h index fd105b9..e941119 100644 --- a/core/settings.h +++ b/core/settings.h @@ -1,9 +1,23 @@ #ifndef SETTINGS_H #define SETTINGS_H +#include + +#include "rapidjson/document.h" + class Settings { public: - int i; + rapidjson::Document settings; + + void parse_file(const std::string &path); + + static Settings *get_singleton(); + + Settings(const bool singleton = false); + virtual ~Settings(); + +protected: + static Settings *_singleton; }; diff --git a/main.cpp b/main.cpp index 25c72e0..d9b52a7 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,6 @@ +#include #include #include -#include #include "core/application.h" #include "core/file_cache.h" @@ -12,8 +12,36 @@ #include "database/db_init.h" +#include "core/settings.h" + #define MAIN_CLASS RDNApplication +void create_databases() { + + Settings *settings = Settings::get_singleton(); + + if (!settings) { + printf("create_databases: Settings singleton is null!"); + return; + } + +/* + rapidjson::Value dbs = settings->settings["databases"]; + + if (!dbs.IsArray()) { + printf("create_databases: dbs !dbs.IsArray()!"); + return; + } +*/ + + DatabaseManager *dbm = DatabaseManager::get_singleton(); + + uint32_t index = dbm->create_database("mysql"); + Database *db = dbm->databases[0]; + //db->_builder_creation_func = MysqlQueryBuilder::create; + db->connect(""); +} + int main(int argc, char **argv) { bool migrate = false; @@ -27,19 +55,21 @@ int main(int argc, char **argv) { initialize_database_backends(); + Settings *settings = new Settings(true); + + settings->parse_file("settings.json"); + FileCache *file_cache = new FileCache(true); file_cache->wwwroot = "./www"; file_cache->wwwroot_refresh_cache(); DatabaseManager *dbm = new DatabaseManager(); - uint32_t index = dbm->create_database("mysql"); - Database *db = dbm->databases[0]; - //db->_builder_creation_func = MysqlQueryBuilder::create; - db->connect(""); + create_databases(); Application *app = new MAIN_CLASS(); + app->load_settings(); app->setup_routes(); app->setup_middleware(); @@ -59,6 +89,7 @@ int main(int argc, char **argv) { delete app; delete dbm; delete file_cache; + delete settings; return 0; } \ No newline at end of file diff --git a/settings.json.example b/settings.json.example new file mode 100644 index 0000000..4a9ab45 --- /dev/null +++ b/settings.json.example @@ -0,0 +1,13 @@ +{ + "databases": { + "0" : { + "type": "mysql", + "host": "127.0.0.1", + "user": "", + "password": "", + "dbname": "testappdb", + "port": "3306" + } + } +} + \ No newline at end of file