1 /**
2 License:
3 	Boost Software License - Version 1.0 - August 17th, 2003
4 
5 	Permission is hereby granted, free of charge, to any person or organization
6 	obtaining a copy of the software and accompanying documentation covered by
7 	this license (the "Software") to use, reproduce, display, distribute,
8 	execute, and transmit the Software, and to prepare derivative works of the
9 	Software, and to permit third-parties to whom the Software is furnished to
10 	do so, all subject to the following:
11 
12 	The copyright notices in the Software and this entire statement, including
13 	the above license grant, this restriction and the following disclaimer,
14 	must be included in all copies of the Software, in whole or in part, and
15 	all derivative works of the Software, unless such copies or derivative
16 	works are solely in the form of machine-executable object code generated by
17 	a source language processor.
18 
19 	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 	FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22 	SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23 	FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24 	ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 	DEALINGS IN THE SOFTWARE.
26 
27 Authors:
28 	Alexandru Ermicioi
29 **/
30 module app;
31 
32 import aermicioi.aedi;
33 import aermicioi.aedi_property_reader;
34 import vibe.vibe;
35 import std.functional : toDelegate;
36 
37 public alias configure = aermicioi.aedi.configurer.register.context.configure;
38 public alias configure = aermicioi.aedi_property_reader.core.core.configure;
39 
40 void showError(HTTPServerRequest req, HTTPServerResponse res, HTTPServerErrorInfo error)
41 {
42 	res.render!("error.dt", req, error);
43 }
44 
45 void showHome(HTTPServerRequest req, HTTPServerResponse res)
46 {
47 	string username = "Tester Test";
48 	res.render!("home.dt", req, username);
49 }
50 
51 auto config() {
52 
53 	auto cont = container(
54 		argument(),
55 		env(),
56 		xml("./config.xml"),
57 		xml("~/.config/aedi-example/config.xml"),
58 		xml("/etc/aedi-example/config.xml"),
59 		json("./config.json"),
60 		json("~/.config/aedi-example/config.json"),
61 		json("/etc/aedi-example/config.json"),
62 		sdlang("./config.sdlang"),
63 		sdlang("~/.config/aedi-example/config.sdlang"),
64 		sdlang("/etc/aedi-example/config.sdlang")
65 	);
66 
67 	foreach (subcontainer; cont) {
68 		with (subcontainer.configure) {
69 			register!ushort("http.port");
70 			register!(string[])("http.listen");
71 			register!string("http.host");
72 			register!bool("http.compression");
73 			register!string("log.access.file");
74 			register!string("log.access.format");
75 			register!bool("log.access.console");
76 			register!string("route.index");
77 			register!string("route.about");
78 			register!string("route.public");
79 		}
80 	}
81 
82 	return cont;
83 }
84 
85 auto services(T)(T parent) {
86 	auto cont = container(
87 		singleton,
88 		prototype
89 	);
90 
91 	with (cont[0].configure(parent)) {
92 		register!HTTPServerSettings
93 			.set!"bindAddresses"("http.listen".lref)
94 			.set!"port"("http.port".lref)
95 			.set!"accessLogFormat"("log.access.format".lref)
96 			.set!"accessLogFile"("log.access.file".lref)
97 			.set!"hostName"("http.host".lref)
98 			.set!"useCompressionIfPossible"("http.compression".lref)
99 			.set!"errorPageHandler"(toDelegate(&showError));
100 
101 		register!URLRouter
102 			.construct("")
103 			.callback((Locator!() l, ref URLRouter router) {
104 				router.get(l.locate!string("route.index"), &showHome);
105 				router.get(l.locate!string("route.about"), staticTemplate!"about.dt");
106 				router.get(l.locate!string("route.public"), serveStaticFiles("public"));
107 			});
108 	}
109 
110 	return cont;
111 }
112 
113 void main()
114 {
115 	import std.file;
116 
117 	auto c = aggregate(config, "parameters");
118 	c.set(services(c), "services");
119 	c.instantiate;
120 	scope(exit) c.terminate;
121 
122 	import std.stdio;
123 	listenHTTP(c.locate!HTTPServerSettings, c.locate!URLRouter);
124 	lowerPrivileges();
125 	runEventLoop();
126 }