data system
This commit is contained in:
@@ -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 }
|
||||
|
||||
+17
-5
@@ -1,4 +1,4 @@
|
||||
import { readFileSync } from "fs";
|
||||
import { existsSync, readFileSync, writeFileSync } from "fs";
|
||||
|
||||
export function findKey<T extends object>(
|
||||
obj: T,
|
||||
@@ -12,14 +12,26 @@ export function findKey<T extends object>(
|
||||
export default class Data<T> {
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { NextResponse } from "next/server";
|
||||
import Data from "../data";
|
||||
import { ServerAPI } from "@/types/server";
|
||||
|
||||
const servers = new Data<ServerAPI>("sentryx/servers.json");
|
||||
const servers = new Data<ServerAPI>("sentryx/servers.json", "array");
|
||||
|
||||
export async function GET(request: Request) {
|
||||
return NextResponse.json({ message: "ok", servers: servers.data });
|
||||
|
||||
Reference in New Issue
Block a user