-
Notifications
You must be signed in to change notification settings - Fork 7
/
server.js
89 lines (75 loc) · 2.63 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
var express = require('express'),
app = express(),
port = process.env.PORT || 3000,
server = app.listen(port),
GoatFactory = require('./goatFactory'),
isFinite = require('lodash.isfinite'),
logger = require('./logger');
logger.info('Server started 🐐');
app.use(express.static(__dirname + '/public_html'));
app.get('/', function (req, res) {
res.send(__dirname + '/public_html/index.html');
});
app.get('/count.json', function (req, res) {
GoatFactory.getGoatsServedCount(function (err, count) {
if (err) {
res.send(500);
} else {
res.set('Content-Type', 'application/json');
res.send({count: count});
}
});
});
// Goat, see?
app.get('/goatse/:width', function (req, res) {
resizeAndServe({
width: req.params.width,
goatse: true
}, req, res);
});
app.get('/goatse/:width/:height', function (req, res) {
resizeAndServe({
width: req.params.width,
height: req.params.height,
goatse: true
}, req, res);
});
// Normal Goats
app.get('/:width', function (req, res) {
resizeAndServe({
width: req.params.width
}, req, res);
});
app.get('/:width/:height', function (req, res) {
resizeAndServe({
width: req.params.width,
height: req.params.height
}, req, res);
});
function resizeAndServe (params, req, res) {
params.height = params.height || params.width; //set height to width if it was not set
if (params.width.indexOf('.php') > -1) {
//handle script kiddies looking for PHP servers
logger.warn('Served a 404 to idiots looking for a .php file. IP: %s', req.get('x-forwarded-for'));
return res.status(404).send("Cannot GET /" + params.width);
}
params.width = parseInt(params.width, 10);
params.height = parseInt(params.height, 10);
if (!isFinite(params.width) || !isFinite(params.height)) {
return res.status(404).send("You must provide a number!");
}
if (params.width > 1500 || params.height > 1500) {
return res.status(413).send("Slow down, buddy. We don't have goats that big.");
}
logger.info('Request for %s x %s from %s - Referrer: %s', req.params.width, req.params.height, req.get('x-forwarded-for'), req.get('Referrer'));
GoatFactory.grabAGoat(params, function (err, goat) {
if (err) {
logger.error("Error serving goat:", err);
res.status(500).send("Sorry, the goats started chewing on the server.");
} else {
GoatFactory.updateGoatsServedCount();
res.set('Content-Type', 'image/jpeg');
res.send(goat);
}
});
}