-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
170 lines (154 loc) · 5.38 KB
/
app.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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
'use strict';
const antidote = require('antidote_ts_client');
const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const { spawn } = require('child_process');
const compression = require('compression');
const helmet = require('helmet');
const conf = require('./config');
const DEBUG = true;
function log(...args) {
if (DEBUG) {
console.log(...args);
}
}
const app = express();
app.use(helmet());
app.use(compression()); // Compress all routes
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
const viewPath = __dirname + '/views/';
// Cache of partition info
// TODO change this if the web server is replicated
let partitionInfo = new Map();
for (let i = 1; i <= conf.antidote.length; i++) {
partitionInfo.set(i, true);
}
// Initialize Antidote clients
let atdClis = [];
for (let i in conf.antidote) {
atdClis.push(antidote.connect(conf.antidote[i].port, conf.antidote[i].host));
}
/* Static web page routing. */
const staticRouter = express.Router();
staticRouter.get('/', function (req, res, next) {
res.sendFile(viewPath + 'index.html');
});
app.use("/", staticRouter);
/* API routing. */
const apiRouter = express.Router();
// Set API
apiRouter.route('/:rep_id/set/:set_id')
.get(function (req, res) {
let repId = parseInt(req.params.rep_id);
let setId = req.params. set_id;
atdClis[repId-1].set(setId).read().then(content => {
log('Get', setId, 'from replica', repId);
res.json({ status: 'OK', cont: content });
});
})
.put(function (req, res) {
let repId = parseInt(req.params.rep_id);
let setId = req.params.set_id;
let value = req.body.value;
atdClis[repId-1].update(
atdClis[repId-1].set(setId).add(value)
).then(resp => {
log('Add', value, 'to', setId, 'on replica', repId)
res.json({ status: 'OK' });
});
})
.delete(function (req, res) {
let repId = parseInt(req.params.rep_id);
let setId = req.params.set_id;
let value = req.body.value;
atdClis[repId-1].update(
atdClis[repId-1].set(setId).remove(value)
).then(resp => {
log('Remove', value, 'from', setId, 'on replica', repId)
res.json({ status: 'OK' });
});
});
// Counter API
apiRouter.route('/:rep_id/count/:counter_id')
.get(function (req, res) {
let repId = parseInt(req.params.rep_id);
let counterId = req.params.counter_id;
atdClis[repId-1].counter(counterId).read().then(content => {
log('Get', counterId, 'from replica', repId);
res.json({ status: 'OK', cont: content });
});
})
.put(function (req, res) {
let repId = parseInt(req.params.rep_id);
let counterId = req.params.counter_id;
atdClis[repId-1].update(
atdClis[repId-1].counter(counterId).increment(1)
).then(resp => {
log('Increment', counterId, 'on replica', repId)
res.json({ status: 'OK' });
});
})
.delete(function (req, res) {
let repId = parseInt(req.params.rep_id);
let counterId = req.params.counter_id;
atdClis[repId-1].update(
atdClis[repId-1].counter(counterId).increment(-1)
).then(resp => {
log('Decrement', counterId, 'on replica', repId)
res.json({ status: 'OK' });
});
});
// Network partition API
apiRouter.route('/:rep_id/part')
.get(function (req, res) {
let repId = parseInt(req.params.rep_id);
let value = partitionInfo.get(repId) ? 'ON' : 'OFF';
res.json({ status: value, rep: repId });
})
.put(function (req, res) {
let repId = parseInt(req.params.rep_id);
if (!partitionInfo.get(repId)) {
log('Partition replica', repId, 'already set');
res.json({ status: 'OK', rep: repId });
} else {
spawn(conf.partitionCmd, ['create', repId])
.on('exit', function (code) {
if (code === 0) {
log('Partition replica', repId);
partitionInfo.set(repId, false);
res.json({ status: 'OK', rep: repId });
}
});
}
})
.delete(function (req, res) {
let repId = parseInt(req.params.rep_id);
if (partitionInfo.get(repId)) {
log('Partition replica', repId, 'already removed');
res.json({ status: 'OK', rep: repId });
} else {
spawn(conf.partitionCmd, ['remove', repId])
.on('exit', function (code) {
if (code === 0) {
log('Remove partition over replica', repId);
partitionInfo.set(repId, true);
res.json({ status: 'OK', rep: repId });
}
});
}
});
app.use("/api", apiRouter);
/* Else, 404-error routing. */
app.use("*", function (req, res) {
res.sendFile(viewPath + "404.html");
});
module.exports = app;