1 /**
2 * Copyright © DiamondMVC 2019
3 * License: MIT (https://github.com/DiamondMVC/Diamond/blob/master/LICENSE)
4 * Author: Jacob Jensen (bausshf)
5 */
6 module diamond.core.webconfig;
7
8 import diamond.core.apptype;
9
10 static if (isWeb)
11 {
12 import vibe.data.serialization : optional;
13
14 /// Web configurations.
15 final class WebConfig
16 {
17 /// The name of the web application.
18 string name;
19 /// The routes that are mapped to static files.
20 @optional string[] staticFileRoutes;
21 /// The paths to white-list for file-access/directory-access.
22 @optional string[] whiteListPaths;
23 /// A list of hosts that the server accepts.
24 @optional string[] hostWhiteList;
25 /// The route that's mapped to the home page.
26 string homeRoute;
27 /// Boolean determining whether views can be accessed by their file name.
28 @optional bool allowFileRoute;
29 /// An array of addresses the web application is accessible by.
30 WebAddress[] addresses;
31 /// The default headers the web application uses.
32 WebHeaders defaultHeaders;
33 /// Boolean determining whether the access log should be redirected to the console.
34 @optional bool accessLogToConsole;
35 /// The time sessions are stored in memory.
36 @optional long sessionAliveTime;
37 // A special string representation that splits the root routes when checking ACL.
38 @optional string specialRouteSplitter;
39 /// Boolean determnining whether views can be cached or not.
40 @optional bool shouldCacheViews;
41 /// An array of global restricted ip addresses.
42 @optional string[] globalRestrictedIPs;
43 /// An array of restricted ip addresses.
44 @optional string[] restrictedIPs;
45 /// A collection of db connection configurations.
46 @optional WebDbConnections dbConnections;
47 /// Tn associative array of specialized routes.
48 @optional WebSpecialRoute[string] specializedRoutes;
49 /// A static web-page to display for maintenance. When specified the website will automatically be set to maintenance-mode.
50 @optional string maintenance;
51 /// An array of ips that can still access the site during maintenance.
52 @optional string[] maintenanceWhiteList;
53 /// Boolean determining whethere there's only one view to use for routing. The view must be named __view.dd
54 @optional bool viewOnly;
55 /// Configurations for mongo db.
56 @optional WebMongoDb mongoDb;
57 /// Configuration to disable privacy logging. When set to true, personal information etc. will be stored in the logging.
58 @optional bool disablePrivacyLogging;
59 /// Configuration for the ssl certificate file.
60 @optional string sslCertificateFile;
61 /// Configuration for the ssl private key file.
62 @optional string sslPrivateKeyFile;
63 /// The http header that contains the client's ip. Useful when using reverse proxies.
64 @optional string ipHeader;
65 /// Collection of webservices.
66 @optional WebService[] webservices;
67 /// An associative array of custom configurations;
68 @optional string[string] customConfig;
69 /// String of an url to redirect all connections that aren't ssl to.
70 @optional string forceSSLUrl;
71 /// An array of routes that shouldn't have logging for headers.
72 @optional string[] disableHeaderLoggingRoutes;
73 /// An associative array of mapped auth keys.
74 @optional string[string] mappedAuthKeys;
75 /// The maximum number of bytes a request can transfer. Defaults to 4,000,000 bytes.
76 @optional ulong maxRequestSize;
77 /// The maximum number of bytes a request header can be. Default is 8,192 bytes.
78 @optional ulong maxRequestHeaderSize;
79 /// Boolean determining whether authentication should run for static files or not.
80 @optional bool authenticateStaticFiles;
81 }
82
83 /// Wrapper around a webservice.
84 final class WebService
85 {
86 /// The name of the webservice.
87 string name;
88
89 /// The wsdl of the webservice.
90 string wsdl;
91
92 /// The module name of the webservice.
93 string moduleName;
94 }
95
96 /// A web address.
97 final class WebAddress
98 {
99 /// An array of ip addresses that the web address is bound to.
100 string[] ipAddresses;
101 /// The port the web address is bound to.
102 ushort port;
103 }
104
105 /// Web headers.
106 final class WebHeaders
107 {
108 /// Headers used for general purpose.
109 string[string] general;
110 /// Headers used for static files.
111 string[string] staticFiles;
112 /// Headers used for 404 responses.
113 string[string] notFound;
114 /// Headers used for error responses.
115 string[string] error;
116 }
117
118 /// Wrapper around db connection configurations.
119 final class WebDbConnections
120 {
121 @optional WebDbConnectionConfig[string] mysql;
122 @optional WebDbConnectionConfig[string] mssql;
123 @optional WebDbConnectionConfig[string] postgresql;
124 @optional WebDbConnectionConfig[string] sqlite;
125 }
126
127 /// Wrapper around a db connection configuration.
128 final class WebDbConnectionConfig
129 {
130 /// The host.
131 string host;
132 /// The port.
133 @optional ushort port;
134 /// The user.
135 string user;
136 /// The password.
137 string password;
138 /// The database.
139 @optional string database;
140
141 static if (hasMsSql)
142 {
143 string namedInstance;
144 }
145 }
146
147 /// Wrapper around a special route.
148 final class WebSpecialRoute
149 {
150 /// The type of the route.
151 string type;
152 /// The value of the route.
153 string value;
154 }
155
156 /// Wrapper around mongo db configurations.
157 final class WebMongoDb
158 {
159 /// The host of the mongo db.
160 string host;
161 /// The port of the mongo db. This should only be used if the host is an IP.
162 @optional ushort port;
163 }
164
165 /// The web configuration.
166 private static __gshared WebConfig _webConfig;
167
168 /// Gets the web configuration.
169 @property WebConfig webConfig() { return _webConfig; }
170
171 /// Loads the web configuration.
172 void loadWebConfig()
173 {
174 import vibe.d : deserializeJson;
175 import std.file : readText;
176
177 _webConfig = deserializeJson!WebConfig(readText("config/web.json"));
178
179 if (_webConfig.homeRoute[0] == '/')
180 {
181 _webConfig.homeRoute = _webConfig.homeRoute[1 .. $];
182 }
183
184 if (_webConfig.homeRoute[$-1] == '/')
185 {
186 _webConfig.homeRoute = _webConfig.homeRoute[0 .. $-1];
187 }
188
189 if (_webConfig.sessionAliveTime <= 0)
190 {
191 _webConfig.sessionAliveTime = 30;
192 }
193
194 if (!_webConfig.specialRouteSplitter)
195 {
196 _webConfig.specialRouteSplitter = "-";
197 }
198 }
199 }