-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
113 lines (94 loc) · 3.46 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
const express = require('express');
const session = require('express-session');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const passport = require('passport');
const httpsRedirect = require('express-https-redirect');
const cron = require('node-cron');
const swaggerUi = require('swagger-ui-express');
const YAML = require('yamljs');
const podcasts = require('./routes/podcasts');
const account = require('./routes/account');
const db = require('./controllers/database');
const sessionCtrl = require('./controllers/session');
const createSwaggerMiddleware = require('swagger-express-middleware');
// Refresh all podcasts every 15 minutes.
cron.schedule('*/15 * * * *', db.refreshAll);
const app = express();
const swaggerFile = path.join(__dirname, '/api/swagger.yaml');
const swaggerDocument = YAML.load(swaggerFile);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
createSwaggerMiddleware(swaggerFile, app, (err, middleware) => {
app.use(
middleware.metadata(),
middleware.files(),
middleware.parseRequest(),
middleware.validateRequest(),
);
// Enable express sessions
app.use(session({
secret: 'keyboard cat',
// secret: require('crypto').randomBytes(64).toString('hex'),
resave: false,
saveUninitialized: true,
cookie: {maxAge: 3600000},
}));
app.use(require('prerender-node'));
app.use(passport.initialize());
app.use(passport.session());
passport.use(db.User.createStrategy());
passport.serializeUser(db.User.serializeUser());
passport.deserializeUser(db.User.deserializeUser());
app.use(express.static('public'));
app.use('/node_modules/qrcode-generator',
express.static(__dirname + '/node_modules/qrcode-generator'));
app.use('/node_modules/angular-qrcode',
express.static(__dirname + '/node_modules/angular-qrcode'));
app.use('/node_modules/ngclipboard',
express.static(__dirname + '/node_modules/ngclipboard'));
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// Redirect to https
app.use('*', httpsRedirect());
app.use('/api', podcasts);
app.use('/api/account', account);
app.post('/api/account/register', function(req, res, next) {
db.User.register(new db.User({username: req.body.username}),
req.body.password, function(err, account) {
if (err) {
console.log(err);
return next(err);
}
passport.authenticate('local')(req, res, function() {
sessionCtrl.synchronizeSession(req);
res.redirect('/');
});
});
});
app.post('/api/account/login', passport.authenticate('local'),
function(req, res) {
sessionCtrl.synchronizeSession(req);
res.redirect('/');
});
app.post('/api/account/logout', function(req, res) {
sessionCtrl.resetSession(req);
req.logout();
res.redirect('/');
});
// Default error handler
app.use(function(err, req, res, next) {
console.log(err);
res.status(err.status || 500).send(err || 'Internal server error');
});
app.get('*', function(req, res) {
res.sendFile(__dirname + '/public/index.html');
});
});
module.exports = app;