This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
205 lines (180 loc) · 5.9 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
/*
The Googol GLaDOS Simulator by Alex I'Anson and Wesley Hill
is licensed under the Creative Commons Attribution-NonCommercial 4.0 International License.
http://creativecommons.org/licenses/by-nc/4.0/
Based on a work at http://alexianson.co.uk/archive/gg/.
app.js
*/
// app.js handles the routing logic of the Googol GLaDOS Simulator using express.
var version = {
'version': "v3.0.1",
'release_name': "caroline-release"
}
var MobileDetect = require('mobile-detect');
var express = require('express');
var chalk = require('chalk');
var bp = require('body-parser');
var nunjucks = require('nunjucks');
var morgan = require('morgan');
var ac = require('appcache-node');
var app = express();
// Create a cache for offline access, invalidate each hour.
// add manifest="app.cache" to app_layout and index_layout
var cache = ac.newCache(["components/bootstrap/dist/css/bootstrap.css",
"components/howler/howler.min.js",
"sounds/chicken.wav",
"sounds/ignore.wav",
"sounds/glados_s6.wav",
"sounds/glados_s7.wav",
"sounds/glados_s8.wav",
"sounds/glados_s9.wav",
"sounds/glados_s10.wav",
])
cache += "\nNETWORK:\n*"
var googol = chalk.blue("g") + chalk.red("o") + chalk.yellow("o") + chalk.blue("g") + chalk.red("o") + chalk.green("l");
var glados = chalk.yellow("g") + chalk.green("l") + chalk.red("a") + chalk.blue("d") + chalk.red("o") + chalk.blue("s");
// Express settings and middleware.
app.set('port', (process.env.PORT || 5000));
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.use(express.static(__dirname + '/public'));
app.disable('x-powered-by');
if (app.settings.env == "development") {
app.use(morgan('dev'))
} else {
app.use(morgan('combined'))
}
// Configure view template for nunjucks.
var nunjucksTemplate = nunjucks.configure('views', {
autoescape:true,
express:app
});
nunjucksTemplate.addFilter('random', function(array) {
return array[Math.floor(Math.random() * array.length)]
});
// Nunjucks custom filters.
nunjucksTemplate.addFilter('json', function(obj) {
return JSON.stringify(obj);
});
// Configuration
// App environment detection.
if (process.env.NODE_HOME) {
if (process.env.NODE_HOME.match("heroku")) {
process.env.ON_HEROKU = true
}
} else {
process.env.ON_HEROKU = false
}
// Set SHOW_RELEASE_NAME to true if not defined.
if (!process.env.SHOW_RELEASE_NAME) {
process.env.SHOW_RELEASE_NAME = true
version['show_release_name'] = process.env.SHOW_RELEASE_NAME
} else {
version['show_release_name'] = process.env.SHOW_RELEASE_NAME
}
// Display app cache.
app.all('/app.cache', function(req, res){
res.writeHead(200, {'Content-Type': 'text/cache-manifest'});
res.end(cache);
})
// Routes.
// Index.
app.get('/', function(req, res) {
var ismob = isMobile(req)
on_heroku = process.env.ON_HEROKU
footerWords = ['Made', 'Crafted', 'Designed', 'Built', 'Created']
date = new Date()
res.render("index.html", {
words: footerWords,
on_heroku: on_heroku,
mob:ismob,
v: version,
year:date.getFullYear()
})
});
// robots.txt
app.all('/robots.txt', function(req, res) {
robotstxt = ""
robotstxt += "User-Agent: *"
robotstxt += "\nDisallow: /app/"
robotstxt += "\nDisallow: /end/"
robotstxt += "\nDisallow: /nope/"
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(robotstxt)
});
// User-Agent Checker.
function isMobile(req) {
md = new MobileDetect(req.headers['user-agent'])
if (md.mobile() || md.is("Console") || md.is("Watch") || md.is("MobileBot") || md.match("\b(Nintendo|Nintendo WiiU|Nintendo 3DS|PLAYSTATION|Xbox)\b")) {
return true
} else {
return false
}
}
// Nope.
app.get('/nope', function(req, res, next) {
if(isMobile(req) == true){
res.render("toobad",{device:md.mobile()})
} else {
res.redirect("/")
}
});
app.get('/end', function(req, res) {
res.redirect("/")
})
// App
app.get('/app', function(req, res) {
if(isMobile(req) == true){
res.render("toobad",{device:md.mobile()})
} else {
exists = req.get('X-IS-GGS')
q = req.query['s']
if (exists) {
if (q == 1) {
res.render("1")
} else if (q == 2) {
res.render("2")
} else if (q == 3) {
res.render("3")
} else if (q == 4) {
res.render("4")
} else if (q == 5) {
res.render("5")
} else if (q == 6) {
res.render("6")
} else if (q == 7) {
res.render("7")
} else if (q == 8) {
res.render("8")
} else if (q == 9) {
res.render("9")
} else if (q == 10) {
res.render("10")
} else if (q == 11) {
on_heroku = process.env.ON_HEROKU
footerWords = ['Made', 'Crafted', 'Designed', 'Built', 'Created']
date = new Date()
res.render("end", {
words: footerWords,
on_heroku: on_heroku,
v: version,
year:date.getFullYear()
})
} else {
res.redirect("/")
}
} else {
res.redirect("/")
}
}
});
// Run server.
app.listen(app.get('port'), function() {
var env = app.settings.env
console.log(googol + " " + glados + ":simulator listening on port " + chalk.cyan.bold(app.get('port')));
if (env == "development") {
console.log("environment status: " + chalk.yellow.bold(env))
} else {
console.log("environment status: " + chalk.green.bold(env))
}
});