-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
172 lines (137 loc) · 5.47 KB
/
server.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
#!/usr/bin/env node
// Copyright 2013 Eric W. Barndollar.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
var child_process = require('child_process');
var commander = require('commander');
var express = require('express');
var nodeWatch = require('node-watch');
var path = require('path');
var projectConfig = require('./project-config.js');
var soy = require('./lib/soy.js');
var underscore = require('underscore');
var EXIT_SUCCESS = 0;
var app = express();
//==============================================================================
// Command-Line Options
//==============================================================================
commander
.option('--debug', 'Run server to server application built in --debug mode')
.option('--port <port>', 'Port to run HTTP server on', Number, 8080)
.option('--watch', 'Watch for changes under this dir and rebuild on demand')
.parse(process.argv);
//==============================================================================
// Configure Soy Template Rendering
//==============================================================================
// Configure all server-side rendered Soy templates here.
var outDir = path.join(__dirname, projectConfig.OPTIONS.outputDir,
commander.debug ? 'debug/' : 'release/');
var soyRenderer = new soy.SoyRenderer(outDir, {
'cbxrs.ui.page.soy.main': 'page.js'
});
app.disable('view cache'); // SoyRenderer does own caching.
app.set('views', outDir);
app.engine('.js', underscore.bind(soyRenderer.render, soyRenderer));
//==============================================================================
// Watch for File Changes & Trigger Rebuilds
//==============================================================================
function rebuild(callbackFn) {
console.log('Rebuilding project, changes detected since last build...\n');
soyRenderer.invalideCachedTemplates();
var args = [path.join(__dirname, 'build.js')];
if (commander.debug) {
args.push('--debug');
}
var projectBuilder = child_process.spawn('node', args, {stdio: 'inherit'});
projectBuilder.on('close', function(exitCode) {
if (exitCode != EXIT_SUCCESS) {
callbackFn(new Error('Rebuilding project failed'));
} else {
console.log('\nRebuilding project succeeded...');
callbackFn(null);
}
});
}
function normalizePath(path) {
return path.replace(/\\/g, '/');
}
// Ignore changes to files under output directories to avoid change cycles.
var IGNORE_CHANGES = new RegExp(
'(' + normalizePath(projectConfig.OPTIONS.generatedCodeDir) + '|' +
normalizePath(projectConfig.OPTIONS.tempFileDir) + '|' +
normalizePath(projectConfig.OPTIONS.outputDir) + ')');
if (commander.watch) {
var needsRebuild = false;
var rebuildInProgress = false;
var lastRebuildFailed = false;
nodeWatch(__dirname, {persistent: false}, function(fileName) {
if (IGNORE_CHANGES.test(normalizePath(fileName))) {
return;
}
// Some file under this directory changed.
needsRebuild = true;
});
// Install this middleware to check if rebuild is needed before each request.
var pendingContinueFns = [];
app.use(function(req, res, continueFn) {
if (!needsRebuild) {
if (lastRebuildFailed) {
throw new Error('Last project rebuild failed. Fix errors & try again.');
}
continueFn(null);
return;
}
pendingContinueFns.push(continueFn);
if (!rebuildInProgress) {
rebuildInProgress = true;
rebuild(function(err) {
rebuildInProgress = false;
needsRebuild = false;
lastRebuildFailed = !!err;
pendingContinueFns.forEach(function(fn) { fn(err); });
pendingContinueFns = [];
});
}
});
console.log('Watching for file changes under this dir...');
} else {
console.log('Not watching for file changes...');
}
//==============================================================================
// Configure HTTP Request Path Handlers
//==============================================================================
// Main page:
app.get('/', function(req, res) {
res.render('page.js', {templateName: 'cbxrs.ui.page.soy.main'});
});
// Static JS and CSS:
// (Note that a more complex project may want to copy these files into a
// dedicated directory for express.static() usage).
app.get('/main.js', function(req, res) {
res.sendfile(path.join(outDir, 'main.js'));
});
app.get('/style.css', function(req, res) {
res.sendfile(path.join(outDir, 'style.css'));
});
// Error page:
app.use(function(err, req, res, next) {
console.error('Request had failures:\n' + err);
res.send(500,
'Sorry :( something went wrong. Tell whoever runs this site to check ' +
'their server logs (and to install a prettier error page).');
});
//==============================================================================
// Start Accepting Requests
//==============================================================================
console.log('Listening for HTTP requests on port ' + commander.port);
app.listen(commander.port);