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.http.fileserver;
35 import vibe.http.router;
36 import vibe.http.server;
37 import std.functional : toDelegate;
38 
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 void routed(URLRouter router) {
52 	router.get("/", &showHome);
53 	router.get("/about", staticTemplate!"about.dt");
54 	router.get("*", serveStaticFiles("public"));
55 }
56 
57 auto config() {
58 	
59 	auto cont = container(
60 		argument(),
61 		environment(),
62 		xml("./config.xml"),
63 		xml("~/.config/aedi-example/config.xml"),
64 		xml("/etc/aedi-example/config.xml"),
65 		json("./config.json"),
66 		json("~/.config/aedi-example/config.json"),
67 		json("/etc/aedi-example/config.json")
68 	);
69 
70 	foreach (c; cont.containers) {
71 		with (c.configure) {
72 			property!ushort("http_port");
73 			property!(string[])("http_bind");
74 		}
75 	}
76 
77 	return cont;
78 }
79 
80 shared static this()
81 {
82 	import std.file;
83 	auto router = new URLRouter;
84 	router.routed;
85 
86 	auto c = config();
87 	
88 	auto settings = new HTTPServerSettings;
89 	settings.bindAddresses = c.locate!(string[])("http_bind");
90 	settings.port = c.locate!ushort("http_port");
91 	settings.errorPageHandler = toDelegate(&showError);
92 
93 	listenHTTP(settings, router);
94 }
95 
96 void main()
97 {
98 	import vibe.vibe;
99 	lowerPrivileges();
100 	runEventLoop();
101 }