-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.js
92 lines (77 loc) · 2.66 KB
/
run.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
var fs = require('fs-extra');
var path = require('path');
var fastify = require('fastify');
const fastifyStatic = require('fastify-static');
var logger = require('./site/logger');
var server = fastify();
var config = require('./config');
fs.ensureDirSync(path.resolve(__dirname, '.heml'));
fs.ensureDirSync(path.resolve(__dirname, '.static'));
fs.ensureDirSync(path.resolve(__dirname, '.templates'));
var apps = fs.readdirSync(path.resolve(__dirname, 'apps'));
for (var i = 0; i < apps.length; ++i) {
// register models
if (fs.existsSync(path.resolve(__dirname, 'apps', apps[i], 'models'))) {
require(`./apps/${apps[i]}/models`);
}
// copy heml
if (fs.existsSync(path.resolve(__dirname, 'apps', apps[i], 'heml'))) {
fs.copySync(
path.resolve(__dirname, 'apps', apps[i], 'heml'),
path.resolve(__dirname, '.heml', apps[i])
);
}
// copy static
if (fs.existsSync(path.resolve(__dirname, 'apps', apps[i], 'static'))) {
fs.copySync(
path.resolve(__dirname, 'apps', apps[i], 'static'),
path.resolve(__dirname, '.static', apps[i])
);
}
// copy templates
if (fs.existsSync(path.resolve(__dirname, 'apps', apps[i], 'templates'))) {
fs.copySync(
path.resolve(__dirname, 'apps', apps[i], 'templates'),
path.resolve(__dirname, '.templates', apps[i])
);
}
// load routes
if (fs.existsSync(path.resolve(__dirname, 'apps', apps[i], 'routes'))) {
var routes = require(`./apps/${apps[i]}/routes`);
server.register(routes, { prefix: `/${apps[i]}` });
}
// run the app's index.js file
var appIndex = require(`./apps/${apps[i]}`);
}
// SITE static
if (fs.existsSync(path.resolve(__dirname, 'site', 'static'))) {
fs.copySync(
path.resolve(__dirname, 'site', 'static'),
path.resolve(__dirname, '.static', 'SITE')
);
}
// SITE templates
if (fs.existsSync(path.resolve(__dirname, 'site', 'templates'))) {
fs.copySync(
path.resolve(__dirname, 'site', 'templates'),
path.resolve(__dirname, '.templates', 'SITE')
);
}
// serve static files
server.register(fastifyStatic, {
root: path.join(__dirname, '.static'),
prefix: '/static/'
});
// root route
server.get('/', require('./site/home'));
server.addHook('preHandler', function(request, reply, next) {
logger.SITE.log('verbose', `${request.req.method} ${request.req.url} from ${request.req.socket.remoteFamily} ${request.req.socket.remoteAddress}:${request.req.socket.remotePort}.`);
next();
});
var runConfig = process.argv[2];
if (!runConfig) runConfig = 'default';
var port = config.SITE.runConfig[runConfig].port;
server.listen(port, function(err) {
if (err) throw err;
logger.SITE.log('info', `Server listening on port ${port}.`);
});