From 78b6c6b68729459cc3cd751b25fb23fde2634e4b Mon Sep 17 00:00:00 2001 From: Aelita4 Date: Thu, 18 Aug 2022 08:31:00 +0200 Subject: [PATCH] Restructure code for multiple files --- src/index.ts | 140 +++++------------------------------------ src/routes/addIP.ts | 18 ++++++ src/routes/downtime.ts | 13 ++++ src/routes/ping.ts | 13 ++++ src/routes/removeIP.ts | 17 +++++ src/routes/root.ts | 13 ++++ views/pages/index.ejs | 97 ++++++++++++++++++++++++++++ 7 files changed, 187 insertions(+), 124 deletions(-) create mode 100644 src/routes/addIP.ts create mode 100644 src/routes/downtime.ts create mode 100644 src/routes/ping.ts create mode 100644 src/routes/removeIP.ts create mode 100644 src/routes/root.ts create mode 100644 views/pages/index.ejs diff --git a/src/index.ts b/src/index.ts index 32c8427..92d5328 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ import express, { Request, Response } from 'express'; import * as Ping from 'ping'; -import * as fs from 'fs'; +import { existsSync, readdirSync, writeFileSync, readFileSync } from 'fs'; const ping = async (host: string) => { const result = await Ping.promise.probe(host, { @@ -14,132 +14,24 @@ const app = express(); const pings = new Map(); -if(!fs.existsSync("../data.json")) fs.writeFileSync("../data.json", "[]"); -let addresses = JSON.parse(fs.readFileSync("../data.json", {encoding:'utf8', flag:'r'})) +if(!existsSync("../data.json")) writeFileSync("../data.json", "[]"); +let addresses = JSON.parse(readFileSync("../data.json", {encoding:'utf8', flag:'r'})) addresses.forEach((a: string) => pings.set(a, -1)); const timeoutDelay = 60000; -app.get("/", async (req: Request, res: Response) => { - addresses = JSON.parse(fs.readFileSync("../data.json", {encoding:'utf8', flag:'r'})) - let out = `
12:00:00

-

`; - - addresses.forEach((a: string) => { - out += `
`; - }) - - out += `
${a}
WAITING
`; - - res.send(out); -}); - -app.get("/ping/:ip", async (req: Request, res: Response) => { - const ip = req.params.ip; - const r = await ping(ip); - if(!r.alive && pings.get(ip) === -1) pings.set(ip, Date.now()); - else if(r.alive) pings.set(ip, -1); - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(r)); -}); - -app.get("/downtime/:ip", async (req: Request, res: Response) => { - const ip = req.params.ip; - - const json = { downSince: pings.get(ip) }; - - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(json)); -}); - -app.get("/addIP/:ip", async (req: Request, res: Response) => { - if(!fs.existsSync("../data.json")) fs.writeFileSync("../data.json", "[]"); - const json = JSON.parse(fs.readFileSync("../data.json", {encoding:'utf8', flag:'r'})); - - json.push(req.params.ip); - - - fs.writeFileSync("../data.json", JSON.stringify(json)) - - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify({ status: "OK" })); -}); - -app.get("/removeIP/:ip", async (req: Request, res: Response) => { - if(!fs.existsSync("../data.json")) fs.writeFileSync("../data.json", "[]"); - const json = JSON.parse(fs.readFileSync("../data.json", {encoding:'utf8', flag:'r'})); - - json.splice(json.indexOf(req.params.ip.toString()), 1) - - fs.writeFileSync("../data.json", JSON.stringify(json)) - - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify({ status: "OK" })); +const routes = readdirSync("./routes"); +routes.forEach(route => { + const file = require(`./routes/${route}`); + app.get(file.url, file.callback.bind(null, { addresses, ping, pings, timeoutDelay })) }); +app.set('views', __dirname + '/../views'); app.set('view engine', 'ejs'); -app.listen(8080, () => console.log('rdy')); \ No newline at end of file +app.listen(8080, () => console.log('rdy')); + +export interface Data { + ping: Function, + pings: Map, + addresses: Array, + timeoutDelay: number +}; \ No newline at end of file diff --git a/src/routes/addIP.ts b/src/routes/addIP.ts new file mode 100644 index 0000000..19b254e --- /dev/null +++ b/src/routes/addIP.ts @@ -0,0 +1,18 @@ +import { Request, Response } from 'express'; +import { existsSync, writeFileSync, readFileSync } from 'fs' + +module.exports = { + url: "/addIP/:ip", + callback: async (data: any, req: Request, res: Response) => { + if(!existsSync("../data.json")) writeFileSync("../data.json", "[]"); + const json = JSON.parse(readFileSync("../data.json", {encoding:'utf8', flag:'r'})); + + json.push(req.params.ip); + + + writeFileSync("../data.json", JSON.stringify(json)) + + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify({ status: "OK" })); + } +} \ No newline at end of file diff --git a/src/routes/downtime.ts b/src/routes/downtime.ts new file mode 100644 index 0000000..4450b43 --- /dev/null +++ b/src/routes/downtime.ts @@ -0,0 +1,13 @@ +import { Request, Response } from 'express'; + +module.exports = { + url: "/downtime/:ip", + callback: async (data: any, req: Request, res: Response) => { + const ip = req.params.ip; + + const json = { downSince: data.pings.get(ip) }; + + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify(json)); + } +} \ No newline at end of file diff --git a/src/routes/ping.ts b/src/routes/ping.ts new file mode 100644 index 0000000..d6109fd --- /dev/null +++ b/src/routes/ping.ts @@ -0,0 +1,13 @@ +import { Request, Response } from 'express'; + +module.exports = { + url: "/ping/:ip", + callback: async (data: any, req: Request, res: Response) => { + const ip = req.params.ip; + const r = await data.ping(ip); + if(!r.alive && data.pings.get(ip) === -1) data.pings.set(ip, Date.now()); + else if(r.alive) data.pings.set(ip, -1); + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify(r)); + } +} \ No newline at end of file diff --git a/src/routes/removeIP.ts b/src/routes/removeIP.ts new file mode 100644 index 0000000..32fba07 --- /dev/null +++ b/src/routes/removeIP.ts @@ -0,0 +1,17 @@ +import { Request, Response } from 'express'; +import { existsSync, writeFileSync, readFileSync } from 'fs' + +module.exports = { + url: "/removeIP/:ip", + callback: async (data: any, req: Request, res: Response) => { + if(!existsSync("../data.json")) writeFileSync("../data.json", "[]"); + const json = JSON.parse(readFileSync("../data.json", {encoding:'utf8', flag:'r'})); + + json.splice(json.indexOf(req.params.ip.toString()), 1) + + writeFileSync("../data.json", JSON.stringify(json)) + + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify({ status: "OK" })); + } +} \ No newline at end of file diff --git a/src/routes/root.ts b/src/routes/root.ts new file mode 100644 index 0000000..d382068 --- /dev/null +++ b/src/routes/root.ts @@ -0,0 +1,13 @@ +import { Request, Response } from 'express'; +import { readFileSync } from 'fs'; + +module.exports = { + url: "/", + callback: async (data: any, req: Request, res: Response) => { + data.addresses = JSON.parse(readFileSync("../data.json", {encoding:'utf8', flag:'r'})) + res.render('pages/index.ejs', { + addresses: data.addresses, + timeoutDelay: data.timeoutDelay + }); + } +} \ No newline at end of file diff --git a/views/pages/index.ejs b/views/pages/index.ejs new file mode 100644 index 0000000..01d2e92 --- /dev/null +++ b/views/pages/index.ejs @@ -0,0 +1,97 @@ + + + + + +
12:00:00
+
+

+ +

+
+ + <% addresses.forEach(a => { %> +
+
+ + + + <% }) %> +
+ + +
<%= a %>
+
+
+ WAITING +
+
+
+ + + \ No newline at end of file