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
|
const Backend = require('./api/structures/Server');
const express = require('express');
const compression = require('compression');
const ream = require('ream');
const config = require('../config');
const path = require('path');
const log = require('./api/utils/Log');
const dev = process.env.NODE_ENV !== 'production';
const oneliner = require('one-liner');
const jetpack = require('fs-jetpack');
function startProduction() {
startAPI();
startSite();
}
function startAPI() {
new Backend().start();
}
function startSite() {
/*
Make sure the frontend has enough data to prepare the service
*/
writeFrontendConfig();
/*
Starting ream's custom server
*/
const server = express();
const app = ream({
dev,
entry: path.join(__dirname, 'site', 'index.js')
});
app.getRequestHandler().then(handler => {
server.use(compression());
/*
This option is mostly for development, since serving the files with nginx is better.
*/
if (config.serveFilesWithNode) {
server.use('/', express.static(`./${config.uploads.uploadFolder}`));
}
server.get('*', handler);
server.listen(config.server.ports.frontend, error => {
if (error) log.error(error);
});
});
app.on('renderer-ready', () => log.info(`> Frontend ready and listening on port ${config.server.ports.frontend}`));
}
function writeFrontendConfig() {
/*
Since ream can't execute getInitialData on non-routes we write a config file for it.
*/
const template = oneliner`
module.exports = {
version: '${process.env.npm_package_version}',
URL: '${config.filesServeLocation}',
baseURL: '${config.backendLocation}',
serviceName: '${config.serviceName}',
maxFileSize: '${config.uploads.uploadMaxSize}',
chunkSize: '${config.uploads.chunkSize}',
maxLinksPerAlbum: '${config.albums.maxLinksPerAlbum}'
}`;
jetpack.write(path.join(__dirname, 'site', 'config.js'), template);
log.success('Frontend config file generated successfully');
}
/*
Having multiple files for different scripts was mendokusai.
*/
const args = process.argv[2];
if (!args) startProduction();
else if (args === 'api') startAPI();
else if (args === 'site') startSite();
else process.exit(0);
|