forked from Sunev/jinrou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
177 lines (162 loc) · 4.41 KB
/
app.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
const http = require('http');
const https = require('https');
const path = require('path');
const cpx = require('cpx');
const ss = require('socketstream');
ss.client.define('main', {
view: 'app.jade',
css: ['libs', 'app.styl'],
code: ['app', 'libs', 'pages', 'shared'],
tmpl: '*',
});
ss.http.router.on('/', function(req, res) {
res.serveClient('main');
});
ss.client.formatters.add(require('ss-latest-coffee'));
// Load config file.
// pull時にはコンフィグファイルないので・・・
try {
global.Config = require('./config/app.coffee');
} catch (e) {
console.error('Failed to load config file.');
console.error(
'Copy config.default/app.coffee to config/app.coffee, edit app.coffee, and retry.',
);
console.error(e.trace || e);
process.exit(1);
}
/**
* Load manifest file from builclient-side assets.
*/
let manifestMain;
let legacyManifestMain;
let manifestFeatureCheck;
try {
const manifest = require('./client/static/front-assets/manifest.json');
manifestMain = manifest['main.js'];
manifestFeatureCheck = manifest['feature-check.js'];
if (Config.front.legacyBuilds) {
const legacyManifest = require('./client/static/front-assets/legacy/manifest.json');
legacyManifestMain = legacyManifest['main.js'];
}
} catch (e) {
// Failed to load the manifest file.
console.error(`Error: failed to load the manifest file.
It is not likely that front-end assets are not built.
See build instructions in README for details.`);
throw e;
}
// Validate manifestMain.
if ('string' !== typeof manifestMain) {
throw new Error('Manifest data is somehow invalid');
}
const libi18n = require('./server/libs/i18n.coffee');
const i18n = libi18n.getWithDefaultNS('view');
/**
* Config object for jade formatter.
*/
const jadeConfig = {
locals: {
Config,
bundle: manifestMain,
legacyBundle: legacyManifestMain,
featureCheckJs: manifestFeatureCheck,
notSupportedPage: Config.front.notSupportedPage,
legacyBuilds: Config.front.legacyBuilds,
i18n,
},
};
ss.client.formatters.add(require('ss-jade'), jadeConfig);
ss.client.formatters.add(require('ss-stylus'));
ss.client.templateEngine.use(require('ss-clientjade'));
ss.client.set({ liveReload: false });
ss.session.store.use('redis', { host: Config.redis.host });
ss.publish.transport.use('redis', { host: Config.redis.host });
/**
* Whether this run is in production mode.
*/
const isProduction = ss.env === 'production';
if (isProduction) {
ss.client.packAssets();
} else {
// development
ss.http.set({
static: {
cacheControlHeader: 'no-cache',
},
});
}
//---- Middleware
const middleware = require('./server/middleware.coffee');
ss.http.middleware.prepend(middleware.jsonapi);
ss.http.middleware.prepend(middleware.manualxhr);
ss.http.middleware.prepend(middleware.images);
ss.http.middleware.prepend(middleware.twitterbot);
//リッスン先設定
ss.ws.transport.use('engineio', {
client: Config.ws.connect,
});
//---- init HTTP server
let server;
if (Config.http.secure != null) {
server = https.createServer(Config.http.secure, ss.http.middleware);
} else {
server = http.createServer(ss.http.middleware);
}
//---- prepare client-side assets
{
// options for cpx.
const copySource = path.join(__dirname, 'front/dist/**/*');
const copyDest = path.join(__dirname, 'client/static/front-assets/');
const copyOptions = {
preserve: true,
update: true,
};
/*
if (isProduction){
// Copy once and run.
cpx.copy(
copySource,
copyDest,
copyOptions,
(err)=>{
if (err != null){
console.error(err);
process.exit(1);
return;
}
runService(server);
},
);
} else {
// Watch changes to copySource.
cpx.watch(
copySource,
copyDest,
copyOptions,
)
.on('watch-ready', ()=>{
// Initial copy is done.
runService(server);
})
.on('watch-error', (err)=>{
if (err != null){
console.error(err);
}
});
}
*/
runService(server);
}
/**
* Function to start the service.
*/
function runService(server) {
// Init connection to DB
const db = require('./server/db.coffee');
db.dbinit(() => {
// Start application
server.listen(Config.http.port);
ss.start(server);
});
}