forked from votem/mongo-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·81 lines (69 loc) · 2.45 KB
/
server.js
File metadata and controls
executable file
·81 lines (69 loc) · 2.45 KB
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
#!/usr/bin/env node
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.json()); // for parsing application/json
const MongoStream = require('./mongo-stream');
let mongoStream;
const config = require('./configParser');
// returns the status of all collectionManagers currently running
app.get('/', (request, response) => {
const collectionManagers = Object.values(mongoStream.collectionManagers);
const responseBody = { total: collectionManagers.length };
collectionManagers.forEach(manager => {
if (manager) {
responseBody[manager.collection] = 'Listening';
} else {
responseBody[manager.collection] = 'Not Listening';
}
});
response.send(responseBody);
});
app.post('/collection-manager?', (request, response) => {
console.log(request.body);
const collections = request.body.collections;
const managerOptions = {
dump: request.body.dump,
ignoreResumeTokens: request.body.ignoreResumeTokens,
watch: request.body.watch
};
return mongoStream.addCollectionManager(collections, managerOptions)
.then((results) => {
response.send(results);
}).catch(err => {
console.log('Error posting collection-manager', err);
response.send(err);
});
});
// manually set the bulk size for replication testing
app.put('/bulk=:bulkSize', (request, response) => {
response.send(`bulk size set from ${mongoStream.elasticManager.bulkSize} to ${request.params.bulkSize}`);
mongoStream.elasticManager.bulkSize = Number(request.params.bulkSize);
});
// triggers a remove for the specified collections
app.delete('/collection-manager/:collections?', (request, response) => {
const collections = request.params.collections.split(',');
console.log('Deleting collections:', collections);
return mongoStream.removeCollectionManager(collections)
.then(results => {
console.log('Remaining collections after Delete:', results);
response.send(results);
}).catch(err => {
response.send(err);
});
});
app.listen(config.adminPort, (err) => {
if (err) {
return console.log(`Error listening on ${config.adminPort}: `, err)
}
MongoStream.init(config)
.then((stream) => {
console.log('connected');
mongoStream = stream;
})
.catch((err) => {
console.log(`Error Creating MongoStream: ${err.message}`);
process.exit();
});
console.log(`server is listening on port ${config.adminPort}`);
});