This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
119 lines (103 loc) · 2.82 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
const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const winston = require('winston');
const logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({
'timestamp': true,
'colorize': true
})
]
});
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const i18n = require('i18n');
const session = require('express-session');
const yaml = require('js-yaml');
const mongoose = require('mongoose');
const passport = require('passport');
const flash = require('connect-flash');
const compression = require('compression');
const nconf = require('nconf');
nconf.file({
file: './config/default.yaml',
format: {
parse: yaml.safeLoad,
stringify: yaml.safeDump
}
});
const app = express();
app.use(compression());
app.set('views', path.resolve(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(favicon(path.resolve(__dirname, 'public', 'favicon.ico')));
// app.use(bodyParser.json());
app.use(bodyParser.raw());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.resolve(__dirname, 'public')));
app.use(session({
secret: 'one of these days i\'m gonna cut you into little pieces',
resave: true,
saveUninitialized: true
}));
app.use(flash());
// GitHub authentication
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (obj, done) {
done(null, obj);
});
const github = require('./lib/githubPassport');
app.use(passport.initialize());
app.use(passport.session());
github.init(app, passport);
// Log out
app.get('/logout', function (req, res) {
if (req.user) {
logger.info(
// Date/time
'User',
req.user.displayName,
'(' + req.user.id + ')',
'logged out'
);
}
req.logout();
req.flash('info', "You've been successfully logged out");
res.redirect('/');
});
// i18n
i18n.configure({
locales: nconf.get('OpenNMTBenchmark:locales'),
defaultLocale: nconf.get('OpenNMTBenchmark:default:locale'),
cookie: 'locale',
directory: path.resolve(__dirname, 'locales')
});
app.use(function (req, res, next) {
res.locals.__ = res.__ = function () {
return i18n.__.apply(req, arguments);
};
next();
});
app.use(i18n.init);
// Set globals
app.use(require('./routes/globals'));
// Routers
app.use('/', require('./routes/router'));
app.use('/', require('./routes/async'));
app.use('/', require('./lib/rest'));
app.use(function (req, res, next) {
var err = new Error(req.url + ' not found');
err.status = 404;
next(err);
});
app.use(function (err, req, res, next) {
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
res.status(err.status || 500);
res.render('error');
});
module.exports = app;