diff --git a/source/config.d b/source/config.d new file mode 100644 index 0000000..0214b33 --- /dev/null +++ b/source/config.d @@ -0,0 +1,21 @@ +module config; + +import std.json; +import std.file; + +import slf4d; + +class Config { + JSONValue v; + private string m_path; + + this(string path = "./config.json") { + this.m_path = path; + } + + void load() { + debugF!"Trying to load config from %s"(this.m_path); + string contents = readText(this.m_path); + this.v = parseJSON(contents); + } +} diff --git a/source/db/db.d b/source/db/db.d index 399163e..26ad5dc 100644 --- a/source/db/db.d +++ b/source/db/db.d @@ -24,17 +24,17 @@ class DB { protected DataSource m_ds; protected Connection m_conn; - shared this(DBSettings settings) { + this(DBSettings settings) { this.m_settings = settings; } - static shared(DB) getDB(DBSettings settings) { + static DB getDB(DBSettings settings) { switch (settings.connector) { case DBConnector.DB_PGSQL: import db.pgsql; debugF!"DB type is PostgreSQL"; - return new shared PostgresDB(settings); + return new PostgresDB(settings); default: throw new Exception("DB not supported"); diff --git a/source/db/pgsql.d b/source/db/pgsql.d index d10fb7b..51ae23a 100644 --- a/source/db/pgsql.d +++ b/source/db/pgsql.d @@ -5,8 +5,10 @@ import db.db; import ddbc.core; import ddbc.common; +import slf4d; + class PostgresDB : DB { - shared this(Args...)(auto ref Args args) { + this(Args...)(auto ref Args args) { super(args); } @@ -22,6 +24,8 @@ class PostgresDB : DB { params = PGSQLDriver.setUserAndPassword(username, password); } + debugF!"DB URL: %s"(url); + return new ConnectionPoolDataSourceImpl(driver, url, params); } } diff --git a/source/main.d b/source/main.d index a9038e4..d520425 100644 --- a/source/main.d +++ b/source/main.d @@ -1,5 +1,45 @@ module main; -void main() { +import slf4d; +import slf4d.default_provider; +import config; +import singletons; +import db.db; + +int main() { + auto provider = new DefaultProvider(true, Levels.DEBUG); + configureLoggingProvider(provider); + + Config cfg = new Config(); + + try { + cfg.load(); + } catch (Exception e) { + error(e); + return 21; + } + + DBSettings dbSettings; + auto dbCfg = cfg.v["db"]; + with (dbSettings) { + host = dbCfg["host"].str; + port = cast(ushort) dbCfg["port"].integer; + username = dbCfg["username"].str; + password = dbCfg["password"].str; + dbname = dbCfg["dbName"].str; + + switch (dbCfg["connector"].str) { + case "postgresql": + connector = DBConnector.DB_PGSQL; + break; + default: + break; + } + } + + Db = DB.getDB(dbSettings); + Db.connect(); + + return 0; } diff --git a/source/singletons.d b/source/singletons.d new file mode 100644 index 0000000..d91aea1 --- /dev/null +++ b/source/singletons.d @@ -0,0 +1,5 @@ +module singletons; + +import db.db; + +DB Db;