This repository was archived by the owner on May 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathstatic
More file actions
executable file
·144 lines (117 loc) · 4.52 KB
/
Copy pathstatic
File metadata and controls
executable file
·144 lines (117 loc) · 4.52 KB
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
#!/usr/bin/env node
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
require('../lib/baseExceptions').addExceptionHandler();
const fs = require('fs');
const path = require('path');
const url = require('url');
const http = require('http');
const cachify = require('connect-cachify');
const connect_fonts = require('connect-fonts');
const connect_fonts_feurasans = require('connect-fonts-feurasans');
const connect_fonts_opensans = require('connect-fonts-opensans');
const express = require('express');
const hood = require('hood');
const i18n = require('i18n-abide');
const urlparse = require('urlparse');
const addP3PHeader = require('../lib/p3p.js');
const assets = require('../lib/static_resources').all;
const config = require('../lib/configuration.js');
const db = require('../lib/db.js');
const heartbeat = require('../lib/heartbeat.js');
const httputils = require('../lib/httputils.js');
const i18n_check = require('../lib/i18n_client_check');
const logger = require('../lib/logging/logging.js').getLogger('bid.bin.static');
const proxySecure = require('../lib/proxy-secure');
const shutdown = require('../lib/shutdown.js');
const statsd = require("../lib/logging/middleware/statsd");
const toobusy = require('../lib/busy_middleware.js');
const wsapi = require('../lib/wsapi.js');
const views = require('../lib/static/views.js');
var app = express.createServer();
logger.info("static starting up");
if (config.get('env') === 'production') {
logger.info('node.js version: ' + process.version + ' at ' + process.execPath);
logger.info('configuration: ', JSON.stringify(JSON.parse(config.toString())));
}
// Setup health check / heartbeat middleware.
// This is in front of logging on purpose. see issue #537
heartbeat.setup(app);
// block requests when overloaded
app.use(toobusy);
// cover our head
app.use(proxySecure());
app.use(hood({
csp: config.get('csp'),
hsts: config.get('hsts'),
xframe: config.get('x_frame_options')
}));
// logging! all requests other than __heartbeat__ are logged
app.use(express.logger({
format: config.get('express_log_format'),
stream: {
write: function(x) {
logger.info(typeof x === 'string' ? x.trim() : x);
}
}
}));
// #2.1 - localization
app.use(i18n.abide({
supported_languages: config.get('supported_languages'),
default_lang: config.get('default_lang'),
debug_lang: config.get('debug_lang'),
translation_directory: config.get('translation_directory'),
disable_locale_check: config.get('disable_locale_check')
}));
i18n_check();
app.use(statsd());
// Add P3P headers to please IE8
app.use(addP3PHeader);
var static_root = path.join(__dirname, "..", "resources", "static");
// #7 - perform response substitution to support local/dev/beta environments
// (specifically, this replaces URLs in responses, e.g. https://browserid.org
// with https://diresworb.org)
config.performSubstitution(app);
var font_middleware = connect_fonts.setup({
fonts: [ connect_fonts_opensans, connect_fonts_feurasans ],
allow_origin: config.get('public_url'),
ua: 'all'
});
app.use(cachify.setup(assets(config.get('supported_languages')),
{
prefix: config.get('cachify_prefix'),
production: config.get('use_minified_resources'),
root: static_root,
url_to_paths: connect_fonts.urlToPaths
}));
// #9 - handle views for dynamicish content
views.setup(app);
// add 'Access-Control-Allow-Origin' headers to static resources that will be served
// from the CDN. We explicitly allow resources served from public_url to access these.
app.use(function(req, res, next) {
res.on('header', function() {
// this allows fonts to be requested cross domain
res.setHeader("Access-Control-Allow-Origin", config.get('public_url'));
// this makes sure caches properly consider language headers
res.setHeader('Vary', 'Accept-Encoding,Accept-Language');
});
next();
});
app.use(font_middleware);
// catch urls that express can't handle (issue #2887)
app.use(function(req, res, next) {
try {
decodeURIComponent(req.url);
next();
} catch (e) {
logger.info('invalid url requested: ' + req.url);
httputils.notFound(res);
}
});
app.use(express.static(static_root));
var bindTo = config.get('bind_to');
app.listen(bindTo.port, bindTo.host, function() {
logger.info("running on http://" + app.address().address + ":" + app.address().port);
});
shutdown.handleTerminationSignals(app, toobusy.shutdown);