-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
130 lines (110 loc) · 2.93 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* trainsharingApp - 2012
*
*/
/* LOAD LIBRARIES ---------- */
var express = require('express'),
mysql = require('mysql'),
moment = require('moment'),
util = require('util'),
events = require('events').EventEmitter,
Worker = require('./lib/worker'),
config = require('./config');
// Initialize EventEmitter based Worker
util.inherits(Worker, events);
var worker = new Worker(util, events);
worker.init();
// Start Webserver & API
var app = express.createServer();
// Configure Express
app.configure(function(){
app.use(express.bodyParser());
var oneYear = 31557600000;
app.use(express.static(__dirname + '/static', {maxAge: oneYear}));
});
// Instantiate MySQL client
var client = mysql.createClient({
user: config.mysql.user,
password: config.mysql.password,
database: config.mysql.database,
port: config.mysql.port,
host: config.mysql.host
});
/* MAKE SURE ALL THE TABLES ARE THERE ----------- */
// routes
client.query(
'SHOW TABLES LIKE "routes"',
function(err, results, fields){
if(results.length > 0){
console.log('table "routes" exists. ok.');
}else if(err){
console.log(err);
}else{
throw new Error('table "routes" does not exist.');
}
});
// users
client.query(
'SHOW TABLES LIKE "users"',
function(err, results, fields){
if(results.length > 0){
console.log('table "users" exists. ok.');
}else if(err){
console.log(err);
}else{
throw new Error('table "users" does not exist.');
}
});
// routes_users
client.query(
'SHOW TABLES LIKE "routes_users"',
function(err, results, fields){
if(results.length > 0){
console.log('table "routes_users" exists. ok.');
}else if(err){
console.log(err);
}else{
throw new Error('table "routes_users" does not exist.');
}
});
/* ROUTES ------------- */
app.get('/', function(req, res){
res.sendfile('static/index.html');
});
app.get('/mysql_test', function(req, res){
client.query(
'SELECT * FROM routes limit 10',
function(err, results, fields){
if(err){
console.log('an error occurred');
}
res.send(results);
}
);
});
app.post('/v1/login', function(req, res){
worker.login({
request: req,
response: res,
mysql: client,
moment: moment
});
});
app.post('/v1/checkin', function(req, res){
worker.checkin({
request: req,
response: res,
mysql: client,
moment: moment
});
});
app.post('/v1/read', function(req, res){
worker.read({
request: req,
response: res,
mysql: client,
moment: moment
});
});
app.listen(process.env.PORT || 5000);
console.log('trainsharing server running on %s:%d.', app.address().address, app.address().port);