forked from azat-co/blog-express
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
134 lines (113 loc) · 3.84 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
var TWITTER_CONSUMER_KEY = process.env.TWITTER_CONSUMER_KEY || 'ABC'
var TWITTER_CONSUMER_SECRET = process.env.TWITTER_CONSUMER_SECRET || 'XYZXYZ'
var express = require('express'),
routes = require('./routes'),
http = require('http'),
path = require('path'),
mongoskin = require('mongoskin'),
dbUrl = process.env.MONGOHQ_URL || 'mongodb://@localhost:27017/blog',
db = mongoskin.db(dbUrl, {safe: true}),
collections = {
articles: db.collection('articles'),
users: db.collection('users')
}
everyauth = require('everyauth');
everyauth.debug = true;
everyauth.twitter
.consumerKey(TWITTER_CONSUMER_KEY)
.consumerSecret(TWITTER_CONSUMER_SECRET)
.findOrCreateUser( function (session, accessToken, accessTokenSecret, twitterUserMetadata) {
var promise = this.Promise();
process.nextTick(function(){
if (twitterUserMetadata.screen_name === 'azat_co') {
session.user = twitterUserMetadata;
session.admin = true;
}
promise.fulfill(twitterUserMetadata);
})
return promise;
// return twitterUserMetadata
})
.redirectPath('/admin');
//we need it because otherwise the session will be kept alive
everyauth.everymodule.handleLogout(routes.user.logout);
everyauth.everymodule.findUserById( function (user, callback) {
callback(user)
});
var app = express();
app.locals.appTitle = "blog-express";
app.use(function(req, res, next) {
if (!collections.articles || ! collections.users) return next(new Error("No collections."))
req.collections = collections;
return next();
});
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.cookieParser('3CCC4ACD-6ED1-4844-9217-82131BDCB239'));
app.use(express.session({secret: '2C44774A-D649-4D44-9535-46E296EF984F'}))
app.use(everyauth.middleware());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(require('stylus').middleware(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public')));
app.use(function(req, res, next) {
if (req.session && req.session.admin)
res.locals.admin = true;
next();
});
//authorization
var authorize = function(req, res, next) {
if (req.session && req.session.admin)
return next();
else
return res.send(401);
};
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.use(app.router);
//PAGES&ROUTES
app.get('/', routes.index);
app.get('/login', routes.user.login);
app.post('/login', routes.user.authenticate); //if you use everyauth, this /logout route is overwriting by everyauth automatically, therefore we use custom/additional handleLogout
app.get('/logout', routes.user.logout);
app.get('/admin', authorize, routes.article.admin);
app.get('/post', authorize, routes.article.post);
app.post('/post', authorize, routes.article.postArticle);
app.get('/articles/:slug', routes.article.show);
//REST API ROUTES
app.all('/api', authorize);
app.get('/api/articles', routes.article.list)
app.post('/api/articles', routes.article.add);
app.put('/api/articles/:id', routes.article.edit);
app.del('/api/articles/:id', routes.article.del);
app.all('*', function(req, res) {
res.send(404);
})
// http.createServer(app).listen(app.get('port'), function(){
// console.log('Express server listening on port ' + app.get('port'));
// });
var server = http.createServer(app);
var boot = function () {
server.listen(app.get('port'), function(){
console.info('Express server listening on port ' + app.get('port'));
});
}
var shutdown = function() {
server.close();
}
if (require.main === module) {
boot();
}
else {
console.info('Running app as a module')
exports.boot = boot;
exports.shutdown = shutdown;
exports.port = app.get('port');
}