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 void showError(HTTPServerRequest req, HTTPServerResponse res, HTTPServerErrorInfo error)
38 {
39 	res.render!("error.dt", req, error);
40 }
41 
42 void showHome(HTTPServerRequest req, HTTPServerResponse res)
43 {
44 	string username = "Tester Test";
45 	res.render!("home.dt", req, username);
46 }
47 
48 auto config() {
49 
50 	auto cont = container(
51 		argument(),
52 		env(),
53 		xml("./config.xml"),
54 		xml("~/.config/aedi-example/config.xml"),
55 		xml("/etc/aedi-example/config.xml"),
56 		json("./config.json"),
57 		json("~/.config/aedi-example/config.json"),
58 		json("/etc/aedi-example/config.json"),
59 		sdlang("./config.sdlang"),
60 		sdlang("~/.config/aedi-example/config.sdlang"),
61 		sdlang("/etc/aedi-example/config.sdlang")
62 	);
63 
64 	foreach (subcontainer; cont) {
65 		with (subcontainer.configure) {
66 			register!ushort("http.port");
67 			register!(string[])("http.listen");
68 			register!string("http.host");
69 			register!bool("http.compression");
70 			register!string("log.access.file");
71 			register!string("log.access.format");
72 			register!bool("log.access.console");
73 			register!string("route.index");
74 			register!string("route.about");
75 			register!string("route.public");
76 		}
77 	}
78 
79 	return cont;
80 }
81 
82 auto services(T)(T parent) {
83 	auto cont = container(
84 		singleton,
85 		prototype
86 	);
87 
88 	with (cont[0].configure(parent)) {
89 		register!HTTPServerSettings
90 			.set!"bindAddresses"("http.listen".lref)
91 			.set!"port"("http.port".lref)
92 			.set!"accessLogFormat"("log.access.format".lref)
93 			.set!"accessLogFile"("log.access.file".lref)
94 			.set!"hostName"("http.host".lref)
95 			.set!"useCompressionIfPossible"("http.compression".lref)
96 			.set!"errorPageHandler"(toDelegate(&showError));
97 
98 		register!URLRouter
99 			.construct("")
100 			.callback((Locator!() l, ref URLRouter router) {
101 				router.get(l.locate!string("route.index"), &showHome);
102 				router.get(l.locate!string("route.about"), staticTemplate!"about.dt");
103 				router.get(l.locate!string("route.public"), serveStaticFiles("public"));
104 			});
105 	}
106 
107 	return cont;
108 }
109 
110 void main()
111 {
112 	import std.file;
113 
114 	auto c = aggregate(config, "parameters");
115 	c.set(services(c), "services");
116 	c.instantiate;
117 	scope(exit) c.terminate;
118 
119 	import std.stdio;
120 	listenHTTP(c.locate!HTTPServerSettings, c.locate!URLRouter);
121 	lowerPrivileges();
122 	runEventLoop();
123 }