1 module semitwistWeb.conf;
2 
3 import std.conv;
4 import std.exception;
5 
6 import sdlang;
7 import vibe.mail.smtp : SMTPAuthType, SMTPConnectionType;
8 import semitwistWeb.util;
9 
10 private Conf conf_;
11 static @property const(Conf) conf()
12 {
13 	return conf_;
14 }
15 static @property Tag confSdlRoot()
16 {
17 	return conf_.sdlRoot;
18 }
19 void loadConf(string configFile = null)
20 {
21 	conf_.load(configFile);
22 }
23 
24 struct Conf
25 {
26 	string host;
27 	string urlBase;
28 
29 	string staticsRealPath;
30 	string staticsVirtualPath;
31 
32 	// DB Connection Settings
33 	string dbHost;
34 	ushort dbPort;
35 	string dbUser;
36 	string dbPass;
37 	string dbName;
38 
39 	// SMTP Settings
40 	SMTPAuthType       smtpAuthType;
41 	SMTPConnectionType smtpConnectionType;
42 	string smtpHost;
43 	//string smtpLocalName;
44 	ushort smtpPort;
45 	string smtpUser;
46 	string smtpPass;
47 
48 	string staticsUrl;
49 
50 	Tag sdlRoot;
51 
52 	void load(string configFile = null)
53 	{
54 		if(configFile == "")
55 		{
56 			import std.file : thisExePath;
57 			import std.path : dirName;
58 			configFile = thisExePath.dirName ~ "/semitwistweb.conf.sdl";
59 		}
60 
61 		auto root = parseFile(configFile);
62 		this.sdlRoot = root;
63 		host               = root.expectTagValue!string("host");
64 		urlBase            = root.expectTagValue!string("urlBase");
65 		staticsRealPath    = root.expectTagValue!string("staticsRealPath");
66 		staticsVirtualPath = root.expectTagValue!string("staticsVirtualPath");
67 
68 		dbHost             = root.expectTagValue!string("dbHost");
69 		dbPort             = root.expectTagValue!int("dbPort").to!ushort;
70 		dbUser             = root.expectTagValue!string("dbUser");
71 		dbPass             = root.expectTagValue!string("dbPass");
72 		dbName             = root.expectTagValue!string("dbName");
73 
74 		smtpAuthType       = root.expectTagValue!string("smtpAuthType").toEnum!SMTPAuthType;
75 		smtpConnectionType = root.expectTagValue!string("smtpConnectionType").toEnum!SMTPConnectionType;
76 		//smtpLocalName      = root.getTagValue!string("smtpLocalName");
77 		smtpPort           = root.expectTagValue!int("smtpPort").to!ushort;
78 		smtpUser           = root.expectTagValue!string("smtpUser");
79 		smtpPass           = root.expectTagValue!string("smtpPass");
80 
81 		// Validate:
82 		import std..string;
83 		enforce!ValidationException(
84 			urlBase.length > 0  &&
85 			urlBase[0  ] == '/' &&
86 			urlBase[$-1] == '/',
87 			"urlBase must start and end with a slash"
88 		);
89 		enforce!ValidationException(
90 			host.strip().length > 0,
91 			"host cannot not be blank"
92 		);
93 		enforce!ValidationException(
94 			host.strip() == host,
95 			"host cannot have leading or trailing whitespace"
96 		);
97 		enforce!ValidationException(
98 			host[$-1] != '/',
99 			"host cannot end with a slash"
100 		);
101 		enforce!ValidationException(
102 			host.toLower().startsWith("http://") ||
103 			host.toLower().startsWith("https://"),
104 			"host must begin with either http:// or https://"
105 		);
106 
107 		staticsUrl = urlBase ~ staticsVirtualPath;
108 	}
109 }