This repository has been archived by the owner on Dec 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
150 lines (122 loc) · 4.13 KB
/
server.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// module dependencies
'use strict';
var express = require('express'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
session = require('express-session'),
flash = require('connect-flash'),
http = require('http'),
path = require('path');
var fs = require('fs'),
sqlite3 = require('sqlite3');
var passport = require('passport'),
GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
var app = express();
var routes = require('./routes'),
config = require('./config');
app.set('port', process.env.PORT || 22245);
app.set('views', path.join(__dirname, 'templates'));
app.set('view engine', 'jade');
app.use(express.static(path.join(__dirname, 'public')));
// accept form POST requests
app.use(bodyParser.urlencoded({
extended: true
}));
// start to allow logging in
app.use(cookieParser());
app.use(session({
key: '',
secret: config.secret,
cookie: {
path: '/',
httpOnly: true,
maxAge: null
},
resave: true,
saveUninitialized: true,
proxy: null
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
// authentication
function isLoggedIn(req, res, next) {
if (req.isAuthenticated() || app.get('env') === 'development') {
return next();
}
res.redirect('/');
}
passport.use(
new GoogleStrategy(
{
callbackURL: config.root + '/login/google/return',
clientID: '355722129027.apps.googleusercontent.com',
clientSecret: 'iFRPbiAo6FFVyF88UQjYOu3U',
realm: config.root
},
function (accessToken, refreshToken, profile, done) { // verify callback
var i;
for (i = 0; i < profile.emails.length; i += 1) {
if (profile.emails[i].value.substr(-12) === '@nouse.co.uk') {
return done(null, true);
}
}
return done(null, false);
}
)
);
// two routes are required for OpenID (Google) authentication
app.get('/login/google', passport.authenticate('google', {
scope: ['email']
}));
app.get('/login/google/return', passport.authenticate('google', {
successRedirect: '/tournaments',
failureRedirect: '/', // try again
successFlash: 'Welcome!',
failureFlash: 'Logging in didn\'t work. You need to use an `@nouse.co.uk` address.',
}));
// session serialization
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (user, done) {
done(null, user);
});
// database setup
fs.open(config.dbfile, 'r', function (err) {
if (err) {
console.log('Creating DB file.');
fs.open(config.dbfile, 'w', function (err) {
if (!err) {
var db = new sqlite3.Database(config.dbfile);
db.run('CREATE TABLE Fixtures (id INTEGER PRIMARY KEY, tournament INTEGER, sport TEXT, name TEXT, day TEXT, time DATETIME, location TEXT, pointsAvailable FLOAT, home TEXT, homeScore FLOAT, awayScore FLOAT, away TEXT, inProgress INTEGER DEFAULT 0);');
db.run('CREATE TABLE Tournaments (id INTEGER PRIMARY KEY, name TEXT, home TEXT, away TEXT);');
db.close();
}
});
}
});
// server
var server = http.createServer(app);
server.listen(app.get('port'));
var io = require('socket.io').listen(server);
// URLs
app.get('/', function (req, res) {
if (req.isAuthenticated()) {
res.redirect('/tournaments');
} else {
res.render('admin-index', { messages: req.flash() });
}
});
app.get('/tournaments', isLoggedIn, routes.tournaments);
app.get('/tournaments/(:id).html', routes.fixturesHTML);
app.get('/tournaments/(:id).json', routes.fixturesJSON);
app.get('/tournaments/(:id)/totals.json', routes.totalsJSON);
app.get('/tournaments/(:id)', isLoggedIn, routes.tournament);
app.get('/tournaments/(:id)/add', isLoggedIn, routes.fixturesAdd);
app.post('/tournaments/:id/update', isLoggedIn, routes.fixturesUpdate(io));
app.get('/logout', function (req, res) {
req.logout();
res.redirect('/');
});
app.post('/tournaments/add', isLoggedIn, routes.tournamentsAdd);