-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathserver.js
More file actions
83 lines (70 loc) · 2.19 KB
/
Copy pathserver.js
File metadata and controls
83 lines (70 loc) · 2.19 KB
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
var concat = require('concat-stream')
var Router = require('routes-router')
var ecstatic = require('ecstatic')
var redis = require('redis')
module.exports = function(opts){
console.log('using redis server')
var port = opts.redis_port || process.env.USE_REDIS_PORT || 6379
var host = opts.redis_host || process.env.USE_REDIS_HOST || 'redis'
var connectionStatus = false
var client = redis.createClient(port, host, {})
client.on('error', function(err){
connectionStatus = false
console.log('Error from the redis connection:')
console.log(err)
})
client.on('end', function(err){
connectionStatus = false
console.log('Lost connection to Redis server')
})
client.on('ready', function(err){
connectionStatus = true
console.log('Connection made to the Redis server')
})
console.log('-------------------------------------------');
console.log('have host: ' + host)
console.log('have port: ' + port)
var router = Router()
var fileServer = ecstatic({ root: __dirname + '/client' })
router.addRoute("/v1/ping", {
// API to provide db connection status
GET: function(req, res){
res.setHeader('Content-type', 'application/json')
res.end(JSON.stringify({
connected:connectionStatus
}))
}
})
router.addRoute("/v1/logos", {
// API to get all logo coordinates
GET: function (req, res) {
client.lrange('logos', 0, -1, function(err, data){
res.setHeader('Content-type', 'application/json')
res.end(JSON.stringify(data))
})
},
// API to add a new logo coordinate
POST: function (req, res) {
req.pipe(concat(function(data){
data = data.toString()
client.rpush('logos', data, function(){
client.save(function(){
res.end('ok')
})
})
}))
}
})
router.addRoute("/v1/reset", {
// API to reset and purge all logo coordinates
PUT: function (req, res) {
client.del('logos', function(err, data){
res.setHeader('Content-type', 'application/json')
res.end(JSON.stringify(data))
})
}
})
// API to handle serving of all other content
router.addRoute("/*", fileServer)
return router
}