1 /**
2 	REST interface implementation.
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 vibedist.rest;
9 
10 import vibedist.controller;
11 import vibedist.engine;
12 
13 import vibe.web.rest;
14 import vibe.http.server;
15 import vibe.stream.tls;
16 
17 
18 interface VibeDistNodeAPI {
19 	@path("/") string getInfo();
20 	void register(string host_name, ushort port, string local_address, ushort local_port, string ssl_settings, int pid);
21 }
22 
23 class VibeDistNodeAPIImpl : VibeDistNodeAPI
24 {
25 	private {
26 		VibeDistController m_ctrl;
27 	}
28 	
29 	this(VibeDistController ctrl)
30 	{
31 		m_ctrl = ctrl;
32 	}
33 
34 	@path("/") string getInfo() { return "This is the VibeDist API."; }
35 
36 	void register(string host_name, ushort port, string local_address, ushort local_port, string ssl_settings, int pid)
37 	{
38 		Config cfg;
39 		m_ctrl.getConfig(cfg);
40 
41 		TLSContext ssl_context; // TODO: add proper support
42 
43 		foreach (intf; g_interfaces) {
44 			auto s = intf.settings;
45 			if (s.hostName != host_name || s.port != port || s.tlsContext !is ssl_context)
46 				continue;
47 
48 			foreach (n; intf.nodes) {
49 				if (n.address == local_address && n.port == local_port)
50 					return;
51 			}
52 
53 			intf.nodes ~= new Node(local_address, local_port, pid);
54 			return;
55 		}
56 
57 		auto settings = new HTTPServerSettings;
58 		settings.hostName = host_name;
59 		settings.port = port;
60 		settings.bindAddresses = cfg.publicInterfaces;
61 		settings.tlsContext = ssl_context;
62 
63 		auto intf = new PublicInterface;
64 		intf.settings = settings;
65 		intf.nodes ~= new Node(local_address, local_port, pid);
66 
67 		listenHTTP(settings, intf);
68 		g_interfaces ~= intf;
69 	}
70 }