forked from mdn/kumascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
214 lines (184 loc) · 7.07 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
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
// ## KumaScript HTTP service
//
// Provides the HTTP service for document processing
/*jshint node: true, expr: false, boss: true */
// ### Prerequisites
var util = require('util'),
crypto = require('crypto'),
_ = require('underscore'),
request = require('request'),
express = require('express'),
log = require('winston'),
firelogger = require('firelogger'),
ks_loaders = require(__dirname + '/loaders'),
ks_caching = require(__dirname + '/caching'),
ks_macros = require(__dirname + '/macros'),
ks_templates = require(__dirname + '/templates'),
ks_api = require(__dirname + '/api'),
ks_utils = require(__dirname + '/utils');
// ### Server
//
// KumaScript service instance.
var Server = ks_utils.Class({
// Service accepts these options:
default_options: {
// Port on which the HTTP service will listen.
port: 9000,
// Template used to resolve incoming URL paths to document source URLs.
document_url_template: "http://localhost:9001/docs/{path}?raw=1",
// Template used to resolve macro names to URLs for the loader.
template_url_template: "http://localhost:9001/templates/{name}?raw=1",
// kumascript.templates class used to render templates
template_class: 'EJSTemplate',
// kumascript.loaders class used to load templates
template_loader_class: 'HTTPLoader',
// Prefix used in headers intended for env variables
env_header_prefix: 'x-kumascript-env-'
},
// Build the service, but do not listen yet.
initialize: function (options) {
var $this = this,
app = this.app = express.createServer();
log.info('Service pid ' + process.pid + ' initializing');
// Create a response cache instance
$this.cache = new ks_caching.ResponseCache();
// Configure the HTTP server...
app.configure(function () {
// Configure a logger that pipes to the winston logger.
app.use(express.logger({
format: ':remote-addr - - [:date] ":method :url ' +
'HTTP/:http-version" :status :res[content-length] ' +
'":referrer" ":user-agent" :response-time',
stream: {
write: function(s) {
log.info(s.trim(), { source: "server" });
}
}
}));
app.use(firelogger());
});
// Set up HTTP routing, pretty simple so far...
app.get('/docs/*', _.bind(this.docs_GET, this));
app.post('/docs/', _.bind(this.docs_POST, this));
// Get a class for rendering templates
var template_name = this.options.template_class,
template_class = ks_templates[template_name] ||
ks_templates.EJSTemplate;
// Get a class for loading templates
var loader_name = this.options.template_loader_class,
template_loader_class = ks_loaders[loader_name] ||
ks_loaders.HTTPLoader;
// Instantiate the macro processor
this.macro_processor = new ks_macros.MacroProcessor({
loader_class: template_loader_class,
loader_options: {
url_template: this.options.template_url_template,
template_class: template_class
}
});
},
// Start the service listening
listen: function () {
log.info('Starting up service on port ' + this.options.port);
this.app.listen(this.options.port);
},
// Close down the service
close: function () {
this.app.close();
},
// #### GET /docs/*
//
// Process source documents, respond with the result of macro evaluation.
docs_GET: function (req, res) {
var $this = this,
opts = {};
// Vary caching on values of env vars, as well as X-FireLogger
var pfx = this.options.env_header_prefix;
var vary = _.chain(req.headers).keys().filter(function (key) {
return 0 === key.indexOf(pfx);
}).value();
vary.push('X-FireLogger');
res.header('Vary', vary.join(','));
this.cache.cacheResponse(req, res, opts, function (req, res) {
var path = req.params[0],
url_tmpl = $this.options.document_url_template,
doc_url = ks_utils.tmpl(url_tmpl, {path: path});
request(doc_url, function (err, doc_resp, src) {
$this._evalMacros(src, req, res);
});
});
},
// #### POST /docs/
//
// Process POST body, respond with result of macro evaluation
docs_POST: function (req, res) {
var $this = this,
buf = '';
// TODO: Be more flexible with encodings.
req.setEncoding('utf8');
req.on('data', function (chunk) { buf += chunk; });
req.on('end', function () {
try {
var src = buf.length ? buf : '';
$this._evalMacros(src, req, res);
} catch (err){
// TODO: Handle errors more gracefully
$this._evalMacros('', req, res);
}
});
},
// #### _evalMacros()
//
// Shared macro evaluator used by GET and POST
_evalMacros: function (src, req, res) {
var $this = this;
// Extract env vars from request headers
var pfx = this.options.env_header_prefix;
var env_vars = _.chain(req.headers).map(function (val, key) {
try {
if (0 !== key.indexOf(pfx)) { return; }
var d_key = key.substr(pfx.length),
d_json = (new Buffer(val, 'base64'))
.toString('utf-8'),
data = JSON.parse(d_json);
return [d_key, data];
} catch (e) {
// No-op, ignore parsing errors for env vars
}
}).object().value();
// Build the API context
var api_ctx = new ks_api.APIContext({
server_options: $this.options,
env: env_vars,
request: req,
response: res,
source: src
});
try {
$this.macro_processor.process(src, api_ctx, function (errors, result) {
if (errors) {
errors.forEach(function (error) {
res.log.error(error.message, {
name: 'kumascript',
template: '%s: %s',
args: [error.name, error.message, error.options]
});
});
}
res.send(result);
});
} catch (error) {
res.log.error(error.message, {
name: 'kumascript',
template: '%s: %s',
args: [error.name, error.message, error.options]
});
// HACK: If all else fails, send back the source
res.send(src);
}
}
});
// ### Exported public API
module.exports = {
Server: Server
};