-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
152 lines (120 loc) · 4.88 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
(function() {
var http = require('http');
var https = require('https');
var fs = require('fs');
var express = require('express');
var passport = require('passport');
var settings = require('./settings.json');
var views = require('./views.js');
var auth = require('./auth.js');
var rest = require('./rest');
var patterns = require('./patterns.js');
var ws = require('./ws');
var db = require('./db');
var app = express();
var httpServer = null;
var httpsServer = null;
if (settings.http) {
httpServer = http.createServer(app);
var port = settings.http.port;
if (port === "$ENV") {
port = process.env.PORT;
}
httpServer.listen(port, settings.http.host);
console.log("http server listening at " + settings.http.host + ':' + port);
}
if (settings.https) {
var certificate = {
key: fs.readFileSync(settings.https.certKey).toString(),
cert: fs.readFileSync(settings.https.cert).toString()
};
httpsServer = https.createServer(certificate, app);
httpsServer.listen(settings.https.port, settings.https.host);
console.log("https server listening at " + settings.https.host + ':' + settings.https.port);
}
function nocache(req, res, next) {
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');
next();
}
app.configure(function () {
app.use(express.logger());
app.use(express.cookieParser());
app.use(express.bodyParser({limit: '50mb'}));
app.use(express.methodOverride());
app.use(express.cookieSession({ secret: settings.sessionSecret }));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.static(__dirname + '/public'));
app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
/**
* Views
*/
app.get('/', views.index);
app.get('/login', views.login);
app.get('/about', views.about);
app.get('/newdoc', [auth.loggedIn], views.newdoc);
app.get('/editdoc/:fileid', [auth.loggedIn, auth.ensureFileUser], views.editdoc);
app.get('/newimg', [auth.loggedIn], views.newimg);
app.get('/editimg/:fileid', [auth.loggedIn, auth.ensureFileUser], views.editimg);
app.get('/loadimg', [nocache, auth.loggedInNoRedirect], views.loadImage);
/**
* Patterns (temporary)
**/
app.get('/patterns/', patterns.list);
app.get('/patterns/:path([a-z.-_/]*)', patterns.get);
/**
* Auth
*/
// Facebook
app.get('/auth/facebook', [auth.storeRedictUrl], passport.authenticate('facebook', { scope: ['email'] } ));
app.get('/auth/facebook/callback', passport.authenticate('facebook'), function(req, res) {
var redirectUrl = req.session.redirectUrl||'/';
req.session.redirectUrl = null;
res.redirect(redirectUrl);
});
// GitHub
app.get('/auth/github', [auth.storeRedictUrl], passport.authenticate('github', { scope: ['user:email'] } ));
app.get('/auth/github/callback', passport.authenticate('github', { failureRedirect: '/login' }), function(req, res) {
var redirectUrl = req.session.redirectUrl||'/';
req.session.redirectUrl = null;
res.redirect(redirectUrl);
});
// Google
app.get('/auth/google', [auth.storeRedictUrl], passport.authenticate('google', { scope: ['https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/userinfo.email'] } ));
app.get('/auth/google/callback', passport.authenticate('google', { failureRedirect: '/login' }), function(req, res) {
var redirectUrl = req.session.redirectUrl||'/';
req.session.redirectUrl = null;
res.redirect(redirectUrl);
});
// Logout
app.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
/**
* API
*/
// https://github.com/foyt/coops-spec/#get--load-request
app.get('/files/:fileid', [auth.loggedInNoRedirect, nocache], rest.fileGet);
// https://github.com/foyt/coops-spec/#get-update-update-request
app.get('/files/:fileid/update', [auth.loggedInNoRedirect, nocache], rest.fileUpdate);
// https://github.com/foyt/coops-spec/#patch--patch-request
app.patch('/files/:fileid', [auth.loggedInNoRedirect, nocache], rest.filePatch);
// https://github.com/foyt/coops-spec/#get-join-join-request
app.get('/files/:fileid/join', [auth.loggedInNoRedirect, nocache], rest.fileJoin);
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
app.use(express.static(__dirname + '/public_test'));
});
ws.configure(httpServer, httpsServer);
db.filesessions.remove();
}).call(this);