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
+7 -3
View File
@@ -1,6 +1,10 @@
import axios, { HttpStatusCode } from "axios";
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) {
const body = await request.json();
@@ -12,7 +16,7 @@ export async function POST(request: Request) {
);
const uuid = findKey(
users,
users.data,
(u) => u.username == body.username && u.password == body.password
);
@@ -25,7 +29,7 @@ export async function POST(request: Request) {
const sessionId = "SESH-" + crypto.randomUUID();
sessions[sessionId] = uuid;
sessions[sessionId] = String(uuid);
return NextResponse.json({ message: "ok", sessionId });
}
+13 -10
View File
@@ -1,3 +1,5 @@
import { readFileSync } from "fs";
export function findKey<T extends object>(
obj: T,
predicate: (value: T[keyof T], key: keyof T) => boolean
@@ -7,16 +9,17 @@ export function findKey<T extends object>(
)?.[0] as keyof T | undefined;
}
export type User = {
username: string;
password: string;
};
export default class Data<T> {
data: T;
file_path: string;
constructor(file_path: string) {
this.file_path = file_path;
this.data = JSON.parse(readFileSync(this.file_path).toString());
}
export const users = {
admin: {
username: "admin",
password: "admin",
},
};
public read() {
this.data = JSON.parse(readFileSync(this.file_path).toString());
}
}
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";
}