Started working on settings.

This commit is contained in:
Relintai 2020-12-08 16:22:18 +01:00
parent 7ea1f4dada
commit a6b29c04a4
7 changed files with 115 additions and 6 deletions

2
.gitignore vendored
View File

@ -4,3 +4,5 @@ bin/**
*.o
*.a
*.pyc
settings.json

View File

@ -12,6 +12,9 @@
#include <stdio.h>
#include <stdlib.h>
void Application::load_settings() {
}
void Application::setup_routes() {
default_error_handler_func = Application::default_fallback_error_handler;

View File

@ -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();

View File

@ -1,2 +1,47 @@
#include "settings.h"
#include <stdio.h>
#include <stdlib.h>
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;

View File

@ -1,9 +1,23 @@
#ifndef SETTINGS_H
#define SETTINGS_H
#include <string>
#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;
};

View File

@ -1,6 +1,6 @@
#include <string.h>
#include <iostream>
#include <string>
#include <string.h>
#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;
}

13
settings.json.example Normal file
View File

@ -0,0 +1,13 @@
{
"databases": {
"0" : {
"type": "mysql",
"host": "127.0.0.1",
"user": "",
"password": "",
"dbname": "testappdb",
"port": "3306"
}
}
}