-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsports_app.js
175 lines (140 loc) · 3.85 KB
/
sports_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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
var morgan = require('morgan');
var bodyParser = require('body-parser');
var express = require('express');
var needle = require('needle');
var url = require('url');
var datetime = require('node-datetime');
var pug = require('pug');
var path = require('path');
app = express();// make app global
require( './App.js' );
require( './Actions.js' );
require( './Routes.js' );
var FormData = require('form-data');
App.mysql = require('mysql');
var http = require('http');
var qs = require('querystring');
var fs = require('fs');
var log = console;
var requests = {};
var reports_requested = {};
UUID = require('node-uuid');
UUID.generate = UUID.v4;
// parse application/json
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// use morgan to log requests to the console
app.use(morgan('dev'));
var sessions = [];
app.roles = {};
fs.readFile('./database.json', 'utf8', function (err,data)
{
if (err)
{
return console.log(err);
}
data = JSON.parse(data).dev;
App.db.pool = App.mysql.createPool(
{
connectionLimit : 100,
host : data.host,
user : data.user,
password : data.password,
database : data.database,
timezone : 'utc',
dateStrings : 'date'
});
App.db.connect_db( function ()
{
} );
var server = app.listen( data.host_port, '0.0.0.0', function ()
{
console.log( 'server app listening on port ', data.host_port );
});
server.timeout = 60000 * 60 * 3;
});
app.all('/*', function(req, res, next)
{
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
app.use( express.static('public/Sports_app') );
app.post('/login', function (req, res)
{
console.log( 'LOGIN', req.body );
var login_form =
{
username: req.body.username,
password: req.body.password,
register: req.body.register
}
if( login_form.register == 'register' )
{
register( login_form, res );
}
else
{
login( login_form, res );
}
});
app.post( '/*get_tournaments',function ( req, res )
{
var user_uuid = req.body.user_uuid;
console.log( 'Get tournaments' );
app.get_tournaments( user_uuid, function ( tournaments )
{
res.send( { success: true, tournaments: tournaments } );
});
});
var login = function ( login_form, res )
{
App.db.select( 'User', { username: login_form.username, password: login_form.password }, function ( results )
{
if( results.results.length > 0 )
{
var session = UUID.generate();
var role = results.results[0].role;
sessions.push( session );
app.add_routes( session );
if( role == 'admin')
{
app.roles[session] = 'admin';
app.get_all_users( function ( users )
{
res.send( { success: true, sessid: session, user_uuid: results.results[0].uuid, role: role, users: users } );
});
}
else
{
res.send( { success: true, sessid: session, user_uuid: results.results[0].uuid, role: role, users: [results.results[0]] } );
}
}
else
{
res.send( { success: false, sessid: null, user_uuid: null, error: 'Invalid user !!!' } );
}
});
};
var register = function ( login_form, res )
{
console.log('REGISTER FIRED');
var user = new App.User( { username: login_form.username, password: login_form.password, role: 'user', date_created: App.u.date_now() } );
user.insert();
App.db.select( 'User', { username: login_form.username }, function ( results )
{
if( results.results.length < 1 )
{
App.db.persist( function ( err )
{
login( login_form, res );
});
}
else
{
res.send( { success: false, error: 'USERNAME TAKEN!!!!'} );
}
});
};
console.log( App.u.date_now() );