-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrest-interface.js
87 lines (75 loc) · 2.32 KB
/
rest-interface.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
/**
* New node file
*/
var txHandler = require('./transaction-handler');
var restify = require('restify');
var config = require("./config");
function respondWithoutTrx(req, res, next) {
console.log(req.body.commandType);
var cmd = req.body;
var txInstance = txHandler.getInstance()
if (cmd.commandType == 'openTransaction') {
txInstance.openTransaction(function() {
var response = initializeSuccesfulResponse();
response.txId = this.getTransactionId() + '';
res.send(JSON.stringify(response));
console.log('resp : %s', JSON.stringify(response));
next();
},
function(err) {
var response = initializeErrorResponse(err, 1);
res.send(JSON.stringify(response));
next();
});
} else if (cmd.commandType == 'commitTransaction') {
txInstance.commitTransaction(cmd.txId, function() {
var response = initializeSuccesfulResponse();
res.send(JSON.stringify(response));
next();
},function(err) {
var response = initializeErrorResponse(err, 1);
res.send(JSON.stringify(response));
next();
});
} else if (cmd.commandType == 'rollbackTransaction') {
txInstance.rollbackTransaction(cmd.txId, function() {
var response = initializeSuccesfulResponse();
res.send(JSON.stringify(response));
next();
},function(err) {
var response = initializeErrorResponse(err, 1);
res.send(JSON.stringify(response));
next();
});
} else if (cmd.commandType == 'execute') {
txInstance.execute(cmd.cql, cmd.txId, function() {
var response = initializeSuccesfulResponse();
res.send(JSON.stringify(response));
next();
},function(err) {
var response = initializeErrorResponse(err, 1);
res.send(JSON.stringify(response));
next();
});
}
}
function initRestServer(){
var server = restify.createServer();
server.use(restify.acceptParser(server.acceptable));
server.use(restify.bodyParser());
server.use(restify.queryParser());
server.get('/cqltx', respondWithoutTrx);
server.post('/cqltx', respondWithoutTrx);
server.listen(config.restPort, function() {
console.log('%s listening at %s', server.name, server.url);
});
}
function initializeSuccesfulResponse(){
return {status:0,responseCode:0,description:"OK"};
}
function initializeErrorResponse(err,code){
return {status:1,responseCode:code,description:err.message,stack:err.stack};
}
module.exports = {
initRestServer : initRestServer
}