aboutsummaryrefslogtreecommitdiffstats
path: root/vnfmarket/src/main/webapp/vnfmarket/node_modules/http-proxy/examples/helpers/store.js
blob: e2860573f8bcaceafa8dad6cb02c76928de46924 (plain)
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
//
// just to make these example a little bit interesting, 
// make a little key value store with an http interface
// (see couchbd for a grown-up version of this)
//
// API:
// GET / 
// retrive list of keys
//
// GET /[url]
// retrive object stored at [url]
// will respond with 404 if there is nothing stored at [url]
//
// POST /[url]
// 
// JSON.parse the body and store it under [url]
// will respond 400 (bad request) if body is not valid json.
//
// TODO: cached map-reduce views and auto-magic sharding.
//
var Store = module.exports = function Store () {
  this.store = {};
};

Store.prototype = {
  get: function (key) {
    return this.store[key]
  },
  set: function (key, value) {
    return this.store[key] = value
  },
  handler:function () {
    var store = this
    return function (req, res) {
      function send (obj, status) {
        res.writeHead(200 || status,{'Content-Type': 'application/json'})
        res.write(JSON.stringify(obj) + '\n')
        res.end()
      }
      var url = req.url.split('?').shift()
      if (url === '/') {
        console.log('get index')
        return send(Object.keys(store.store))
      } else if (req.method == 'GET') {
        var obj = store.get (url)
        send(obj || {error: 'not_found', url: url}, obj ? 200 : 404)
      } else {
        //post: buffer body, and parse.
        var body = '', obj
        req.on('data', function (c) { body += c})
        req.on('end', function (c) {
          try {
            obj = JSON.parse(body)
          } catch (err) {
            return send (err, 400)
          }
          store.set(url, obj)
          send({ok: true})
        })
      } 
    }
  }
}