Adapt NextJS/modules

This commit is contained in:
Aelita4 2022-08-22 09:17:06 +02:00
parent f795c986a8
commit d55e9c3d45
Signed by: Aelita4
GPG Key ID: F8EC95519509D1D5
13 changed files with 47 additions and 27 deletions

View File

@ -9,6 +9,7 @@
"keywords": [], "keywords": [],
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"type": "module",
"dependencies": { "dependencies": {
"bcrypt": "^5.0.1", "bcrypt": "^5.0.1",
"cookie-parser": "^1.4.6", "cookie-parser": "^1.4.6",

View File

@ -1,10 +1,14 @@
import express, { Request, Response } from 'express'; import express, { Request, Response } from 'express';
import * as Ping from 'ping'; import Ping from 'ping';
import { existsSync, readdirSync, writeFileSync, readFileSync } from 'fs'; import { existsSync, readdirSync, writeFileSync, readFileSync } from 'fs';
import sessions from "express-session" import sessions from "express-session"
import cookieParser from "cookie-parser" import cookieParser from "cookie-parser"
import mysql from 'mysql' import mysql from 'mysql'
import bcrypt from 'bcrypt' 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 ping = async (host: string) => {
const result = await Ping.promise.probe(host, { const result = await Ping.promise.probe(host, {
@ -14,6 +18,17 @@ const ping = async (host: string) => {
return result; return result;
} }
const getIP = () : Promise<string[]> => {
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({ const connection = mysql.createConnection({
host: "mysql", host: "mysql",
user: "root", user: "root",
@ -38,32 +53,34 @@ app.use(express.urlencoded());
const pings = new Map(); const pings = new Map();
if(!existsSync("/usr/src/app/data.json")) writeFileSync("/usr/src/app/data.json", "[]"); let addresses: Array<string> = await getIP();
let addresses = JSON.parse(readFileSync("/usr/src/app/data.json", {encoding:'utf8', flag:'r'}))
//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)); addresses.forEach((a: string) => pings.set(a, -1));
const timeoutDelay = 60000; const timeoutDelay = 60000;
const routes = readdirSync(__dirname + "/routes"); const routes = readdirSync(__dirname + "/routes");
routes.forEach(route => { routes.forEach(async route => {
const file = require(`${__dirname}/routes/${route}`); const file = await import(`${__dirname}/routes/${route}`);
console.log(`[${file.method}] ${file.url}`); console.log(`[${file.default.method}] ${file.default.url}`);
switch(file.method) { switch(file.default.method) {
case "GET": app.get(file.url, file.callback.bind(null, { addresses, ping, pings, timeoutDelay })); break; case "GET": app.get(file.default.url, file.default.callback.bind(null, { addresses, ping, pings, timeoutDelay })); break;
case "POST": app.post(file.url, file.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')); app.listen(8080, () => console.log('rdy'));
interface Data { export interface Data {
ping: Function, ping: Function,
pings: Map<string, number>, pings: Map<string, number>,
addresses: Array<string>, addresses: Array<string>,
timeoutDelay: number timeoutDelay: number
}; };
interface User { export interface User {
username: string username: string
} }
export { connection, bcrypt, User, Data }; export { connection, bcrypt, getIP };

View File

@ -1,7 +1,7 @@
import { Request, Response } from 'express'; import { Request, Response } from 'express';
import { existsSync, writeFileSync, readFileSync } from 'fs' import { existsSync, writeFileSync, readFileSync } from 'fs'
module.exports = { export default {
method: "GET", method: "GET",
url: "/addIP/:ip", url: "/addIP/:ip",
callback: async (data: any, req: Request, res: Response) => { callback: async (data: any, req: Request, res: Response) => {

View File

@ -1,6 +1,6 @@
import { Request, Response } from 'express'; import { Request, Response } from 'express';
module.exports = { export default {
method: "GET", method: "GET",
url: "/downtime/:ip", url: "/downtime/:ip",
callback: async (data: any, req: Request, res: Response) => { callback: async (data: any, req: Request, res: Response) => {

View File

@ -1,6 +1,6 @@
import { Request, Response } from 'express'; import { Request, Response } from 'express';
module.exports = { export default {
method: "GET", method: "GET",
url: "/login", url: "/login",
callback: async (data: any, req: Request, res: Response) => { callback: async (data: any, req: Request, res: Response) => {

View File

@ -1,7 +1,7 @@
import { Request, Response } from 'express'; import { Request, Response } from 'express';
import { connection, bcrypt } from '../index' import { connection, bcrypt } from '../index.js'
module.exports = { export default {
method: "POST", method: "POST",
url: "/loginUser", url: "/loginUser",
callback: async (data: any, req: Request, res: Response) => { callback: async (data: any, req: Request, res: Response) => {
@ -18,6 +18,8 @@ module.exports = {
//@ts-ignore //@ts-ignore
session.user.username = req.body.username; session.user.username = req.body.username;
session.save();
res.redirect("/"); res.redirect("/");
} else res.render("pages/login.ejs", { invalid: "baduserorpass" }); } else res.render("pages/login.ejs", { invalid: "baduserorpass" });
} }

View File

@ -1,6 +1,6 @@
import { Request, Response } from 'express'; import { Request, Response } from 'express';
module.exports = { export default {
method: "GET", method: "GET",
url: "/logout", url: "/logout",
callback: async (data: any, req: Request, res: Response) => { callback: async (data: any, req: Request, res: Response) => {

View File

@ -1,6 +1,6 @@
import { Request, Response } from 'express'; import { Request, Response } from 'express';
module.exports = { export default {
method: "GET", method: "GET",
url: "/ping/:ip", url: "/ping/:ip",
callback: async (data: any, req: Request, res: Response) => { callback: async (data: any, req: Request, res: Response) => {

View File

@ -1,6 +1,6 @@
import { Request, Response } from 'express'; import { Request, Response } from 'express';
module.exports = { export default {
method: "GET", method: "GET",
url: "/register", url: "/register",
callback: async (data: any, req: Request, res: Response) => { callback: async (data: any, req: Request, res: Response) => {

View File

@ -1,7 +1,7 @@
import e, { Request, Response } from 'express'; import { Request, Response } from 'express';
import { connection, bcrypt } from '../index' import { connection, bcrypt } from '../index.js'
module.exports = { export default {
method: "POST", method: "POST",
url: "/registerUser", url: "/registerUser",
callback: async (data: any, req: Request, res: Response) => { callback: async (data: any, req: Request, res: Response) => {

View File

@ -1,7 +1,7 @@
import { Request, Response } from 'express'; import { Request, Response } from 'express';
import { existsSync, writeFileSync, readFileSync } from 'fs' import { existsSync, writeFileSync, readFileSync } from 'fs'
module.exports = { export default {
method: "GET", method: "GET",
url: "/removeIP/:ip", url: "/removeIP/:ip",
callback: async (data: any, req: Request, res: Response) => { callback: async (data: any, req: Request, res: Response) => {

View File

@ -1,7 +1,7 @@
import { Request, Response } from 'express'; import { Request, Response } from 'express';
import { readFileSync } from 'fs'; import { readFileSync } from 'fs';
module.exports = { export default {
method: "GET", method: "GET",
url: "/", url: "/",
callback: async (data: any, req: Request, res: Response) => { callback: async (data: any, req: Request, res: Response) => {

View File

@ -24,7 +24,7 @@
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
/* Modules */ /* 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. */ "rootDir": "./src", /* Specify the root folder within your source files. */
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */ // "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. */ // "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`. */ // "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. */ // "types": [], /* Specify type package names to be included without being referenced in a source file. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ // "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 `<reference>`s from expanding the number of files TypeScript should add to a project. */ // "noResolve": true, /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */
/* JavaScript Support */ /* JavaScript Support */