-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
65 lines (50 loc) · 1.61 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
var express = require('express');
var logger = require('morgan');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var cors = require('cors');
var GithubStrategy = require('passport-github').Strategy;
var passport = require('passport');
var CN = require('cn-utils');
var env = process.env.NODE_ENV || 'development';
var config = require('./config/' + env);
var oauth = require('./config/oauth');
var User = require('./models/user');
require('colors');
var app = express();
// passport setup
passport.use(new GithubStrategy(oauth.github,
function (accessToken, refreshToken, profile, cb) {
return cb(null, profile);
}));
// serialize and deserialize
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (obj, done) {
done(null, obj);
});
mongoose.connect(config.db.url);
app.db = mongoose.connection;
app.use(cors({
origin: true,
credentials: true
}));
app.db.on('open', function () {
console.log('connected to db'.yellow);
});
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(passport.initialize());
CN.configPassport(passport, User, config);
require('./routes/auth-jwt')(app, User, config.secret, '/auth/jwt');
require('./routes/users')(app, '/users');
require('./routes/problems')(app, '/problems');
require('./routes/submissions')(app, '/submissions');
require('./routes/contests')(app, '/contests');
require('./routes/auth')(app, '/auth', passport);
// / catch 404 and forwarding to error handler
app.use(function (req, res, next) {
res.status(404).json({ok: 'false', error: 'not found'});
});
module.exports = app;