feat: es6+mods and replace node-cron with croner

This commit is contained in:
Andres 2023-10-08 16:37:26 +02:00
parent 19c92ed244
commit ea828c326f
9 changed files with 10437 additions and 374 deletions

View file

@ -1,7 +1,7 @@
import express from "express";
const router = express.Router();
import { authorize } from "../services/auth.js";
import * as auth from "../services/auth.js";
router.get("/login", async function (req, res) {
if (process.env.ZU_DISABLE_AUTH === "true") {
@ -13,7 +13,7 @@ router.get("/login", async function (req, res) {
router.post("/login", async function (req, res) {
if (req.body.username && req.body.password) {
authorize(req.body.username, req.body.password, function (err, user) {
auth.authorize(req.body.username, req.body.password, function (err, user) {
if (user) {
res.send({ token: user["token"] });
} else {

View file

@ -1,10 +1,10 @@
import express from "express";
const router = express.Router();
import { isAuthorized } from "../services/auth.js";
import * as auth from "../services/auth.js";
import { api } from "../utils/controller-api.js";
router.get("/status", isAuthorized, async function (req, res) {
router.get("/status", auth.isAuthorized, async function (req, res) {
api.get("status").then(function (controllerRes) {
res.send(controllerRes.data);
});

View file

@ -1,24 +1,20 @@
import express from "express";
const router = express.Router({ mergeParams: true });
import { isAuthorized } from "../services/auth.js";
import {
deleteMemberAdditionalData,
getMembersData,
updateMemberAdditionalData,
} from "../services/member.js";
import * as auth from "../services/auth.js";
import * as member from "../services/member.js";
import { api } from "../utils/controller-api.js";
// get all members
router.get("/", isAuthorized, async function (req, res) {
router.get("/", auth.isAuthorized, async function (req, res) {
// @ts-ignore
const nwid = req.params.nwid;
api
.get("controller/network/" + nwid + "/member")
.then(async function (controllerRes) {
const mids = Object.keys(controllerRes.data);
const data = await getMembersData(nwid, mids);
const data = await member.getMembersData(nwid, mids);
res.send(data);
})
.catch(function (err) {
@ -27,11 +23,11 @@ router.get("/", isAuthorized, async function (req, res) {
});
// get member
router.get("/:mid", isAuthorized, async function (req, res) {
router.get("/:mid", auth.isAuthorized, async function (req, res) {
// @ts-ignore
const nwid = req.params.nwid;
const mid = req.params.mid;
const data = await getMembersData(nwid, [mid]);
const data = await member.getMembersData(nwid, [mid]);
if (data[0]) {
res.send(data[0]);
} else {
@ -40,33 +36,33 @@ router.get("/:mid", isAuthorized, async function (req, res) {
});
// update member
router.post("/:mid", isAuthorized, async function (req, res) {
router.post("/:mid", auth.isAuthorized, async function (req, res) {
// @ts-ignore
const nwid = req.params.nwid;
const mid = req.params.mid;
updateMemberAdditionalData(nwid, mid, req.body);
member.updateMemberAdditionalData(nwid, mid, req.body);
if (req.body.config) {
api
.post("controller/network/" + nwid + "/member/" + mid, req.body.config)
.then(async function () {
const data = await getMembersData(nwid, [mid]);
const data = await member.getMembersData(nwid, [mid]);
res.send(data[0]);
})
.catch(function (err) {
res.status(500).send({ error: err.message });
});
} else {
const data = await getMembersData(nwid, [mid]);
const data = await member.getMembersData(nwid, [mid]);
res.send(data[0]);
}
});
// delete member
router.delete("/:mid", isAuthorized, async function (req, res) {
router.delete("/:mid", auth.isAuthorized, async function (req, res) {
// @ts-ignore
const nwid = req.params.nwid;
const mid = req.params.mid;
deleteMemberAdditionalData(nwid, mid);
member.deleteMemberAdditionalData(nwid, mid);
api
.delete("controller/network/" + nwid + "/member/" + mid)
.then(function () {})

View file

@ -1,13 +1,8 @@
import express from "express";
const router = express.Router();
import { isAuthorized } from "../services/auth.js";
import {
getNetworksData,
createNetworkAdditionalData,
updateNetworkAdditionalData,
deleteNetworkAdditionalData,
} from "../services/network.js";
import * as auth from "../services/auth.js";
import * as network from "../services/network.js";
import { api } from "../utils/controller-api.js";
import { defaultRules } from "../utils/constants.js";
@ -19,18 +14,18 @@ getZTAddress().then(function (address) {
});
// get all networks
router.get("/", isAuthorized, async function (req, res) {
router.get("/", auth.isAuthorized, async function (req, res) {
api.get("controller/network").then(async function (controllerRes) {
const nwids = controllerRes.data;
const data = await getNetworksData(nwids);
const data = await network.getNetworksData(nwids);
res.send(data);
});
});
// get network
router.get("/:nwid", isAuthorized, async function (req, res) {
router.get("/:nwid", auth.isAuthorized, async function (req, res) {
const nwid = req.params.nwid;
const data = await getNetworksData([nwid]);
const data = await network.getNetworksData([nwid]);
if (data[0]) {
res.send(data[0]);
} else {
@ -39,7 +34,7 @@ router.get("/:nwid", isAuthorized, async function (req, res) {
});
// create new network
router.post("/", isAuthorized, async function (req, res) {
router.post("/", auth.isAuthorized, async function (req, res) {
let reqData = req.body;
if (reqData.config) {
const config = reqData.config;
@ -52,36 +47,36 @@ router.post("/", isAuthorized, async function (req, res) {
api
.post("controller/network/" + ZT_ADDRESS + "______", reqData)
.then(async function (controllerRes) {
await createNetworkAdditionalData(controllerRes.data.id);
const data = await getNetworksData([controllerRes.data.id]);
await network.createNetworkAdditionalData(controllerRes.data.id);
const data = await network.getNetworksData([controllerRes.data.id]);
res.send(data[0]);
});
});
// update network
router.post("/:nwid", isAuthorized, async function (req, res) {
router.post("/:nwid", auth.isAuthorized, async function (req, res) {
const nwid = req.params.nwid;
updateNetworkAdditionalData(nwid, req.body);
network.updateNetworkAdditionalData(nwid, req.body);
if (req.body.config) {
api
.post("controller/network/" + nwid, req.body.config)
.then(async function () {
const data = await getNetworksData([nwid]);
const data = await network.getNetworksData([nwid]);
res.send(data[0]);
})
.catch(function (err) {
res.status(500).send({ error: err.message });
});
} else {
const data = await getNetworksData([nwid]);
const data = await network.getNetworksData([nwid]);
res.send(data[0]);
}
});
// delete network
router.delete("/:nwid", isAuthorized, async function (req, res) {
router.delete("/:nwid", auth.isAuthorized, async function (req, res) {
const nwid = req.params.nwid;
deleteNetworkAdditionalData(nwid);
network.deleteNetworkAdditionalData(nwid);
api
.delete("controller/network/" + nwid)
.then(function (controllerRes) {