Browse Source

add toml config

221V 1 week ago
parent
commit
bf13268c38
5 changed files with 117 additions and 2 deletions
  1. 1 0
      stest/dub.json
  2. 3 1
      stest/dub.selections.json
  3. 6 0
      stest/settings.toml
  4. 11 1
      stest/source/app.d
  5. 96 0
      stest/source/settings_toml.d

+ 1 - 0
stest/dub.json

@@ -6,6 +6,7 @@
   "dflags": ["-w", "-O"],
   "dflags": ["-w", "-O"],
   "targetType": "executable",
   "targetType": "executable",
   "dependencies": {
   "dependencies": {
+    "toml": "~>2.0.1",
     "serverino": "~>0.7.18"
     "serverino": "~>0.7.18"
   },
   },
   "libs": [],
   "libs": [],

+ 3 - 1
stest/dub.selections.json

@@ -1,6 +1,8 @@
 {
 {
 	"fileVersion": 1,
 	"fileVersion": 1,
 	"versions": {
 	"versions": {
-		"serverino": "0.7.18"
+		"serverino": "0.7.18",
+		"toml": "2.0.1",
+		"unit-threaded": "2.0.5"
 	}
 	}
 }
 }

+ 6 - 0
stest/settings.toml

@@ -0,0 +1,6 @@
+# settings
+
+[http]
+port = 8081 # integer
+host = "127.0.0.1" # string
+

+ 11 - 1
stest/source/app.d

@@ -1,6 +1,8 @@
 
 
+
 import std;
 import std;
 
 
+
 // Docs: https://trikko.github.io/serverino/
 // Docs: https://trikko.github.io/serverino/
 // Tips and tricks: https://github.com/trikko/serverino/wiki/
 // Tips and tricks: https://github.com/trikko/serverino/wiki/
 // Examples: https://github.com/trikko/serverino/tree/master/examples
 // Examples: https://github.com/trikko/serverino/tree/master/examples
@@ -8,13 +10,21 @@ import serverino;
 
 
 mixin ServerinoMain;
 mixin ServerinoMain;
 
 
+
+import settings_toml; // get settings from settings.toml, settings keys, settings validation etc
+
+
 @onServerInit ServerinoConfig setup(){
 @onServerInit ServerinoConfig setup(){
   ServerinoConfig sc = ServerinoConfig.create(); // Config with default params
   ServerinoConfig sc = ServerinoConfig.create(); // Config with default params
   //writeln("ServerinoConfig sc = ", sc);
   //writeln("ServerinoConfig sc = ", sc);
   // ServerinoConfig sc = ServerinoConfig(
   // ServerinoConfig sc = ServerinoConfig(
   //  DaemonConfig(all, 10485760, 10 secs, 3 secs, 0, 5, 2048, false, true, false, []),
   //  DaemonConfig(all, 10485760, 10 secs, 3 secs, 0, 5, 2048, false, true, false, []),
   //  WorkerConfig(5 secs, 10 secs, 6 hours, 1 hour, 1 minute, true, "", "", false), [], 0, false)
   //  WorkerConfig(5 secs, 10 secs, 6 hours, 1 hour, 1 minute, true, "", "", false), [], 0, false)
-  sc.addListener("127.0.0.1", 8081);
+  
+  read_settings_toml(); // read settings, settings validation
+  //sc.addListener("127.0.0.1", 8081);
+  sc.addListener(toml_s[s_toml_http][s_toml_http_host].str().idup, cast(ushort)toml_s[s_toml_http][s_toml_http_port].integer() );
+  
   //writeln("ServerinoConfig sc = ", sc);
   //writeln("ServerinoConfig sc = ", sc);
   //ServerinoConfig sc = ServerinoConfig(
   //ServerinoConfig sc = ServerinoConfig(
   //  DaemonConfig(all, 10485760, 10 secs, 3 secs, 0, 5, 2048, false, true, false, [serverino.config.Listener]),
   //  DaemonConfig(all, 10485760, 10 secs, 3 secs, 0, 5, 2048, false, true, false, [serverino.config.Listener]),

+ 96 - 0
stest/source/settings_toml.d

@@ -0,0 +1,96 @@
+
+public{
+  import std.stdio;
+  import std.file : read;
+  import toml;
+}
+
+TOMLDocument toml_s;
+
+// settings keys
+string s_toml_http            = "http";
+string s_toml_http_host       = "host";
+string s_toml_http_port       = "port";
+
+/*
+string s_toml_db              = "database";
+string s_toml_db_host         = "host";
+string s_toml_db_port         = "port";
+string s_toml_db_name         = "dbname";
+string s_toml_db_user         = "user";
+string s_toml_db_pass         = "pass";
+string s_toml_db_conn_timeout = "connect_timeout";
+string s_toml_db_conn_num     = "connections_number";
+*/
+
+void read_settings_toml(){ // read settings, settings validation
+  toml_s = parseTOML(cast(string)read("settings.toml"));
+  if( are_valid_config_values(toml_s) ){}else{ return; } // settings validation
+}
+
+
+bool are_valid_config_values(ref TOMLDocument toml_s){ // settings validation
+  string invalid_settings = "invalid settings: ";
+  string grumpy = " :(";
+  string invalid_group = " group ";
+  string invalid_key = " key" ~ grumpy;
+  
+  bool invalid_toml_group(string group){
+    writeln(invalid_settings ~ group ~ invalid_group ~ grumpy); return false;
+  }
+  
+  bool invalid_toml_value(string group, string key){
+    writeln(invalid_settings ~ group ~ invalid_group ~ key ~ invalid_key ~ grumpy); return false;
+  }
+  
+  
+  if((s_toml_http in toml_s) != null){
+    auto toml_http = toml_s[s_toml_http];
+    
+    if((s_toml_http_host in toml_http) != null){
+      if(toml_http[s_toml_http_host].type == TOMLType.STRING){}else{ return invalid_toml_value(s_toml_http, s_toml_http_host); }
+    }else{ return invalid_toml_value(s_toml_http, s_toml_http_host); }
+    
+    if((s_toml_http_port in toml_http) != null){
+      if(toml_http[s_toml_http_port].type == TOMLType.INTEGER){}else{ return invalid_toml_value(s_toml_http, s_toml_http_port); }
+    }else{ return invalid_toml_value(s_toml_http, s_toml_http_port); }
+    
+  }else{ return invalid_toml_group(s_toml_http); }
+  
+  /*
+  if((s_toml_db in toml_s) != null){
+    auto toml_db = toml_s[s_toml_db];
+    
+    if((s_toml_db_host in toml_db) != null){
+      if(toml_db[s_toml_db_host].type == TOMLType.STRING){}else{ return invalid_toml_value(s_toml_db, s_toml_db_host); }
+    }else{ return invalid_toml_value(s_toml_db, s_toml_db_host); }
+    
+    if((s_toml_db_port in toml_db) != null){
+      if(toml_db[s_toml_db_port].type == TOMLType.STRING){}else{ return invalid_toml_value(s_toml_db, s_toml_db_port); }
+    }else{ return invalid_toml_value(s_toml_db, s_toml_db_port); }
+    
+    if((s_toml_db_name in toml_db) != null){
+      if(toml_db[s_toml_db_name].type == TOMLType.STRING){}else{ return invalid_toml_value(s_toml_db, s_toml_db_name); }
+    }else{ return invalid_toml_value(s_toml_db, s_toml_db_name); }
+    
+    if((s_toml_db_user in toml_db) != null){
+      if(toml_db[s_toml_db_user].type == TOMLType.STRING){}else{ return invalid_toml_value(s_toml_db, s_toml_db_user); }
+    }else{ return invalid_toml_value(s_toml_db, s_toml_db_user); }
+    
+    if((s_toml_db_pass in toml_db) != null){
+      if(toml_db[s_toml_db_pass].type == TOMLType.STRING){}else{ return invalid_toml_value(s_toml_db, s_toml_db_pass); }
+    }else{ return invalid_toml_value(s_toml_db, s_toml_db_pass); }
+    
+    if((s_toml_db_conn_timeout in toml_db) != null){
+      if(toml_db[s_toml_db_conn_timeout].type == TOMLType.STRING){}else{ return invalid_toml_value(s_toml_db, s_toml_db_conn_timeout); }
+    }else{ return invalid_toml_value(s_toml_db, s_toml_db_conn_timeout); }
+    
+    if((s_toml_db_conn_num in toml_db) != null){
+      if(toml_db[s_toml_db_conn_num].type == TOMLType.INTEGER){}else{ return invalid_toml_value(s_toml_db, s_toml_db_conn_num); }
+    }else{ return invalid_toml_value(s_toml_db, s_toml_db_conn_num); }
+  
+  }else{ return invalid_toml_group(s_toml_db); }
+  */
+  return true;
+}
+