1 | import * as fs from 'fs';
|
2 | import * as akala from '@akala/core';
|
3 | import { join as pathJoin } from 'path';
|
4 | import { router, Request, Response } from './router';
|
5 | import * as pac from './package';
|
6 | var log = akala.log('akala:master');
|
7 | import { microservice } from './microservice';
|
8 | import { updateConfig, getConfig } from './config';
|
9 | import { serveStatic } from './master-meta';
|
10 |
|
11 | var httpPackage: 'http' | 'https' = 'http';
|
12 |
|
13 | var port = process.env.PORT || '5678';
|
14 |
|
15 | akala.register('$updateConfig', new Proxy(updateConfig, {
|
16 | get: function (uc, key: string)
|
17 | {
|
18 | return function (config, subKey)
|
19 | {
|
20 | return uc(config, key + '.' + subKey);
|
21 | }
|
22 | }
|
23 | }));
|
24 | akala.registerFactory('$config', new Proxy(getConfig, {
|
25 | get: function (c, key: string)
|
26 | {
|
27 | return function ()
|
28 | {
|
29 | return c().then(function (config) { return config[key]; });
|
30 | }
|
31 | }
|
32 | }));
|
33 |
|
34 | var lateBoundRoutes = router();
|
35 | var preAuthenticatedRouter = router();
|
36 | var authenticationRouter = router();
|
37 | var app = router();
|
38 | akala.register('$preAuthenticationRouter', preAuthenticatedRouter);
|
39 | akala.register('$authenticationRouter', authenticationRouter);
|
40 | akala.register('$router', lateBoundRoutes);
|
41 | var masterRouter = router();
|
42 | masterRouter.use(preAuthenticatedRouter.router);
|
43 | masterRouter.use(authenticationRouter.router);
|
44 | masterRouter.use(lateBoundRoutes.router);
|
45 | masterRouter.use(app.router);
|
46 |
|
47 | var root: string;
|
48 | var index: string;
|
49 | var privateKey: string;
|
50 | var fullchain: string;
|
51 |
|
52 | var configFile = fs.realpathSync('./config.json');
|
53 | fs.exists(configFile, function (exists)
|
54 | {
|
55 | var config = exists && require(configFile) || {};
|
56 |
|
57 | root = config && config['@akala/server'] && config['@akala/server'].root;
|
58 | index = config && config['@akala/server'] && config['@akala/server'].index;
|
59 | port = config && config['@akala/server'] && config['@akala/server'].port || port;
|
60 | privateKey = config && config['@akala/server'] && config['@akala/server'].privateKey || 'privkey.pem';
|
61 | fullchain = config && config['@akala/server'] && config['@akala/server'].fullchain || 'fullchain.pem';
|
62 | if (fs.existsSync(privateKey) && fs.existsSync(fullchain))
|
63 | httpPackage = 'https';
|
64 |
|
65 |
|
66 | var dn = config && config['@akala/server'] && config['@akala/server'].dn || 'localhost';
|
67 |
|
68 | akala.register('$rootUrl', httpPackage + '://' + dn + ':' + port);
|
69 |
|
70 | var sourcesFile = './sources.list';
|
71 | fs.readFile(sourcesFile, 'utf8', function (error, sourcesFileContent)
|
72 | {
|
73 | var sources: string[] = [];
|
74 | var modules: string[] = [];
|
75 | if (error && error.code == 'ENOENT')
|
76 | {
|
77 | var pkg: pac.CoreProperties = require(pathJoin(process.cwd(), './package.json'))
|
78 | var [source, folder] = pkg.name.split('/');
|
79 | microservice(pkg.name, source, [source], config);
|
80 | modules.push(pkg.name)
|
81 | }
|
82 | else
|
83 | {
|
84 | sources = JSON.parse(sourcesFileContent);
|
85 | }
|
86 | akala.eachAsync(sources, function (source, i, next)
|
87 | {
|
88 | fs.readdir('node_modules/' + source, function (err, dirModules)
|
89 | {
|
90 | if (err)
|
91 | {
|
92 | console.error(err);
|
93 | return;
|
94 | }
|
95 |
|
96 | dirModules.forEach(function (folder)
|
97 | {
|
98 | microservice(source + '/' + folder, source, [source], config);
|
99 | modules.push(source + '/' + folder);
|
100 | });
|
101 |
|
102 | next();
|
103 | });
|
104 | }, function (error?)
|
105 | {
|
106 | if (error)
|
107 | {
|
108 | console.error(error);
|
109 | return;
|
110 | }
|
111 |
|
112 | akala.register('$$modules', modules);
|
113 |
|
114 | log(modules);
|
115 |
|
116 | akala.module('bootstrap', ...modules).activate([], function ()
|
117 | {
|
118 | log('registering error handler');
|
119 |
|
120 | var serveRoot = serveStatic(root, { index: index || undefined })
|
121 | preAuthenticatedRouter.use(serveStatic(root, { index: index || undefined, fallthrough: true }));
|
122 | preAuthenticatedRouter.get('/favicon.ico', serveStatic(root, { index: index || undefined, fallthrough: false }));
|
123 | preAuthenticatedRouter.get('/manifest.json', serveStatic(root, { index: index || undefined, fallthrough: false }));
|
124 | app.get('/@akala/client', function (_req: Request, res: Response)
|
125 | {
|
126 | res.json(modules.map(m => { return { name: m, dep: akala.module(m).dep } }))
|
127 | });
|
128 | app.get('*', function (request, response)
|
129 | {
|
130 | if (request.url.endsWith('.map'))
|
131 | {
|
132 | response.sendStatus(404);
|
133 | }
|
134 | else
|
135 | serveRoot(request, response, function ()
|
136 | {
|
137 | fs.createReadStream(root + '/index.html').pipe(response);
|
138 | });
|
139 | });
|
140 |
|
141 | masterRouter.use(function (err, req: Request, res: Response, next)
|
142 | {
|
143 | try
|
144 | {
|
145 | if (err)
|
146 | {
|
147 | console.error('error occurred on ' + req.url);
|
148 |
|
149 | console.error(err.stack);
|
150 | res.statusCode = 500;
|
151 | res.write(JSON.stringify(err));
|
152 | res.end();
|
153 | }
|
154 | else
|
155 | res.sendStatus(404);
|
156 | }
|
157 | catch (e)
|
158 | {
|
159 | console.error(e.stack)
|
160 | res.statusCode = 500;
|
161 | res.end();
|
162 | }
|
163 | });
|
164 |
|
165 | });
|
166 |
|
167 | akala.module('bootstrap').activate(['$rootUrl'], function (url)
|
168 | {
|
169 | console.log('server ready and listening on ' + url + '...');
|
170 | })
|
171 |
|
172 | akala.module('bootstrap').start();
|
173 | });
|
174 | });
|
175 |
|
176 | switch (httpPackage)
|
177 | {
|
178 | case 'http':
|
179 | const http = require('http');
|
180 | var server = http.createServer();
|
181 | break;
|
182 | case 'https':
|
183 |
|
184 |
|
185 | const https = require(httpPackage);
|
186 | var server = https.createServer({ key: fs.readFileSync(privateKey), cert: fs.readFileSync(fullchain) });
|
187 | break;
|
188 | }
|
189 | server.listen(port, dn);
|
190 | akala.register('$server', server);
|
191 | masterRouter.attachTo(server);
|
192 | });
|
193 |
|