-
Notifications
You must be signed in to change notification settings - Fork 1
/
keyvaluestore.js
executable file
·47 lines (36 loc) · 1.2 KB
/
keyvaluestore.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
const express = require('express')
var bodyParser = require('body-parser');
const app = express()
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
const port = process.env.MICRO_KEYVALUESTORE_PORT || process.env.PORT || 9000;
var keyValueStore = {}
keyValueStore['masterendpoint'] = process.env.MICRO_MASTER_ENDPOINT;
keyValueStore['dbendpoint'] = process.env.MICRO_DB_ENDPOINT;
keyValueStore['storageendpoint'] = process.env.MICRO_STORAGE_ENDPOINT;
app.get('/:key/', (req, res) => {
var key = req.params.key
if (key in keyValueStore) {
res.send(keyValueStore[key])
} else {
res.status(404).send("Key not found")
}
})
app.post('/:key/', function(req, res) {
var key = req.params.key
var value = req.body.value
console.log(key + "=" + value)
if (!keyValueStore[key]) {
keyValueStore[key] = value;
} else {
console.log('Prevented updating keyvaluestore, key = ' + key, ', value = ' + value);
}
res.send('')
})
app.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(`server is listening on ${port}`)
})
console.log("Running keyvaluestore on port: ", port);