-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
117 lines (99 loc) · 3.49 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
var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var _ = require('underscore');
var app = express();
app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'web')));
var System = require('cassandra-client').System;
/** gets keyspace information */
System.prototype.describeKeyspaces = function(callback) {
this.q.put(function(con) {
con.thriftCli.describe_keyspaces(callback);
});
this.emit('checkq');
};
app.get('/api/:ks/:cf', function(req, res) {
var PooledConnection = require('cassandra-client').PooledConnection;
var hosts = [];
console.log(req.query.ip);
hosts.push(req.query.ip);
var ks = req.params.ks;
var connection_pool = new PooledConnection({'hosts': hosts, 'keyspace': ks});
// Reading
var cf = req.params.cf;
connection_pool.execute('SELECT * FROM ?', [cf],
function(err, rows) {
if (err) {
console.log(err);
} else {
for (var i = 0; i < rows.length; i++) {
console.log(rows[i]);
for (var key in rows[i].colHash) {
var value = rows[i].colHash[key];
rows[i].colHash[key] = value instanceof Buffer? value.toString(): value;
value = rows[i].colHash[key];
try {
if (value != '{}') {
rows[i].colHash[key] = JSON.parse(value);
if (typeof rows[i].colHash[key] === 'object') {
var arr = _.pairs(rows[i].colHash[key]).sort();
var newObj = {};
arr.forEach(function (kv) {
newObj[kv[0]]=kv[1];
});
rows[i].colHash[key] = newObj;}
}
}
catch (e) {
rows[i].colHash[key] = value;
}
}
rows[i].key = rows[i].key.toString();
delete rows[i].cols;
}
}
res.end(JSON.stringify(rows));
connection_pool.shutdown(function() { console.log("connection pool shutdown"); });
}
);
});
app.get('/api/ks', function(req, res) {
console.log(req.query.ip);
var sys = new System(req.query.ip);
sys.describeKeyspaces(function(err, ksDefs) {
if (err) {
console.log(err);
} else {
res.end(JSON.stringify(ksDefs));
}
sys.close();
});
});
/// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
/// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.send(500, 'Something broken!\n' + err.stack);
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.send(500, 'Something broken!\n' + err.message);
});
module.exports = app;