DB: remove shared stuff, add singleton
Add Config class
This commit is contained in:
21
source/config.d
Normal file
21
source/config.d
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,17 +24,17 @@ class DB {
|
|||||||
protected DataSource m_ds;
|
protected DataSource m_ds;
|
||||||
protected Connection m_conn;
|
protected Connection m_conn;
|
||||||
|
|
||||||
shared this(DBSettings settings) {
|
this(DBSettings settings) {
|
||||||
this.m_settings = settings;
|
this.m_settings = settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
static shared(DB) getDB(DBSettings settings) {
|
static DB getDB(DBSettings settings) {
|
||||||
switch (settings.connector) {
|
switch (settings.connector) {
|
||||||
case DBConnector.DB_PGSQL:
|
case DBConnector.DB_PGSQL:
|
||||||
import db.pgsql;
|
import db.pgsql;
|
||||||
|
|
||||||
debugF!"DB type is PostgreSQL";
|
debugF!"DB type is PostgreSQL";
|
||||||
return new shared PostgresDB(settings);
|
return new PostgresDB(settings);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new Exception("DB not supported");
|
throw new Exception("DB not supported");
|
||||||
|
|||||||
@@ -5,8 +5,10 @@ import db.db;
|
|||||||
import ddbc.core;
|
import ddbc.core;
|
||||||
import ddbc.common;
|
import ddbc.common;
|
||||||
|
|
||||||
|
import slf4d;
|
||||||
|
|
||||||
class PostgresDB : DB {
|
class PostgresDB : DB {
|
||||||
shared this(Args...)(auto ref Args args) {
|
this(Args...)(auto ref Args args) {
|
||||||
super(args);
|
super(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,6 +24,8 @@ class PostgresDB : DB {
|
|||||||
params = PGSQLDriver.setUserAndPassword(username, password);
|
params = PGSQLDriver.setUserAndPassword(username, password);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
debugF!"DB URL: %s"(url);
|
||||||
|
|
||||||
return new ConnectionPoolDataSourceImpl(driver, url, params);
|
return new ConnectionPoolDataSourceImpl(driver, url, params);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,45 @@
|
|||||||
module main;
|
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;
|
||||||
}
|
}
|
||||||
|
|||||||
5
source/singletons.d
Normal file
5
source/singletons.d
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
module singletons;
|
||||||
|
|
||||||
|
import db.db;
|
||||||
|
|
||||||
|
DB Db;
|
||||||
Reference in New Issue
Block a user