forked from OpenDroneMap/ClusterODM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adminweb.js
121 lines (107 loc) · 3.58 KB
/
adminweb.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
118
119
120
121
/**
* ClusterODM - A reverse proxy, load balancer and task tracker for NodeODM
* Copyright (C) 2018-present MasseranoLabs LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const logger = require("./libs/logger");
const express = require("express");
const basicAuth = require("express-basic-auth");
const nodes = require("./libs/nodes");
const package_info = require("./package_info");
const cors = require("cors");
const netutils = require("./libs/netutils");
const asrProvider = require("./libs/asrProvider");
module.exports = {
create: function (options) {
logger.info("Starting admin web interface on " + options.port);
const app = express();
app.use(express.json());
app.use(cors());
app.get("/signout", (req, res) => {
res.status(401).send('Signed out. <br /> <a href="/">Sign back in</a>');
});
if (!options.password) {
logger.warn(`No admin password specified, make sure port ${options.port} is secured`);
} else {
app.use(
basicAuth({
users: { admin: options.password },
challenge: true,
realm: "ClusterODM",
})
);
}
app.use(express.static("public"));
app.use(express.json());
// API
app.get("/r/info", (req, res) => {
const { name, version } = package_info;
res.json({ name, version });
});
app.get("/r/node/list", (req, res) => {
const list = nodes.all();
res.json(list.map((node) => nodeToJson(node)));
});
app.delete("/r/node", async (req, res) => {
const { number } = req.body;
if (number) {
const isSuccess = await netutils.removeAndCleanupNode(nodes.nth(number), asrProvider.get());
res.status(200).json(isSuccess);
} else {
res.status(403).send();
}
});
app.post("/r/node/unlock", (req, res) => {
const { number } = req.body;
if (number) {
const isSuccess = nodes.unlock(nodes.nth(number));
res.status(200).json(isSuccess);
} else {
res.status(403).send();
}
});
app.post("/r/node/lock", (req, res) => {
const { number } = req.body;
if (number) {
const isSuccess = nodes.lock(nodes.nth(number));
res.status(200).json(isSuccess);
} else {
res.status(403).send();
}
});
app.post("/r/node/add", (req, res) => {
const { hostname, port, token } = req.body;
const node = nodes.addUnique(hostname, port, token);
if (node) {
node.updateInfo();
res.send({ success: true });
} else {
res.send({ error: "Invalid" });
}
});
app.listen(options.port);
},
};
const nodeToJson = (node) => ({
name: node.toString(),
isLocked: node.isLocked(),
isAutoSpawned: node.isAutoSpawned(),
isOnline: node.isOnline(),
getTaskQueueCount: node.getTaskQueueCount(),
getMaxParallelTasks: node.getMaxParallelTasks(),
getEngineInfo: node.getEngineInfo(),
getVersion: node.getVersion(),
nodeData: node.nodeData,
});