/
index.js
228 lines (202 loc) · 5.98 KB
/
index.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//------------------------------------------------------------------------------
// node.js starter application for hosting
//------------------------------------------------------------------------------
import cors from 'cors';
import express from 'express';
import bodyParser from 'body-parser';
import router from './server/router';
import cookieParser from 'cookie-parser';
import cookieSession from 'cookie-session';
import proxy from 'http-proxy-middleware';
import morgan from 'morgan';
import apiConfig from 'appdir/api/config';
import path from 'path';
import fs from 'fs';
import op from 'object-path';
import _ from 'underscore';
import staticGzip from 'express-static-gzip';
import moment from 'moment';
import SDK from 'reactium-core/sdk';
const { Enums } = SDK;
global.defines = {};
global.rootPath = path.resolve(__dirname, '..');
global.parseAppId = apiConfig.parseAppId;
global.restAPI = apiConfig.restAPI;
global.isSSR = 'SSR_MODE' in process.env && process.env.SSR_MODE === 'on';
// SSR Defines
if (!'defines' in global) {
global.defines = {};
}
if (fs.existsSync(`${rootPath}/src/app/server/defines.js`)) {
const defs = require(`${rootPath}/src/app/server/defines.js`);
Object.keys(defs).forEach(key => {
if (key !== 'process.env') {
global.defines[key] = defs[key];
}
});
}
const app = express();
let gulpConfig;
try {
gulpConfig = require('./gulp.config');
} catch (err) {
gulpConfig = { port: { proxy: 3030 } };
}
let node_env = process.env.hasOwnProperty('NODE_ENV')
? process.env.NODE_ENV
: 'development';
// PORT setup:
let port = gulpConfig.port.proxy;
let pvar = op.get(process.env, 'PORT_VAR', false);
if (pvar) {
port = op.get(process.env, pvar, port);
} else {
port = op.get(process.env, 'APP_PORT', port);
port = op.get(process.env, 'PORT', port);
}
port = Number(port);
const adminURL = process.env.ACTINIUM_ADMIN_URL || false;
// set app variables
app.set('x-powered-by', false);
// express middlewares
let middlewares = [
process.env.DEBUG === 'on'
? {
name: 'logging',
use: morgan('combined'),
order: Enums.priority.highest,
}
: false,
{
name: 'cors',
use: cors(),
order: Enums.priority.highest,
},
{
name: 'placeholder',
use: require('./server/middleware/placeholder'),
order: Enums.priority.highest,
},
{
name: 'api',
use: proxy('/api', {
target: restAPI,
changeOrigin: true,
pathRewrite: {
'^/api': '',
},
logLevel: process.env.DEBUG === 'on' ? 'debug' : 'error',
ws: true,
}),
order: Enums.priority.highest,
},
// parsers
{
name: 'jsonParser',
use: bodyParser.json(),
order: Enums.priority.high,
},
{
name: 'urlEncoded',
use: bodyParser.urlencoded({ extended: true }),
order: Enums.priority.high,
},
// cookies
{
name: 'cookieParser',
use: cookieParser(),
order: Enums.priority.high,
},
{
name: 'cookieSession',
use: cookieSession({
name: op.get(process.env, 'COOKIE_SESSION_NAME', 'aljtka4'),
keys: JSON.parse(
op.get(
process.env,
'COOKIE_SESSION_KEYS',
JSON.stringify([
'Q2FtZXJvbiBSdWxlcw',
'vT3GtyZKbnoNSdWxlcw',
]),
),
),
order: Enums.priority.high,
}),
},
];
// development mode
if (process.env.NODE_ENV === 'development') {
const webpack = require('webpack');
const gulpConfig = require('./gulp.config');
const webpackConfig = require('./webpack.config')(gulpConfig);
const wpMiddlware = require('webpack-dev-middleware');
const wpHotMiddlware = require('webpack-hot-middleware');
const publicPath = `http://localhost:${port}/`;
// local development overrides for webpack config
webpackConfig.entry.main = [
`webpack-hot-middleware/client?path=/__webpack_hmr&quiet=true`,
webpackConfig.entry.main,
];
webpackConfig.plugins.push(new webpack.HotModuleReplacementPlugin());
webpackConfig.output.publicPath = publicPath;
const compiler = webpack(webpackConfig);
middlewares.push({
name: 'webpack',
use: wpMiddlware(compiler, {
serverSideRender: true,
path: '/',
publicPath,
}),
order: Enums.priority.high,
});
middlewares.push({
name: 'hmr',
use: wpHotMiddlware(compiler, {
reload: true,
}),
order: Enums.priority.high,
});
}
// serve the static files out of ./public or specified directory
const staticAssets =
process.env.PUBLIC_DIRECTORY || path.resolve(process.cwd(), 'public');
middlewares.push({
name: 'static',
use: staticGzip(staticAssets),
order: Enums.priority.neutral,
});
// default route handler
middlewares.push({
name: 'router',
use: router,
order: Enums.priority.neutral,
});
// Give app an opportunity to change middlewares
if (fs.existsSync(`${rootPath}/src/app/server/middleware.js`)) {
middlewares = require(`${rootPath}/src/app/server/middleware.js`)(
middlewares,
);
}
_.sortBy(_.compact(middlewares), 'order').forEach(({ use }) => {
if (Array.isArray(use)) {
app.use(...use);
} else {
app.use(use);
}
});
// start server on the specified port and binding host
app.listen(port, '0.0.0.0', function() {
console.log(
`[${moment().format(
'HH:mm:ss',
)}] Reactium Server running on port '${port}'...`,
);
});
// Provide opportunity for ssl server
if (fs.existsSync(`${rootPath}/src/app/server/ssl.js`)) {
require(`${rootPath}/src/app/server/ssl.js`)(app);
}
if (isSSR) {
app.dependencies = global.dependencies = require('dependencies').default;
}