Browse Source

mv settings read - validation to separated module

221V 3 days ago
parent
commit
680d3cf947
2 changed files with 97 additions and 87 deletions
  1. 3 87
      vtest/source/app.d
  2. 94 0
      vtest/source/settings_toml.d

+ 3 - 87
vtest/source/app.d

@@ -12,7 +12,6 @@ import vibe.http.websockets;
 import vibe.core.log;
 
 
-import std.stdio;
 import std.string;
 //import std.array;
 
@@ -34,26 +33,8 @@ import vibe.data.bson;
 
 PostgresClient[] clients;
 
+import settings_toml; // get settings from settings.toml, settings keys, settings validation etc
 
-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";
 
 
 
@@ -106,8 +87,8 @@ string test_args_types_mismash2(test_key3 x, test_key4 y){
 
 
 void main(){
-  toml_s = parseTOML(cast(string)read("settings.toml"));
-  if( are_valid_config_values(toml_s) ){}else{ return; } // settings validation
+  read_settings_toml(); // read settings, settings validation
+  
   uint conn_num = cast(uint) toml_s[s_toml_db][s_toml_db_conn_num].integer();
   uint8 i = cast(uint8) conn_num;
   // https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING
@@ -182,71 +163,6 @@ void hello(HTTPServerRequest req, HTTPServerResponse res){
 */
 
 
-
-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;
-}
-
 /*
 void test_pg_conn_driver(){ // https://github.com/denizzzka/vibe.d.db.postgresql/blob/master/example/example.d#L13
   client.pickConnection( (scope conn){

+ 94 - 0
vtest/source/settings_toml.d

@@ -0,0 +1,94 @@
+
+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;
+}
+