From 714d27ccc363e9ff916e04246f6ee206c9978fc9 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Wed, 16 Jul 2025 22:44:36 +0200 Subject: [PATCH] data system --- src/app/api/auth/route.ts | 15 +++++++++++---- src/app/api/data.ts | 22 +++++++++++++++++----- src/app/api/servers/route.ts | 2 +- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/app/api/auth/route.ts b/src/app/api/auth/route.ts index 9c320ab..d7c9246 100644 --- a/src/app/api/auth/route.ts +++ b/src/app/api/auth/route.ts @@ -1,10 +1,15 @@ import axios, { HttpStatusCode } from "axios"; import { NextResponse } from "next/server"; -import Data, { findKey, sessions } from "../data"; +import Data, { findKey } from "../data"; const users = new Data<{ [key: string]: { username: string; password: string }; -}>("sentryx/users.json"); +}>("sentryx/users.json", "object"); + +const sessions = new Data<{ [key: string]: string }>( + "sentryx/sessions.json", + "object" +); export async function POST(request: Request) { const body = await request.json(); @@ -29,7 +34,9 @@ export async function POST(request: Request) { const sessionId = "SESH-" + crypto.randomUUID(); - sessions[sessionId] = String(uuid); + sessions.data[sessionId] = String(uuid); + + sessions.write(); return NextResponse.json({ message: "ok", sessionId }); } @@ -39,7 +46,7 @@ export async function GET(request: Request) { const id = searchParams.get("id") || ""; - if (!sessions[id]) + if (!sessions.data[id]) return NextResponse.json( { message: "Invalid session" }, { status: HttpStatusCode.Unauthorized } diff --git a/src/app/api/data.ts b/src/app/api/data.ts index 1fd59b1..0ce0e7c 100644 --- a/src/app/api/data.ts +++ b/src/app/api/data.ts @@ -1,4 +1,4 @@ -import { readFileSync } from "fs"; +import { existsSync, readFileSync, writeFileSync } from "fs"; export function findKey( obj: T, @@ -12,14 +12,26 @@ export function findKey( export default class Data { data: T; file_path: string; - constructor(file_path: string) { + constructor(file_path: string, type: "object" | "array") { this.file_path = file_path; - this.data = JSON.parse(readFileSync(this.file_path).toString()); + if (existsSync(file_path)) { + try { + this.data = JSON.parse(readFileSync(file_path, "utf-8")); + } catch { + this.data = (type == "object" ? {} : []) as T; + this.write(); + } + } else { + this.data = (type == "object" ? {} : []) as T; + this.write(); + } } public read() { this.data = JSON.parse(readFileSync(this.file_path).toString()); } -} -export const sessions: { [key: string]: string } = {}; + public write() { + writeFileSync(this.file_path, JSON.stringify(this.data)); + } +} diff --git a/src/app/api/servers/route.ts b/src/app/api/servers/route.ts index 47316f1..cb0a84e 100644 --- a/src/app/api/servers/route.ts +++ b/src/app/api/servers/route.ts @@ -2,7 +2,7 @@ import { NextResponse } from "next/server"; import Data from "../data"; import { ServerAPI } from "@/types/server"; -const servers = new Data("sentryx/servers.json"); +const servers = new Data("sentryx/servers.json", "array"); export async function GET(request: Request) { return NextResponse.json({ message: "ok", servers: servers.data });