-
Notifications
You must be signed in to change notification settings - Fork 8
/
api.js
58 lines (56 loc) · 1.77 KB
/
api.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
var Hapi = require('hapi')
var chalk = require('chalk')
var rainbow = require('ansi-rainbow')
var utils = require('./utils.js')
var routes = [
{
method: 'GET',
path: '/',
handler: function (request, reply) {
request.store.list(function (err, all_records) {
if (err) console.log('NEED ERROR HANDLING! GAAH!')
reply(all_records)
})
}
},
{
method: 'PUT',
path: '/{name}',
handler: function (request, reply) {
var obj = utils.wrapDefaults(encodeURIComponent(request.params.name), request.payload, request.argv)
request.store.set(obj.name, obj.payload, function (err, set_value) {
if (err) console.log('NEED ERROR HANDLING! GAAH!')
reply('OK.');
})
}
},
{
method: 'DELETE',
path: '/{name}',
handler: function (request, reply) {
var obj = utils.wrapDefaults(encodeURIComponent(request.params.name), request.payload, request.argv)
request.store.del(obj.name, function (err, set_value) {
if (err) console.log('NEED ERROR HANDLING! GAAH!')
reply(obj.name+' removed.');
})
}
}
]
module.exports = function (argv,store) {
var server = new Hapi.Server(argv.apihost, argv.apiport)
routes.forEach(function (route) {
server.route(route)
})
server.ext('onRequest', function (request, next) {
request.store = store
request.argv = argv
next()
})
server.realStart = server.start
server.start = function () {
this.realStart(function () {
utils.displayServiceStatus('api',server.info.uri, true)
})
}
return server
}