data system

This commit is contained in:
2025-07-16 22:29:10 +02:00
parent 8cbb431f8b
commit 495213a2ca
6 changed files with 49 additions and 13 deletions
+6
View File
@@ -0,0 +1,6 @@
[
{
"name": "Main Server",
"ip": "10.7.1.2"
}
]
+6
View File
@@ -0,0 +1,6 @@
{
"admin": {
"username": "admin",
"password": "admin"
}
}
+7 -3
View File
@@ -1,6 +1,10 @@
import axios, { HttpStatusCode } from "axios"; import axios, { HttpStatusCode } from "axios";
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
import { findKey, sessions, users } from "../data"; import Data, { findKey, sessions } from "../data";
const users = new Data<{
[key: string]: { username: string; password: string };
}>("sentryx/users.json");
export async function POST(request: Request) { export async function POST(request: Request) {
const body = await request.json(); const body = await request.json();
@@ -12,7 +16,7 @@ export async function POST(request: Request) {
); );
const uuid = findKey( const uuid = findKey(
users, users.data,
(u) => u.username == body.username && u.password == body.password (u) => u.username == body.username && u.password == body.password
); );
@@ -25,7 +29,7 @@ export async function POST(request: Request) {
const sessionId = "SESH-" + crypto.randomUUID(); const sessionId = "SESH-" + crypto.randomUUID();
sessions[sessionId] = uuid; sessions[sessionId] = String(uuid);
return NextResponse.json({ message: "ok", sessionId }); return NextResponse.json({ message: "ok", sessionId });
} }
+13 -10
View File
@@ -1,3 +1,5 @@
import { readFileSync } from "fs";
export function findKey<T extends object>( export function findKey<T extends object>(
obj: T, obj: T,
predicate: (value: T[keyof T], key: keyof T) => boolean predicate: (value: T[keyof T], key: keyof T) => boolean
@@ -7,16 +9,17 @@ export function findKey<T extends object>(
)?.[0] as keyof T | undefined; )?.[0] as keyof T | undefined;
} }
export type User = { export default class Data<T> {
username: string; data: T;
password: string; file_path: string;
}; constructor(file_path: string) {
this.file_path = file_path;
this.data = JSON.parse(readFileSync(this.file_path).toString());
}
export const users = { public read() {
admin: { this.data = JSON.parse(readFileSync(this.file_path).toString());
username: "admin", }
password: "admin", }
},
};
export const sessions: { [key: string]: string } = {}; export const sessions: { [key: string]: string } = {};
+9
View File
@@ -0,0 +1,9 @@
import { NextResponse } from "next/server";
import Data from "../data";
import { ServerAPI } from "@/types/server";
const servers = new Data<ServerAPI>("sentryx/servers.json");
export async function GET(request: Request) {
return NextResponse.json({ message: "ok", servers: servers.data });
}
+8
View File
@@ -0,0 +1,8 @@
export interface ServerAPI {
name: string;
ip: string;
}
export default interface Server extends ServerAPI {
status: "online" | "offline" | "warning" | "maintenance";
}