data system
This commit is contained in:
@@ -1,10 +1,15 @@
|
|||||||
import axios, { HttpStatusCode } from "axios";
|
import axios, { HttpStatusCode } from "axios";
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import Data, { findKey, sessions } from "../data";
|
import Data, { findKey } from "../data";
|
||||||
|
|
||||||
const users = new Data<{
|
const users = new Data<{
|
||||||
[key: string]: { username: string; password: string };
|
[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) {
|
export async function POST(request: Request) {
|
||||||
const body = await request.json();
|
const body = await request.json();
|
||||||
@@ -29,7 +34,9 @@ export async function POST(request: Request) {
|
|||||||
|
|
||||||
const sessionId = "SESH-" + crypto.randomUUID();
|
const sessionId = "SESH-" + crypto.randomUUID();
|
||||||
|
|
||||||
sessions[sessionId] = String(uuid);
|
sessions.data[sessionId] = String(uuid);
|
||||||
|
|
||||||
|
sessions.write();
|
||||||
|
|
||||||
return NextResponse.json({ message: "ok", sessionId });
|
return NextResponse.json({ message: "ok", sessionId });
|
||||||
}
|
}
|
||||||
@@ -39,7 +46,7 @@ export async function GET(request: Request) {
|
|||||||
|
|
||||||
const id = searchParams.get("id") || "";
|
const id = searchParams.get("id") || "";
|
||||||
|
|
||||||
if (!sessions[id])
|
if (!sessions.data[id])
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ message: "Invalid session" },
|
{ message: "Invalid session" },
|
||||||
{ status: HttpStatusCode.Unauthorized }
|
{ 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>(
|
export function findKey<T extends object>(
|
||||||
obj: T,
|
obj: T,
|
||||||
@@ -12,14 +12,26 @@ export function findKey<T extends object>(
|
|||||||
export default class Data<T> {
|
export default class Data<T> {
|
||||||
data: T;
|
data: T;
|
||||||
file_path: string;
|
file_path: string;
|
||||||
constructor(file_path: string) {
|
constructor(file_path: string, type: "object" | "array") {
|
||||||
this.file_path = file_path;
|
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() {
|
public read() {
|
||||||
this.data = JSON.parse(readFileSync(this.file_path).toString());
|
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 Data from "../data";
|
||||||
import { ServerAPI } from "@/types/server";
|
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) {
|
export async function GET(request: Request) {
|
||||||
return NextResponse.json({ message: "ok", servers: servers.data });
|
return NextResponse.json({ message: "ok", servers: servers.data });
|
||||||
|
|||||||
Reference in New Issue
Block a user