-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
200 lines (158 loc) · 4.7 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
var express = require('express')
, stylus = require('stylus')
, nib = require('nib')
, morgan = require('morgan')
, fs = require('fs')
, ip = require('./lib/ip')
, mailer = require('./lib/mailer')
, recaptcha = require('./lib/recaptcha')
, handbook = require('./lib/handbook')
, _ = require('lodash')
, datetime = require('./lib/datetime')
, meetup = require('./lib/meetup')
, bodyParser = require('body-parser')
, viewHelpers = require('./lib/view_helpers');
var app = express();
app.set('port', (process.env.PORT || 9090));
app.set('dev', (process.env.TF_ENV || "production"));
app.use(bodyParser.json());
// Enable Request Logging
app.use(morgan('dev'));
viewHelpers.init(app);
var cssCompile = function (str, path) {
var style = stylus(str)
.set('filename', path)
.set('sourcemap', {basePath: "public/css", sourceRoot: "../"})
.use(nib());
// Write a source map.
style.render(function() {
fs.writeFileSync("public/css/main.css.map", JSON.stringify(style.sourcemap));
});
return style;
};
app.use(stylus.middleware(
{
src: __dirname + '/public'
, compile: cssCompile
}
));
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.static(__dirname + '/public'));
app.use('/libs', express.static(__dirname + '/bower_components'));
app.get('/', function (req, res) {
res.render('index',
{title: 'Home'}
);
});
app.get('/jobs', function (req, res) {
res.render('jobs',
{title: 'Jobs'}
);
});
app.get('/contact', function (req, res) {
res.render('contact',
{title: 'Contact'}
);
});
app.get('/handbook', function (req, res) {
var handbookData = handbook.content();
var handbookHtml = handbook.html(handbookData);
res.render('handbook',
{
title: 'Trifork Handbook',
chunk: function (coll, n) {
var size = Math.ceil(coll.length / n);
return _.chunk(coll, size);
},
handbookHtml: handbookHtml,
handbookToc: handbook.toc(handbookData)
}
);
});
app.get('/handbook.epub', function (req, res) {
res.sendFile("public/handbook.epub");
});
app.get('/handbook.docx', function (req, res) {
res.sendFile("public/handbook.docx");
});
app.get('/handbook.pdf', function (req, res) {
res.sendFile("public/handbook.pdf");
});
app.get('/handbook.md', function (req, res) {
res.sendFile("public/handbook.md");
});
app.get('/goto-nights-stockholm', function (req, res) {
var render = function (events) {
res.render('events',
{
events: events,
title: 'Events',
format_date: datetime.format
}
);
};
var handleError = function () {
console.error("Unable to fetch events from meetup.com. Rendering empty list.");
render([]);
};
meetup.fetch_events().then(render, handleError);
});
app.post('/email', function (req, res) {
var message =
"Name: " + req.body.name + "\n\n" +
"Email: " + req.body.email + "\n\n" +
"Message:\n\n" + req.body.message;
console.log("Contact Form Message", message);
var handler = function(remote_ip) {
var recaptchaToken = req.body.recaptchaToken;
var mailOk = function() {
console.log("Email Sent");
res
.json({ message: "Email sent."})
.end();
};
var mailError = function() {
res
.status(500)
.json({ message: "Mail failed to send."})
.end();
};
var recaptchaOk = function() {
console.log("Sending email");
var to = 'thb@trifork.com';
var from = 'contact-form@trifork.se';
var subject = 'Trifork.se - Contact Form Submission';
mailer.send(from, to, subject, message)
.then(mailOk, mailError);
};
var recaptchaError = function(e) {
console.warn("reCaptche validation failed", e);
res.status(400).json({ message: "Invalid reCaptcha"}).end();
};
console.log("Checking reCaptcha", remote_ip, recaptchaToken);
recaptcha.verify(recaptchaToken, remote_ip).then(recaptchaOk, recaptchaError);
};
if (process.env.TF_ENV === 'dev') {
console.warn("DEV MODE: Using Server External IP for reCaptcha");
ip.getExternalIp().then(handler);
}
else {
ip.getRemoteIp(req).then(handler);
}
});
// Redirect old URLs to new ones, to make the crawlers happy.
// TODO: Consider doing this in a nginx config instead so,
// the request does not make it all the way to the app.
var redirects = [
["/events", "/goto-nights-stockholm"],
//["/goto/", "/goto-conference-stockholm"]
["/goto", "http://www.goto-stockholm.com/"],
["/goto-conference-stockholm", "http://www.goto-stockholm.com/"]
];
redirects.forEach(function(options) {
app.get(options[0], function(req, res) {
res.redirect(301, options[1]);
});
});
app.listen(app.get('port'));