This repository has been archived by the owner on Dec 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
101 lines (90 loc) · 2.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
/**
* Module dependencies.
*/
const express = require('express');
const compression = require('compression');
const session = require('express-session');
const bodyParser = require('body-parser');
const logger = require('morgan');
const chalk = require('chalk');
const errorHandler = require('errorhandler');
const dotenv = require('dotenv');
const path = require('path');
const expressStatusMonitor = require('express-status-monitor');
const sass = require('node-sass-middleware');
const hbs = require('express-handlebars');
const passport = require('passport');
const expressValidator = require('express-validator');
/**
* Load environment variables from .env file, where API keys and passwords are configured.
*/
dotenv.load({ path: '.env' });
/**
* Controllers (route handlers).
*/
const homeController = require('./controllers/home');
const downloads = require('./controllers/api/downloads');
const dashboardController = require('./controllers/dashboard');
/**
* Create Express server.
*/
const app = express();
/**
* Passport config.
*/
const passportConfig = require('./config/passport');
/**
* Express configuration.
*/
app.engine('.hbs', hbs({extname: '.hbs', partialsDir: __dirname + '/views/partials'}));
app.set('host', process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0');
app.set('port', process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', '.hbs');
app.use(expressStatusMonitor());
app.use(compression());
app.use(expressValidator());
app.use(sass({
src: path.join(__dirname, 'public'),
dest: path.join(__dirname, 'public')
}));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(session({
resave: true,
saveUninitialized: true,
secret: process.env.SESSION_SECRET
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, 'public'), { maxAge: 31557600000 }));
/**
* App routes.
*/
app.get('/', homeController.index);
app.get('/dashboard', passportConfig.isAuthenticated, dashboardController.index);
/**
* Auth routes.
*/
app.get('/auth/google', passport.authenticate('google', { scope: 'profile email' }));
app.get('/auth/google/callback', passport.authenticate('google', { failureRedirect: '/' }), (req, res) => {
res.redirect('/dashboard');
});
/**
* API routes.
*/
app.post('/api/startDownload', passportConfig.isAuthenticated, downloads.start);
app.get('/api/listCurrentDownloads', passportConfig.isAuthenticated, downloads.list);
/**
* Error Handler.
*/
app.use(errorHandler());
/**
* Start Express server.
*/
app.listen(app.get('port'), () => {
console.log('%s App is running at http://localhost:%d in %s mode', chalk.green('✓'), app.get('port'), app.get('env'));
console.log(' Press CTRL-C to stop\n');
});
module.exports = app;