forked from Detry322/redisred
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
77 lines (69 loc) · 2.35 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
// Load the dotfiles.
require('dotenv').load();
var port = process.env.PORT || 3000;
var redisUrl = process.env.REDIS_URL || 'redis://127.0.0.1:6379/0';
var sessionSecret = process.env.SESSION_SECRET || 'this is really secure';
var adminUsername = process.env.ADMIN_USERNAME || 'admin';
var adminPassword = process.env.ADMIN_PASSWORD || '123456';
var rootRedirect = process.env.ROOT_REDIRECT || 'https://google.com';
var apiToken = process.env.API_TOKEN || '1234567890abcdefghijklmnopqrstuvwxyz';
//Includes
var authentication = require('./authentication');
var express = require('express');
var expressSession = require('express-session');
var cookieParser = require('cookie-parser');
var Redis = require('ioredis');
var passport = require('passport');
var favicon = require('serve-favicon');
var RedisStore = require('connect-redis')(expressSession);
//Initialize auth
authentication(passport, adminUsername, adminPassword);
//Connect to Redis
var redis = new Redis(redisUrl);
//Initialize the app
var app = express();
var redisSessionStore = new RedisStore({ client: redis });
app.set('views', './views');
app.set('view engine', 'jade');
app.use(favicon('./public/assets/favicon.png'));
app.use(cookieParser());
app.use(
expressSession({
store: redisSessionStore,
secret: sessionSecret,
resave: true,
saveUninitialized: true,
}),
);
app.use(passport.initialize());
app.use(passport.session());
//Initialize controllers
var frontendController = require('./controllers/admin/FrontendController')(
redis,
passport,
);
var apiController = require('./controllers/admin/APIController')(
redis,
apiToken,
);
var redirectController = require('./controllers/RedirectController')(redis);
//Initialize routes
var admin = require('./routes/admin.js')(frontendController, apiController);
app.use('/admin', admin);
var list = require('./routes/list.js')(frontendController, apiController);
app.use('/list', list);
var main = require('./routes/main.js')(rootRedirect, redirectController);
app.use('/', main);
app.use(function (req, res, next) {
res.status(404).render('404');
});
// Start the server
console.log(`Connecting to redis for admin ${adminUsername}...`);
redis.ping(function (err) {
if (!err) {
console.log('Connection successful. Server listening on port ' + port);
app.listen(port);
} else {
console.error(`Redis connection failed: ${err}`);
}
});