-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuckler.js
57 lines (49 loc) · 1.51 KB
/
buckler.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
var restify = require('restify')
var server = restify.createServer()
var config = require('./config')
var allowedClients = require('./allowedClients')
var client = restify.createJsonClient({
url: config.elasticsearch
})
server.use(restify.authorizationParser())
server.use(function(req, res, next){
var deny = function(){
res.json(401, {"error": "Unauthorized client"})
}
if(!req.authorization || !req.authorization.basic)
deny()
else
var user = req.authorization.basic.username
var password = req.authorization.basic.password
if(
allowedClients[user] &&
allowedClients[user] == password
){
next()
} else {
deny()
}
})
server.use(restify.bodyParser())
server.use(function(req, res, next){
var path = req.url
var method = req.method.toLowerCase()
if(method == 'delete')
method = 'del'
var body = req.body
var callback = function(err, request, response, obj){
var statusCode = response.statusCode
res.json(statusCode, obj || {})
}
if(method == 'post' || method == 'put'){
client[method](path, body, callback)
} else {
client[method](path, callback)
}
})
server.get(/^\/([a-zA-Z0-9_\.~-]+)\/(.*)/,()=>{})
server.post(/^\/([a-zA-Z0-9_\.~-]+)\/(.*)/,()=>{})
server.del(/^\/([a-zA-Z0-9_\.~-]+)\/(.*)/,()=>{})
server.put(/^\/([a-zA-Z0-9_\.~-]+)\/(.*)/,()=>{})
server.head(/^\/([a-zA-Z0-9_\.~-]+)\/(.*)/,()=>{})
server.listen(config.port)