-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
203 lines (155 loc) · 5.14 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
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, config = require('./config/config')
, expressValidator = require('express-validator')
, bunyan = require('bunyan')
, _ = require('underscore')
, passport = require('passport')
, LocalStrategy = require('passport-local').Strategy
, store = require('revisionary')
, wiki = require('wiki')
, markup = require('markup')
, http = require('http')
, path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.set('log',bunyan.createLogger({name: "wiki", streams: [{
path: 'app.log',
},{
stream: process.stderr,
level: 'info'
}]
}))
// view helpers
app.locals({
_ : _,
wiki: wiki,
markup: markup
})
// authentication
passport.use(new LocalStrategy(
function(username, password, done) {
if (username && password) {
done(null, { email: username })
} else {
done(null, false, 'Incorrect username or password')
}
}
));
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.bodyParser());
app.use(expressValidator());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(require('less-middleware')({ src: __dirname + '/public' }));
// normal error-handling middleware
app.use(function(err, req, res, next) {
app.get('log').error(err);
res.render('error.jade')
})
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
// authentication
passport.serializeUser(function(user, done) {
done(null, user.email);
});
passport.deserializeUser(function(email, done) {
done(null, {email: email})
});
function auth(format) {
format || (format = 'html')
switch(format) {
case 'html':
default:
return function(req,res,next) {
if (req.isAuthenticated()) { res.locals.user = req.user; return next(); }
res.redirect('/login')
}
}
}
// set up repository
app.set('store', (function() {
var StoreClass, engine = config.store.engine, appStore;
switch(engine) {
case 'git':
default:
StoreClass = store.GitStore;
}
// Create new store for the app
appStore = new StoreClass(config.store.path);
if(!appStore) {
// TODO: handle error
console.log("Failed to create store for application with engine '"+engine+"'. Check config.json.")
throw new Error();
}
// If the appStore was created successfully, detect whether the
// repository needs to be initialized and if so, do so.
appStore.detect(function(err,valid) {
if(err) {
// TODO: handle error
console.log(err)
throw err;
}
if(!valid) {
appStore.initialize(null, function(err) {
if(err) {
// TODO: handle error
console.log(err)
throw err;
}
})
}
})
return appStore;
})() )
routes = routes(app)
// configure routes
app.get('/login',function(req,res,next) {
res.render('login.jade')
})
app.post('/login',
passport.authenticate('local', { successRedirect: '/',
failureRedirect: '/login',
failureFlash: true })
);
app.get('/', routes.index);
app.get(/^\/pages\/([\w\.\/% ]+)\/view$/, auth('html'), routes.page.view);
app.get(/^\/pages\/([\w\.\/% ]+)\/view\/([\w]+)$/, auth('html'), routes.page.view);
app.get(/^\/pages\/([\w\.\/% ]+)\/diff\/([\w]+)\/([\w]+)$/, auth('html'), routes.page.diff);
app.get(/^\/pages\/([\w\.\/% ]+)\/edit$/, auth('html'), routes.page.editor);
app.get(/^\/pages\/([\w\.\/% ]+)\/log$/, auth('html'), routes.page.log);
app.get(/^\/pages\/([\w\.\/% ]+)\/print$/, auth('html'), routes.page.print);
app.get(/^\/pages\/([\w\.\/% ]+)\/export\/([\w\. ]+)$/, auth('html'), routes.page.export);
app.get(/^\/pages\/([\w\.\/% ]+)\/raw$/, auth('html'), routes.page.raw);
app.get(/^\/files\/([\w\.\/% ]+)\/?/, auth('html'), routes.page.raw);
app.post(/^\/pages\/([\w\.\/% ]+)\/edit$/, auth('html'), routes.page.edit);
app.post(/^\/pages\/([\w\.\/% ]+)\/remove$/, auth('html'), routes.page.remove);
app.post(/^\/pages\/([\w\.\/% ]+)\/move$/, auth('html'), routes.page.move);
app.get(/^\/pages\/([\w\.\/% ]+)\/discuss$/, auth('html'), routes.page.discuss.view);
app.get(/^\/pages\/([\w\.\/% ]+)\/discuss\/edit$/, auth('html'), routes.page.discuss.editor);
app.post(/^\/pages\/([\w\.\/% ]+)\/discuss\/edit$/, auth('html'), routes.page.discuss.edit);
app.get('/categories', auth('html'), routes.categories.list)
app.get(/^\/categories\/([\w\.\/ ]+)/, auth('html'), routes.categories.view)
app.get('/upload', auth('html'),routes.special.uploader)
app.post('/upload', auth('html'),routes.special.upload)
app.get('/recent', auth('html'), routes.special.recent)
app.get('/all', auth('html'), routes.special.all)
app.post('/go', auth('html'), routes.special.go)
app.post('/search', auth('html'), routes.special.search)
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});