-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
101 lines (90 loc) · 3.21 KB
/
server.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
var express = require('express');
var bodyParser = require('body-parser');
var cassandra = require('cassandra-driver');
var async = require("async");
var upsertSong = 'INSERT INTO simplex.songs (id, title, album, artist, tags, data) '
+ 'VALUES(?, ?, ?, ?, ?, ?);';
var getSongById = 'SELECT * FROM simplex.songs WHERE id = ?;';
var getFlightById = 'SELECT * FROM demo.flights_load WHERE id = ?;';
var client = new cassandra.Client( { contactPoints : [ '127.0.0.1' ] } );
client.connect(function(err, result) {
console.log('Connected.');
});
var app = express();
app.use(bodyParser.json());
app.set('json spaces', 2);
app.get('/metadata', function(req, res) {
res.send(client.hosts.slice(0).map(function (node) {
return { address : node.address, rack : node.rack, datacenter : node.datacenter }
}));
});
app.post('/keyspace', function(req, res) {
client.execute("CREATE KEYSPACE IF NOT EXISTS simplex WITH replication " +
"= {'class' : 'SimpleStrategy', 'replication_factor' : 3};",
afterExecution('Error: ', 'Keyspace created.', res));
});
app.post('/tables', function(req, res) {
async.parallel([
function(next) {
client.execute('CREATE TABLE IF NOT EXISTS simplex.songs (' +
'id uuid PRIMARY KEY,' +
'title text,' +
'album text,' +
'artist text,' +
'tags set<text>,' +
'data blob' +
');',
next);
},
function(next) {
client.execute('CREATE TABLE IF NOT EXISTS simplex.playlists (' +
'id uuid,' +
'title text,' +
'album text,' +
'artist text,' +
'song_id uuid,' +
'PRIMARY KEY (id, title, album, artist)' +
');',
next);
}
], afterExecution('Error: ', 'Tables created.' , res));
});
app.post('/song', function(req, res) {
var id = null;
if ( ! req.body.hasOwnProperty('id')) {
id = cassandra.types.uuid();
} else {
id = req.body.id;
}
client.execute(upsertSong,
[id, req.body.title, req.body.album, req.body.artist, req.body.tags, null],
afterExecution('Error: ', 'Song ' + req.body.title + ' upserted.', res));
});
app.get('/song/:id', function(req, res) {
client.execute(getSongById, [ req.params.id ], function(err, result) {
if (err) {
res.status(404).send({ msg : 'Song not found.' });
} else {
res.json(result); }
});
});
app.get('/FlightById/:id', function(req, res) {
client.execute(getFlightById, [ req.params.id ], { hints : ['int'] }, function(err, result) {
if (err) {
res.status(404).send({ msg : 'Flight not found. '});
} else {
res.json(result); }
});
});
function afterExecution(errorMsessage, successMessage) {
return function(err, result) {
if (err) {
return console.log(errorMessage);
} else {
return console.log(successMessage);
}
}
}
var server = app.listen(3000, function() {
console.log('Listening on port %d', server.address().port);
});