1 /**
2 	Provides abstract database access.
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.controller;
9 
10 import vibe.db.mongo.mongo;
11 import vibe.crypto.passwordhash;
12 
13 import vibedist.admin;
14 
15 struct Config {
16 	BsonObjectID _id;
17 	string name;
18 
19 	string adminInterface;
20 	ushort adminPort;
21 
22 	string[] nodeInterfaces;
23 	ushort nodePort;
24 
25 	string[] publicInterfaces;
26 
27 	string rootPasswordHash;
28 }
29 
30 class VibeDistController {
31 	private {
32 		MongoCollection configs;
33 	}
34 
35 	this()
36 	{
37 		auto db = connectMongoDB("localhost").getDatabase("vibedist");
38 		configs = db["configs"];
39 	}
40 
41 	void getConfig(ref Config cfg, string name = "default")
42 	{
43 		// load or create default config
44 		auto bcfg = configs.findOne(["name": name]);
45 		if( bcfg.isNull() ){
46 			cfg._id = BsonObjectID.generate();
47 			cfg.name = name;
48 			cfg.adminInterface = "127.0.0.1";
49 			cfg.adminPort = 8080;
50 			cfg.nodeInterfaces = ["127.0.0.1"];
51 			cfg.publicInterfaces = ["0.0.0.0"];
52 			cfg.nodePort = 11000;
53 			cfg.rootPasswordHash = generateSimplePasswordHash("admin");
54 			configs.insert(cfg);
55 		} else deserializeBson(cfg, bcfg);
56 	}
57 }