1 /**
2 	Copyright: © 2013 rejectedsoftware e.K.
3 	License: Subject to the terms of the GNU GPLv3 license, as written in the included LICENSE.txt file.
4 	Authors: Sönke Ludwig
5 */
6 module app;
7 
8 import dubregistry.dbcontroller;
9 import dubregistry.mirror;
10 import dubregistry.repositories.bitbucket;
11 import dubregistry.repositories.github;
12 import dubregistry.repositories.gitlab;
13 import dubregistry.registry;
14 import dubregistry.web;
15 import dubregistry.api;
16 
17 import std.algorithm : sort;
18 import std.file;
19 import std.path;
20 import userman.web;
21 import vibe.d;
22 
23 
24 Task s_checkTask;
25 DubRegistry s_registry;
26 DubRegistryWebFrontend s_web;
27 string s_mirror;
28 
29 void startMonitoring()
30 {
31 	void monitorNewVersions()
32 	{
33 		sleep(10.seconds()); // give the cache a chance to warm up first
34 		while(true){
35 			if (s_mirror.length) s_registry.mirrorRegistry(URL(s_mirror));
36 			else s_registry.checkForNewVersions();
37 			sleep(30.minutes());
38 		}
39 	}
40 	s_checkTask = runTask(&monitorNewVersions);
41 }
42 
43 version (linux) private immutable string certPath;
44 
45 shared static this()
46 {
47 	setLogFile("log.txt", LogLevel.diagnostic);
48 
49 	string hostname = "code.dlang.org";
50 
51 	readOption("mirror", &s_mirror, "URL of a package registry that this instance should mirror (WARNING: will overwrite local database!)");
52 	readOption("hostname", &hostname, "Domain name of this instance (default: code.dlang.org)");
53 
54 	// validate provided mirror URL
55 	if (s_mirror.length)
56 		validateMirrorURL(s_mirror);
57 
58 	version (linux) {
59 		logInfo("Enforcing certificate trust.");
60 		enum debianCA = "/etc/ssl/certs/ca-certificates.crt";
61 		enum redhatCA = "/etc/pki/tls/certs/ca-bundle.crt";
62 		certPath = redhatCA.exists ? redhatCA : debianCA;
63 
64 		HTTPClient.setTLSSetupCallback((ctx) {
65 			ctx.useTrustedCertificateFile(certPath);
66 			ctx.peerValidationMode = TLSPeerValidationMode.trustedCert;
67 		});
68 	}
69 
70 	import dub.internal.utils : jsonFromFile;
71 	auto regsettingsjson = jsonFromFile(Path("settings.json"), true);
72 	auto ghuser = regsettingsjson["github-user"].opt!string;
73 	auto ghpassword = regsettingsjson["github-password"].opt!string;
74 	auto glurl = regsettingsjson["gitlab-url"].opt!string;
75 	auto glauth = regsettingsjson["gitlab-auth"].opt!string;
76 
77 	GithubRepository.register(ghuser, ghpassword);
78 	BitbucketRepository.register();
79 	if (glurl.length) GitLabRepository.register(glauth, glurl);
80 
81 	auto router = new URLRouter;
82 	if (s_mirror.length) router.any("*", (req, res) { req.params["mirror"] = s_mirror; });
83 	router.get("*", (req, res) @trusted { if (!s_checkTask.running) startMonitoring(); });
84 
85 	// VPM registry
86 	auto regsettings = new DubRegistrySettings;
87 	s_registry = new DubRegistry(regsettings);
88 
89 	UserManController userdb;
90 
91 	if (!s_mirror.length) {
92 		// user management
93 		auto udbsettings = new UserManSettings;
94 		udbsettings.serviceName = "DUB - The D package registry";
95 		udbsettings.serviceUrl = URL("http://code.dlang.org/");
96 		udbsettings.serviceEmail = "noreply@vibed.org";
97 		udbsettings.databaseURL = "mongodb://127.0.0.1:27017/vpmreg";
98 		udbsettings.requireAccountValidation = false;
99 		userdb = createUserManController(udbsettings);
100 	}
101 
102 	// web front end
103 	s_web = router.registerDubRegistryWebFrontend(s_registry, userdb);
104 	router.registerDubRegistryAPI(s_registry);
105 
106 	// start the web server
107  	auto settings = new HTTPServerSettings;
108 	settings.hostName = hostname;
109 	settings.bindAddresses = ["127.0.0.1"];
110 	settings.port = 8005;
111 	settings.sessionStore = new MemorySessionStore;
112 
113 	listenHTTP(settings, router);
114 
115 	// poll github for new project versions
116 	startMonitoring();
117 }