1 /**
2 	Contains the application entry point.
3 
4 	Copyright: © 2012-2013 RejectedSoftware e.K.
5 	License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
6 	Authors: Sönke Ludwig
7 */
8 module app;
9 
10 import vibe.d;
11 import vibe.crypto.passwordhash;
12 
13 import vibedist.admin;
14 import vibedist.controller;
15 import vibedist.engine;
16 
17 void apiIndex(HTTPServerRequest req, HTTPServerResponse res)
18 {
19 	res.writeJsonBody(["message": "This is the VibeDist API."]);
20 }
21 
22 void registerNode(HTTPServerRequest req, HTTPServerResponse res)
23 {
24 	Config cfg;
25 	getConfig(cfg);
26 	auto jreq = req.json;
27 
28 	string hostname = jreq.hostName.get!string;
29 	ushort port = jreq.port.get!ushort;
30 	string local_address = req.peer;
31 	ushort local_port = jreq.localPort.get!ushort;
32 	string ssl_cert = jreq.sslCertFile.get!string;
33 	string ssl_key = jreq.sslKeyFile.get!string;
34 	int pid = jreq.pid.get!int;
35 
36 	SSLContext ssl_context; // TODO: add proper support
37 
38 	foreach( intf; g_interfaces ){
39 		auto s = intf.settings;
40 		if (s.hostName != hostname || s.port != port || s.sslContext !is ssl_context)
41 			continue;
42 
43 		foreach( n; intf.nodes ){
44 			if( n.address == local_address && n.port == local_port )
45 				return;
46 		}
47 
48 		intf.nodes ~= new Node(local_address, local_port, pid);
49 		return;
50 	}
51 
52 	auto settings = new HTTPServerSettings;
53 	settings.hostName = hostname;
54 	settings.port = port;
55 	settings.bindAddresses = cfg.publicInterfaces;
56 	settings.sslContext = ssl_context;
57 
58 	auto intf = new PublicInterface;
59 	intf.settings = settings;
60 	intf.nodes ~= new Node(local_address, local_port, pid);
61 
62 	listenHTTP(settings, intf);
63 	g_interfaces ~= intf;
64 
65 	res.writeJsonBody(["message": "Successfully registered."]);
66 }
67 
68 bool testPassword(string username, string password)
69 {
70 	if( username != "root" ) return false;
71 	Config cfg;
72 	getConfig(cfg);
73 	auto pwhash = cfg.rootPasswordHash;
74 	return testSimplePasswordHash(pwhash, password);
75 }
76 
77 static this()
78 {
79 	Config cfg;
80 	getConfig(cfg);
81 
82 	{ // setup node interface
83 		auto settings = new HTTPServerSettings;
84 		settings.bindAddresses = cfg.nodeInterfaces;
85 		settings.port = cfg.nodePort;
86 
87 		auto router = new URLRouter;
88 		router.get("/", &apiIndex);
89 		router.post("/register", &registerNode);
90 
91 		listenHTTP(settings, router);
92 	}
93 
94 	{ // setup admin interface
95 		auto settings = new HTTPServerSettings;
96 		settings.bindAddresses = [cfg.adminInterface];
97 		settings.port = cfg.adminPort;
98 		//settings.sslCertFile = "admin.crt";
99 		//settings.sslKeyFile = "admin.key";
100 
101 		auto router = new URLRouter;
102 		router.get("*", performBasicAuth("VibeDist Admin Interface", toDelegate(&testPassword)));
103 		router.get("/", &showAdminHome);
104 		router.post("/restart_node", &reloadNode);
105 		router.post("/start_node", &startNode);
106 
107 		listenHTTP(settings, router);
108 	}
109 }