zero-ui/backend/routes/network.js

91 lines
2.6 KiB
JavaScript
Raw Normal View History

2023-10-07 04:11:40 +08:00
import express from "express";
2021-03-22 03:25:13 +08:00
const router = express.Router();
import * as auth from "../services/auth.js";
import * as network from "../services/network.js";
2021-03-22 03:25:13 +08:00
2023-10-07 04:11:40 +08:00
import { api } from "../utils/controller-api.js";
import { defaultRules } from "../utils/constants.js";
import { getZTAddress } from "../utils/zt-address.js";
2021-03-22 03:25:13 +08:00
let ZT_ADDRESS = null;
getZTAddress().then(function (address) {
ZT_ADDRESS = address;
});
// get all networks
router.get("/", auth.isAuthorized, async function (req, res) {
2021-03-22 03:25:13 +08:00
api.get("controller/network").then(async function (controllerRes) {
const nwids = controllerRes.data;
const data = await network.getNetworksData(nwids);
2021-03-22 03:25:13 +08:00
res.send(data);
});
});
// get network
router.get("/:nwid", auth.isAuthorized, async function (req, res) {
const nwid = req.params.nwid;
const data = await network.getNetworksData([nwid]);
if (data[0]) {
res.send(data[0]);
} else {
res.status(404).send({ error: "Network not found" });
}
});
2021-03-22 03:25:13 +08:00
// create new network
router.post("/", auth.isAuthorized, async function (req, res) {
2021-03-22 03:25:13 +08:00
let reqData = req.body;
if (reqData.config) {
const config = reqData.config;
delete reqData.config;
reqData = config;
2023-10-07 04:11:40 +08:00
reqData.rules = JSON.parse(defaultRules);
2021-03-22 03:25:13 +08:00
} else {
res.status(400).send({ error: "Bad request" });
}
api
.post("controller/network/" + ZT_ADDRESS + "______", reqData)
.then(async function (controllerRes) {
await network.createNetworkAdditionalData(controllerRes.data.id);
const data = await network.getNetworksData([controllerRes.data.id]);
2021-03-22 03:25:13 +08:00
res.send(data[0]);
});
});
// update network
router.post("/:nwid", auth.isAuthorized, async function (req, res) {
2021-03-22 03:25:13 +08:00
const nwid = req.params.nwid;
network.updateNetworkAdditionalData(nwid, req.body);
2021-03-22 03:25:13 +08:00
if (req.body.config) {
api
.post("controller/network/" + nwid, req.body.config)
.then(async function () {
const data = await network.getNetworksData([nwid]);
2021-03-22 03:25:13 +08:00
res.send(data[0]);
})
.catch(function (err) {
res.status(500).send({ error: err.message });
});
} else {
const data = await network.getNetworksData([nwid]);
2021-03-22 03:25:13 +08:00
res.send(data[0]);
}
});
// delete network
router.delete("/:nwid", auth.isAuthorized, async function (req, res) {
2021-03-22 03:25:13 +08:00
const nwid = req.params.nwid;
network.deleteNetworkAdditionalData(nwid);
2021-03-22 03:25:13 +08:00
api
.delete("controller/network/" + nwid)
.then(function (controllerRes) {
res.status(controllerRes.status).send("");
})
.catch(function (err) {
res.status(500).send({ error: err.message });
});
});
2023-10-07 04:11:40 +08:00
export default router;