data system
This commit is contained in:
@@ -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
@@ -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 } = {};
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
export interface ServerAPI {
|
||||
name: string;
|
||||
ip: string;
|
||||
}
|
||||
|
||||
export default interface Server extends ServerAPI {
|
||||
status: "online" | "offline" | "warning" | "maintenance";
|
||||
}
|
||||
Reference in New Issue
Block a user