-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
76 lines (58 loc) · 2.05 KB
/
index.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
var express = require('express')
, app = express()
, server = require('http').Server(app)
, bodyParser = require('body-parser')
, mappings = require('./lib/mappings')
, config = require('./lib/config')
, path = require('path')
, session = require('express-session')
, middlewares = require('./lib/middlewares')
, webapp = express.Router()
, api = express.Router()
, login = require('./lib/login')
;
// Initialize Express server and session
// Right now the session store is a simple in memory key value store and gets erased
// on restart. If needed I'll create a Nedb-backed one
app.use(bodyParser.json());
app.use(session({ secret: 'eropcwnjdi'
, resave: true
, saveUninitialized: true
}));
// API
api.use(middlewares.apiMustBeLoggedIn);
api.post('/mappings', mappings.createMapping);
app.use('/api', api);
// Auth with Google
app.get('/login', login.initialRequest);
app.get('/googleauth', login.returnFromGoogle);
app.get('/logout', login.logout);
// Web interface
webapp.use(middlewares.mustBeLoggedIn);
webapp.use(middlewares.addCommonLocals);
webapp.get('/create', function (req, res) {
res.render('create-mapping.jade');
});
webapp.get('/list', mappings.showAllMappings);
webapp.get('/view/:from', mappings.viewMapping);
app.use('/web', webapp);
// Root. Descriptive main page if not logged, main action (create a mapping) if logged
app.get('/', function (req, res) {
if (req.session.user) {
return res.redirect(302, '/web/create');
} else {
return res.render('main.jade');
}
});
// Serve static client-side js and css (should really be done through Nginx but at this scale we don't care)
app.get('/assets/*', function (req, res) {
res.sendFile(process.cwd() + req.url);
});
// Actual redirections
app.get('/:from', mappings.redirect);
// Last wall of defense against a bad crash
process.on('uncaughtException', function (err) {
console.log('Caught an uncaught exception, I should probably send an email or something');
console.log(err);
});
server.listen(config.serverPort);