This repository has been archived by the owner on Sep 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
app.js
executable file
·209 lines (179 loc) · 5.75 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
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
var express = require("express"),
favicon = require("serve-favicon"),
static = require("serve-static"),
compress = require("compression"),
bodyparser = require("body-parser"),
forceSSL = require("express-force-ssl"),
goggles = require("./lib/goggles"),
habitat = require("habitat"),
helmet = require("helmet"),
i18n = require("webmaker-i18n"),
lessMiddleWare = require("less-middleware"),
nunjucks = require("nunjucks"),
path = require("path"),
version = require("./package").version,
wts = require("webmaker-translation-stats"),
WWW_ROOT = path.resolve(__dirname, 'public');
// Load config from ".env"
habitat.load();
// Generate app variables
var app = express(),
appName = "goggles",
env = new habitat(),
node_env = env.get('NODE_ENV'),
nunjucksEnv = new nunjucks.Environment([
new nunjucks.FileSystemLoader(path.join(__dirname, 'views')),
new nunjucks.FileSystemLoader(path.join(__dirname, 'public/bower'))
], {
autoescape: true
});
// No need to tell the world this:
app.disable("x-powered-by");
// Enable template rendering with nunjucks
nunjucksEnv.express(app);
nunjucksEnv.addFilter( "instantiate", function( input ) {
var tmpl = new nunjucks.Template( input );
return tmpl.render( this.getVariables() );
});
if (!!env.get("FORCE_SSL") ) {
app.set("forceSSLOptions", {
enable301Redirects: true,
trustXFPHeader: true,
httpsPort: 443,
sslRequiredMessage: "SSL Required."
});
app.use(forceSSL);
app.use(helmet.hsts());
app.enable("trust proxy");
}
app.use(favicon(__dirname + '/public/img/favicon.ico'));
app.use(compress());
app.use(bodyparser.json());
app.use(bodyparser.urlencoded());
// Setup locales with i18n
app.use( i18n.middleware({
supported_languages: env.get("SUPPORTED_LANGS"),
default_lang: "en-US",
mappings: require("webmaker-locale-mapping"),
translation_directory: path.resolve( __dirname, "locale" )
}));
app.use(require("xfo-whitelist")([
"/easy-remix-dialog/index.html",
"/easy-remix-dialog/blank.html",
"/preferences.html",
"/uproot-dialog.html",
"/help.html"
]));
app.locals.GA_ACCOUNT = env.get("GA_ACCOUNT");
app.locals.GA_DOMAIN = env.get("GA_DOMAIN");
app.locals.hostname = env.get("APP_HOSTNAME");
app.locals.languages = i18n.getSupportLanguages();
app.locals.bower_path = "public/bower";
// universal error handling
app.use(function(err, req, res, next) {
console.error(err.msg);
throw err;
});
// DEVOPS - Healthcheck
app.get('/healthcheck', function(req, res) {
var healthcheckObject = {
http: "okay",
version: version
};
wts(i18n.getSupportLanguages(), path.join(__dirname, "locale"), function(err, data) {
if(err) {
healthcheckObject.locales = err.toString();
} else {
healthcheckObject.locales = data;
}
res.json(healthcheckObject);
});
});
app.get('/', function(req, res) {
res.render("homepage/index.html", {
idwmoURL: env.get("ID_WMO_URL"),
clientId: env.get("ID_WMO_CLIENT_ID")
});
});
app.get('/index.html', function(req, res) {
res.redirect("/");
});
app.get('/ftu', function(req, res) {
res.render("homepage/ftu.html", {
idwmoURL: env.get("ID_WMO_URL"),
clientId: env.get("ID_WMO_CLIENT_ID")
});
});
// oauth login confirmation page
app.get("/login-confirmation.html", function(req, res) {
res.render("login-confirmation.html");
});
app.get("/authtoken.js", function(req, res) {
res.render("authtoken.js");
});
// intercept webxray's publication dialog - HTML part
app.get("/uproot-dialog.html", function(req, res) {
res.render("uproot-dialog.html", {
hostname: env.get("APP_HOSTNAME")
});
});
// intercept webxray's help dialog - HTML part
app.get("/help.html", function(req, res) {
res.render("help.html", {
hostname: env.get("APP_HOSTNAME")
});
});
// intercept webxray's publication dialog - JS part
app.get("/publication.js", function(req, res) {
res.set( "Content-Type", "application/javascript; charset=utf-8" );
res.render("publication.js", {
idwmoURL: env.get("ID_WMO_URL"),
clientId: env.get("ID_WMO_CLIENT_ID"),
clientIdLib: env.get("ID_WMO_CLIENT_ID_LIB"),
publishwmoURL: env.get("PUBLISH_WMO_URL")
});
});
// intercept webxray's publication dialog - HTML part
app.get("/gogglesnotice.js", function(req, res) {
res.render("gogglesnotice.js", {
hostname: env.get("APP_HOSTNAME")
});
});
var optimize = (node_env !== "development"),
tmpDir = path.join( require("os").tmpDir(), "mozilla.webmaker.org");
app.use(lessMiddleWare({
once: optimize,
debug: !optimize,
dest: tmpDir,
src: WWW_ROOT,
compress: true,
yuicompress: optimize,
optimization: optimize ? 0 : 2
}));
// serve static content, resolved in this order:
app.use(static(tmpDir));
app.use(static(path.join(__dirname, "public")));
app.use(static(path.join(__dirname, "webxray/static-files")));
["src", "test", "js"].forEach(function(dir) {
app.use("/" + dir, static(path.join(__dirname, "webxray/" + dir)));
});
// Localized Strings
app.get("/strings/:lang?", function( req, res, next ) {
res.header( "Access-Control-Allow-Origin", "*" );
res.jsonp(i18n.getStrings(req.params.lang || req.localeInfo.lang || "en-US"));
});
// override some path
app.get("/easy-remix-dialog/index.html", function(req, res) {
res.render("/easy-remix-dialog/index.html");
});
// build webxray and then run the app server
goggles.build(env, nunjucksEnv, function() {
app.listen(env.get("port"), function(){
console.log('Express server listening on ' + env.get("APP_HOSTNAME"));
});
});
// If we're in running in emulated S3 mode, run a mini
// server for serving up the "s3" published content.
if (env.get("node_env") !== "production") {
require("mox-server").runServer(env.get("MOX_PORT", 12319));
}