-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
65 lines (52 loc) · 1.7 KB
/
cli.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
const fs = require('fs');
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers')
const RaftRunnerService = require('./src/RaftRunnerService');
const argv = yargs(hideBin(process.argv)).argv
const arg_path = argv.path || ".raft/data";
const arg_id = argv.id || "id1";
const arg_port = argv.port || 8047;
const arg_peers = argv.peers ? JSON.parse(argv.peers) : undefined
const ipAddress = argv.ip ? argv.ip : undefined
console.log('peers = ',arg_peers);
console.log('port = '+arg_port);
console.log('id = '+arg_id);
fs.mkdirSync('.db', { recursive: true });
const raftRunnerService = new RaftRunnerService({
id: arg_id,
path: arg_path,
port: arg_port,
peers: arg_peers,
ipAddress: ipAddress,
fileName:'.db/sqlite_'+arg_port+'.db',
stateChangeCallback: (state) => {
console.log('--- stateChangeCallback: ', state)
},
raftStateCallback: (state) => {
console.log('--- raftStateCallback: ', state)
}
})
function createExpressHandler() {
this.app = require('express')()
const bodyParser = require('body-parser');
this.app.use(bodyParser.json());
this.app.use(bodyParser.raw());
this.app.post('/query', (req, res) => {
const query = req.body.q
if (query) {
raftRunnerService.queryHandler(query).then((result) => {
res.send(result)
}).catch((err) => {
res.send(err)
})
} else {
console.log('no q query param')
res.send('no q query param')
}
})
const port = arg_port + 3
this.app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
}
createExpressHandler()