From d55e9c3d4558fdc7b555356b7fa668dfd464e1da Mon Sep 17 00:00:00 2001 From: Aelita4 Date: Mon, 22 Aug 2022 09:17:06 +0200 Subject: [PATCH] Adapt NextJS/modules --- package.json | 1 + src/index.ts | 41 +++++++++++++++++++++++++++----------- src/routes/addIP.ts | 2 +- src/routes/downtime.ts | 2 +- src/routes/login.ts | 2 +- src/routes/loginUser.ts | 6 ++++-- src/routes/logout.ts | 2 +- src/routes/ping.ts | 2 +- src/routes/register.ts | 2 +- src/routes/registerUser.ts | 6 +++--- src/routes/removeIP.ts | 2 +- src/routes/root.ts | 2 +- tsconfig.json | 4 ++-- 13 files changed, 47 insertions(+), 27 deletions(-) diff --git a/package.json b/package.json index 4ae96e6..8637170 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "keywords": [], "author": "", "license": "ISC", + "type": "module", "dependencies": { "bcrypt": "^5.0.1", "cookie-parser": "^1.4.6", diff --git a/src/index.ts b/src/index.ts index 78dfe6f..8389078 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,14 @@ import express, { Request, Response } from 'express'; -import * as Ping from 'ping'; +import Ping from 'ping'; import { existsSync, readdirSync, writeFileSync, readFileSync } from 'fs'; import sessions from "express-session" import cookieParser from "cookie-parser" import mysql from 'mysql' import bcrypt from 'bcrypt' +import url from 'url'; + +//const __filename = url.fileURLToPath(import.meta.url); +const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); const ping = async (host: string) => { const result = await Ping.promise.probe(host, { @@ -14,6 +18,17 @@ const ping = async (host: string) => { return result; } +const getIP = () : Promise => { + return new Promise((resolve, reject) => { + connection.query("SELECT * FROM ips", async (err, results, fields) => { + if(err) reject(err); + const addresses = [...results.slice()]; + + resolve(addresses); + }); + }); +} + const connection = mysql.createConnection({ host: "mysql", user: "root", @@ -38,32 +53,34 @@ app.use(express.urlencoded()); const pings = new Map(); -if(!existsSync("/usr/src/app/data.json")) writeFileSync("/usr/src/app/data.json", "[]"); -let addresses = JSON.parse(readFileSync("/usr/src/app/data.json", {encoding:'utf8', flag:'r'})) +let addresses: Array = await getIP(); + +//if(!existsSync("/usr/src/app/data.json")) writeFileSync("/usr/src/app/data.json", "[]"); +//let addresses = JSON.parse(readFileSync("/usr/src/app/data.json", {encoding:'utf8', flag:'r'})) addresses.forEach((a: string) => pings.set(a, -1)); const timeoutDelay = 60000; const routes = readdirSync(__dirname + "/routes"); -routes.forEach(route => { - const file = require(`${__dirname}/routes/${route}`); - console.log(`[${file.method}] ${file.url}`); - switch(file.method) { - case "GET": app.get(file.url, file.callback.bind(null, { addresses, ping, pings, timeoutDelay })); break; - case "POST": app.post(file.url, file.callback.bind(null, { addresses, ping, pings, timeoutDelay })); break; +routes.forEach(async route => { + const file = await import(`${__dirname}/routes/${route}`); + console.log(`[${file.default.method}] ${file.default.url}`); + switch(file.default.method) { + case "GET": app.get(file.default.url, file.default.callback.bind(null, { addresses, ping, pings, timeoutDelay })); break; + case "POST": app.post(file.default.url, file.default.callback.bind(null, { addresses, ping, pings, timeoutDelay })); break; } }); app.listen(8080, () => console.log('rdy')); -interface Data { +export interface Data { ping: Function, pings: Map, addresses: Array, timeoutDelay: number }; -interface User { +export interface User { username: string } -export { connection, bcrypt, User, Data }; \ No newline at end of file +export { connection, bcrypt, getIP }; \ No newline at end of file diff --git a/src/routes/addIP.ts b/src/routes/addIP.ts index 539571e..9b0fd9c 100644 --- a/src/routes/addIP.ts +++ b/src/routes/addIP.ts @@ -1,7 +1,7 @@ import { Request, Response } from 'express'; import { existsSync, writeFileSync, readFileSync } from 'fs' -module.exports = { +export default { method: "GET", url: "/addIP/:ip", callback: async (data: any, req: Request, res: Response) => { diff --git a/src/routes/downtime.ts b/src/routes/downtime.ts index 1a9d03e..8331301 100644 --- a/src/routes/downtime.ts +++ b/src/routes/downtime.ts @@ -1,6 +1,6 @@ import { Request, Response } from 'express'; -module.exports = { +export default { method: "GET", url: "/downtime/:ip", callback: async (data: any, req: Request, res: Response) => { diff --git a/src/routes/login.ts b/src/routes/login.ts index 7e2b744..194dba6 100644 --- a/src/routes/login.ts +++ b/src/routes/login.ts @@ -1,6 +1,6 @@ import { Request, Response } from 'express'; -module.exports = { +export default { method: "GET", url: "/login", callback: async (data: any, req: Request, res: Response) => { diff --git a/src/routes/loginUser.ts b/src/routes/loginUser.ts index 3332432..9dcd09b 100644 --- a/src/routes/loginUser.ts +++ b/src/routes/loginUser.ts @@ -1,7 +1,7 @@ import { Request, Response } from 'express'; -import { connection, bcrypt } from '../index' +import { connection, bcrypt } from '../index.js' -module.exports = { +export default { method: "POST", url: "/loginUser", callback: async (data: any, req: Request, res: Response) => { @@ -17,6 +17,8 @@ module.exports = { session.user = {} //@ts-ignore session.user.username = req.body.username; + + session.save(); res.redirect("/"); } else res.render("pages/login.ejs", { invalid: "baduserorpass" }); diff --git a/src/routes/logout.ts b/src/routes/logout.ts index 2c0e71f..60a0b3e 100644 --- a/src/routes/logout.ts +++ b/src/routes/logout.ts @@ -1,6 +1,6 @@ import { Request, Response } from 'express'; -module.exports = { +export default { method: "GET", url: "/logout", callback: async (data: any, req: Request, res: Response) => { diff --git a/src/routes/ping.ts b/src/routes/ping.ts index 1134517..f072f84 100644 --- a/src/routes/ping.ts +++ b/src/routes/ping.ts @@ -1,6 +1,6 @@ import { Request, Response } from 'express'; -module.exports = { +export default { method: "GET", url: "/ping/:ip", callback: async (data: any, req: Request, res: Response) => { diff --git a/src/routes/register.ts b/src/routes/register.ts index fb90843..87d2f6e 100644 --- a/src/routes/register.ts +++ b/src/routes/register.ts @@ -1,6 +1,6 @@ import { Request, Response } from 'express'; -module.exports = { +export default { method: "GET", url: "/register", callback: async (data: any, req: Request, res: Response) => { diff --git a/src/routes/registerUser.ts b/src/routes/registerUser.ts index 29c0652..102463e 100644 --- a/src/routes/registerUser.ts +++ b/src/routes/registerUser.ts @@ -1,7 +1,7 @@ -import e, { Request, Response } from 'express'; -import { connection, bcrypt } from '../index' +import { Request, Response } from 'express'; +import { connection, bcrypt } from '../index.js' -module.exports = { +export default { method: "POST", url: "/registerUser", callback: async (data: any, req: Request, res: Response) => { diff --git a/src/routes/removeIP.ts b/src/routes/removeIP.ts index 6869f41..32ee41d 100644 --- a/src/routes/removeIP.ts +++ b/src/routes/removeIP.ts @@ -1,7 +1,7 @@ import { Request, Response } from 'express'; import { existsSync, writeFileSync, readFileSync } from 'fs' -module.exports = { +export default { method: "GET", url: "/removeIP/:ip", callback: async (data: any, req: Request, res: Response) => { diff --git a/src/routes/root.ts b/src/routes/root.ts index 5cddefe..210c482 100644 --- a/src/routes/root.ts +++ b/src/routes/root.ts @@ -1,7 +1,7 @@ import { Request, Response } from 'express'; import { readFileSync } from 'fs'; -module.exports = { +export default { method: "GET", url: "/", callback: async (data: any, req: Request, res: Response) => { diff --git a/tsconfig.json b/tsconfig.json index 956729b..158c4ec 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -24,7 +24,7 @@ // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ /* Modules */ - "module": "commonjs", /* Specify what module code is generated. */ + "module": "ESNext", /* Specify what module code is generated. */ "rootDir": "./src", /* Specify the root folder within your source files. */ // "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */ // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ @@ -33,7 +33,7 @@ // "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */ // "types": [], /* Specify type package names to be included without being referenced in a source file. */ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ - "resolveJsonModule": true, /* Enable importing .json files */ + // "resolveJsonModule": true, /* Enable importing .json files */ // "noResolve": true, /* Disallow `import`s, `require`s or ``s from expanding the number of files TypeScript should add to a project. */ /* JavaScript Support */