-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
109 lines (98 loc) · 2.74 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
101
102
103
104
105
106
107
108
109
import express from 'express';
import ViteExpress from 'vite-express';
import bodyParser from 'body-parser';
import fs from 'fs';
const saveServers = (srv) => {
fs.writeFileSync('servers.json', JSON.stringify(srv, null, 2));
};
const loadServers = () => {
if (fs.existsSync('servers.json')) {
return JSON.parse(fs.readFileSync('servers.json', 'utf8'));
}
return {};
};
const fetchAllReplicators = (knownServers) =>
new Promise((resolve, reject) => {
const suffix = '/_replicator/_all_docs?include_docs=true';
const allServerPromises = [];
// Go and fetch replication from all servers
for (const key of Object.keys(servers)) {
const srv = servers[key];
const options = {
method: 'GET',
cache: 'no-cache',
redirect: 'error',
headers: {
Authorization: getAuthorization(srv)
}
};
const target = srv.url + suffix;
const p = fetch(target, options).then((response) => response.json());
allServerPromises.push(p);
}
processReplicatorResults(allServerPromises, resolve, reject);
});
const processReplicatorResults = (allServerPromises, resolve, reject) => {
// Process the results
let result = [];
Promise.allSettled(allServerPromises)
.then((results) => {
results.forEach((p) => {
if (p.status === 'fulfilled') {
// We got data
const rows = p.value.rows;
if (rows) {
for (const row of rows) {
if (row.doc.source) {
let resultRow = {
source: row.doc.source.url,
target: row.doc.target.url
};
result.push(resultRow);
}
}
}
} else {
// Something didn't add up
console.error(p);
}
});
resolve(result);
})
.catch((e) => reject(e));
};
// Basic or Bearer Auth
const getAuthorization = (srv) => {
if (srv.auth) {
return `Bearer ${srv.auth}`;
}
let basic = Buffer.from(`${srv.user}:${srv.pwd}`);
return `Basic ${basic.toString('base64')}`;
};
const servers = loadServers();
const app = express();
app.use(express.static('public'));
app.use(bodyParser.json());
app.get('/api/data', (_req, res) => {
fetchAllReplicators(servers)
.then((json) => res.json(json))
.catch((err) => {
console.error(err);
res.status(500).json({ error: err.message });
});
});
app.post('/api/server', (req, res) => {
let body = req.body;
if (body.url) {
servers[body.url] = body;
saveServers(servers);
}
res.json(servers);
});
app.get('/api/server', (_req, res) => {
res.json(servers);
});
// Run the server
ViteExpress.listen(app, 3000, () => {
console.log('Server up and running on 3000');
});