-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
119 lines (106 loc) · 3.31 KB
/
handler.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
var http = require('http');
var fs = require('fs');
var app = require('./helpers.js');
var client = require('./redis.js');
var port = process.env.PORT || 8000;
var index = fs.readFileSync(__dirname + '/public/index.html');
function handler(req, res) {
var url = req.url;
console.log(req.method);
console.log(url);
if (url === '/') {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(index);
} else if (url.indexOf('.html') > -1 || url.indexOf('.css') > -1 || url.indexOf('.js') > -1 || url.indexOf('.ico') > -1) {
var ext = url.split('.')[1];
var file = fs.readFileSync(__dirname + url);
res.writeHead(200, {'Content-Type': 'text/' + ext});
res.end(file);
} else if (url === '/riddle' || url.indexOf('/newriddle') > -1) {
getRandomRiddle(function(err, obj) {
if (err) {
console.log(err);
} else {
console.log(obj);
res.end(JSON.stringify(obj));
}
});
} else if (url === '/auth'){
app.authHandler(req,res);
} else if (req.method === 'POST') {
var postRiddle = (url.split('/')[1]).replace(/%20/g, ' ');
var postAnswer = (url.split('/')[2]).replace(/%20/g, ' ');
app.validate(req,res, riddleAddAndRespond);
function riddleAddAndRespond(res){
addToDb(postRiddle, postAnswer, function(err, reply) {
if (err) {
console.log(err);
} else {
res.end(reply);
}
});
}
} else if (url.indexOf('/answer') > -1) {
var riddle = (url.split('/')[2]).replace(/%20/g, ' ');
getAnswer(riddle, function (err, reply) {
if (err) {
console.log(err);
} else {
res.end(JSON.stringify(reply.answer));
}
});
}
}
function create() {
var server = http.createServer(handler).listen(port);
console.log('Server running at http://' + port);
var io = require('socket.io')(server);
io.on('connection', manageConnection);
function manageConnection(socket){
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
socket.on('chat message in', function(msg){
console.log('message>>>>>>>', msg);
io.emit('chat message out', msg);
});
}
}
function addToDb(riddle, answer, callback) {
client.INCR('riddlecount', function(err, riddlecount) {
if (err) {
console.log(err);
} else {
client.HMSET(riddlecount, 'riddle', riddle, 'answer', answer, callback);
}
});
}
function getRandomRiddle(callback) {
client.GET('riddlecount', function(err, reply) {
var randomNumber = Math.floor(Math.random() * (reply - 2)) + 1;
client.HGET(randomNumber, 'riddle', function(err, data) {
if (err) {
console.log(err);
} else {
var response = {
ID: randomNumber,
riddle: data
};
callback(err, response);
}
});
});
}
function getAnswer(riddle, callback) {
client.HGETALL(riddle, callback);
}
module.exports = {
handler: handler,
create: create,
client: client,
getAnswer : getAnswer,
getRandomRiddle : getRandomRiddle,
addToDb : addToDb,
// manageConnection : manageConnection
};