Structured libs

This commit is contained in:
2026-02-14 16:42:11 +01:00
parent a7e29867b8
commit 1331e36177
15 changed files with 42 additions and 38 deletions
+4 -4
View File
@@ -1,9 +1,9 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import { version } from "@/../package.json"; import { version } from "@/../package.json";
import testAuth from "../auth"; import testAuth from "../../../lib/server/auth";
import { StatusCodes } from "http-status-codes"; import { StatusCodes } from "http-status-codes";
import { read } from "@/app/api/file"; import { read } from "@/lib/server/file";
import { NodeJsonTemplate } from "../types"; import { NodeJsonTemplate } from "../../../lib/server/types";
import osName from "os-name"; import osName from "os-name";
export async function GET(req: NextRequest): Promise<NextResponse> { export async function GET(req: NextRequest): Promise<NextResponse> {
@@ -12,7 +12,7 @@ export async function GET(req: NextRequest): Promise<NextResponse> {
if (auth) { if (auth) {
return NextResponse.json( return NextResponse.json(
{ message: auth }, { message: auth },
{ status: StatusCodes.UNAUTHORIZED } { status: StatusCodes.UNAUTHORIZED },
); );
} }
+4 -4
View File
@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import testAuth from "../auth"; import testAuth from "../../../lib/server/auth";
import { StatusCodes } from "http-status-codes"; import { StatusCodes } from "http-status-codes";
import { getDocker, removeContainer } from "@/core/docker"; import { getDocker, removeContainer } from "@/core/docker";
import { Docker } from "@/lib/types"; import { Docker } from "@/lib/types";
@@ -10,7 +10,7 @@ export async function GET(req: NextRequest): Promise<NextResponse> {
if (auth) { if (auth) {
return NextResponse.json( return NextResponse.json(
{ message: auth }, { message: auth },
{ status: StatusCodes.UNAUTHORIZED } { status: StatusCodes.UNAUTHORIZED },
); );
} }
@@ -30,14 +30,14 @@ export async function DELETE(req: NextRequest): Promise<NextResponse> {
if (!id) { if (!id) {
return NextResponse.json( return NextResponse.json(
{ message: "Invalid containerId param" }, { message: "Invalid containerId param" },
{ status: StatusCodes.BAD_REQUEST } { status: StatusCodes.BAD_REQUEST },
); );
} }
if (auth) { if (auth) {
return NextResponse.json( return NextResponse.json(
{ message: auth }, { message: auth },
{ status: StatusCodes.UNAUTHORIZED } { status: StatusCodes.UNAUTHORIZED },
); );
} }
+5 -5
View File
@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import testAuth from "../../auth"; import testAuth from "../../../../lib/server/auth";
import { StatusCodes } from "http-status-codes"; import { StatusCodes } from "http-status-codes";
import { startContainer, stopContainer } from "@/core/docker"; import { startContainer, stopContainer } from "@/core/docker";
@@ -10,14 +10,14 @@ export async function POST(req: NextRequest): Promise<NextResponse> {
if (!id) { if (!id) {
return NextResponse.json( return NextResponse.json(
{ message: "Invalid containerId param" }, { message: "Invalid containerId param" },
{ status: StatusCodes.BAD_REQUEST } { status: StatusCodes.BAD_REQUEST },
); );
} }
if (auth) { if (auth) {
return NextResponse.json( return NextResponse.json(
{ message: auth }, { message: auth },
{ status: StatusCodes.UNAUTHORIZED } { status: StatusCodes.UNAUTHORIZED },
); );
} }
@@ -35,14 +35,14 @@ export async function DELETE(req: NextRequest): Promise<NextResponse> {
if (!id) { if (!id) {
return NextResponse.json( return NextResponse.json(
{ message: "Invalid containerId param" }, { message: "Invalid containerId param" },
{ status: StatusCodes.BAD_REQUEST } { status: StatusCodes.BAD_REQUEST },
); );
} }
if (auth) { if (auth) {
return NextResponse.json( return NextResponse.json(
{ message: auth }, { message: auth },
{ status: StatusCodes.UNAUTHORIZED } { status: StatusCodes.UNAUTHORIZED },
); );
} }
+3 -3
View File
@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import testAuth from "../../auth"; import testAuth from "../../../../lib/server/auth";
import { StatusCodes } from "http-status-codes"; import { StatusCodes } from "http-status-codes";
import { getContainerStats } from "@/core/docker"; import { getContainerStats } from "@/core/docker";
@@ -10,14 +10,14 @@ export async function GET(req: NextRequest): Promise<NextResponse> {
if (!id) { if (!id) {
return NextResponse.json( return NextResponse.json(
{ message: "Invalid containerId param" }, { message: "Invalid containerId param" },
{ status: StatusCodes.BAD_REQUEST } { status: StatusCodes.BAD_REQUEST },
); );
} }
if (auth) { if (auth) {
return NextResponse.json( return NextResponse.json(
{ message: auth }, { message: auth },
{ status: StatusCodes.UNAUTHORIZED } { status: StatusCodes.UNAUTHORIZED },
); );
} }
+1 -1
View File
@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import testAuth from "../auth"; import testAuth from "../../../lib/server/auth";
import { StatusCodes } from "http-status-codes"; import { StatusCodes } from "http-status-codes";
import { deployApp, toggleDeployment } from "@/core/deployment"; import { deployApp, toggleDeployment } from "@/core/deployment";
import { Application, Port } from "@/lib/types"; import { Application, Port } from "@/lib/types";
+3 -3
View File
@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import testAuth from "../auth"; import testAuth from "../../../lib/server/auth";
import { StatusCodes } from "http-status-codes"; import { StatusCodes } from "http-status-codes";
import { getDeployments } from "@/core/deployment"; import { getDeployments } from "@/core/deployment";
@@ -10,14 +10,14 @@ export async function POST(req: NextRequest): Promise<NextResponse> {
if (auth) { if (auth) {
return NextResponse.json( return NextResponse.json(
{ message: auth }, { message: auth },
{ status: StatusCodes.UNAUTHORIZED } { status: StatusCodes.UNAUTHORIZED },
); );
} }
if (!apps) if (!apps)
return NextResponse.json( return NextResponse.json(
{ message: "Body should be Application[]" }, { message: "Body should be Application[]" },
{ status: StatusCodes.UNAUTHORIZED } { status: StatusCodes.UNAUTHORIZED },
); );
const deployments = await getDeployments(apps); const deployments = await getDeployments(apps);
+2 -2
View File
@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import testAuth from "../auth"; import testAuth from "../../../lib/server/auth";
import { StatusCodes } from "http-status-codes"; import { StatusCodes } from "http-status-codes";
import { getAllContainerLogs } from "@/core/docker"; import { getAllContainerLogs } from "@/core/docker";
@@ -9,7 +9,7 @@ export async function GET(req: NextRequest): Promise<NextResponse> {
if (auth) { if (auth) {
return NextResponse.json( return NextResponse.json(
{ message: auth }, { message: auth },
{ status: StatusCodes.UNAUTHORIZED } { status: StatusCodes.UNAUTHORIZED },
); );
} }
+2 -2
View File
@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import testAuth from "../auth"; import testAuth from "../../../lib/server/auth";
import { StatusCodes } from "http-status-codes"; import { StatusCodes } from "http-status-codes";
import si from "systeminformation"; import si from "systeminformation";
import { average } from "@/lib/format"; import { average } from "@/lib/format";
@@ -93,7 +93,7 @@ export async function GET(req: NextRequest): Promise<NextResponse> {
if (auth) { if (auth) {
return NextResponse.json( return NextResponse.json(
{ message: auth }, { message: auth },
{ status: StatusCodes.UNAUTHORIZED } { status: StatusCodes.UNAUTHORIZED },
); );
} }
+2 -2
View File
@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import testAuth from "../auth"; import testAuth from "../../../lib/server/auth";
import { StatusCodes } from "http-status-codes"; import { StatusCodes } from "http-status-codes";
import si from "systeminformation"; import si from "systeminformation";
import { NodeLiveData } from "@/lib/types"; import { NodeLiveData } from "@/lib/types";
@@ -10,7 +10,7 @@ export async function GET(req: NextRequest): Promise<NextResponse> {
if (auth) { if (auth) {
return NextResponse.json( return NextResponse.json(
{ message: auth }, { message: auth },
{ status: StatusCodes.UNAUTHORIZED } { status: StatusCodes.UNAUTHORIZED },
); );
} }
+8 -4
View File
@@ -1,5 +1,7 @@
"use server"; "use server";
import { read, write } from "@/app/api/file"; import testAuth from "@/lib/server/auth";
import { read, write } from "@/lib/server/file";
import { NodeJsonTemplate } from "@/lib/server/types";
import axios from "axios"; import axios from "axios";
import { createHash } from "crypto"; import { createHash } from "crypto";
import { cookies } from "next/headers"; import { cookies } from "next/headers";
@@ -20,7 +22,7 @@ export async function getSession(): Promise<
const tokens: Record<string, string> = await read( const tokens: Record<string, string> = await read(
() => ({}), () => ({}),
"data", "data",
"sessions.json" "sessions.json",
); );
const tokenHash = createHash("sha256").update(accessToken).digest("hex"); const tokenHash = createHash("sha256").update(accessToken).digest("hex");
@@ -36,7 +38,7 @@ export async function getSession(): Promise<
// Test the token // Test the token
const users = Object.keys( const users = Object.keys(
await read<Users>(() => ({}), "data", "users.json") await read<Users>(() => ({}), "data", "users.json"),
); );
if (!users.includes(String(userId))) { if (!users.includes(String(userId))) {
@@ -44,8 +46,10 @@ export async function getSession(): Promise<
await write( await write(
{ [userId]: { roles: ["admin"] } } as Users, // First user is always an admin { [userId]: { roles: ["admin"] } } as Users, // First user is always an admin
"data", "data",
"users.json" "users.json",
); );
const { key } = await read(NodeJsonTemplate, "data", "node.json");
console.log(`Node key: ${key}\ncwd: ${process.cwd()}`);
} else { } else {
throw new Error("Invalid user"); throw new Error("Invalid user");
} }
+3 -3
View File
@@ -2,7 +2,7 @@
import { verifySession } from "../../core/auth/session"; import { verifySession } from "../../core/auth/session";
import { Env, EnvVariable, Application } from "@/lib/types"; import { Env, EnvVariable, Application } from "@/lib/types";
import { read, write } from "@/app/api/file"; import { read, write } from "@/lib/server/file";
import { randomUUID } from "crypto"; import { randomUUID } from "crypto";
export async function getApps(): Promise<Application[]> { export async function getApps(): Promise<Application[]> {
@@ -19,7 +19,7 @@ export async function getApp(appId: string): Promise<Application | undefined> {
} }
return (await read<Application[]>(() => [], "data", "apps.json")).find( return (await read<Application[]>(() => [], "data", "apps.json")).find(
(app) => app.id === appId (app) => app.id === appId,
); );
} }
@@ -86,7 +86,7 @@ export async function getEnv(): Promise<Record<string, Env>> {
export async function setEnv( export async function setEnv(
appId: string, appId: string,
variables: Record<string, EnvVariable> variables: Record<string, EnvVariable>,
) { ) {
if (await verifySession()) { if (await verifySession()) {
throw new Error("Unauthorized"); throw new Error("Unauthorized");
+2 -2
View File
@@ -1,6 +1,6 @@
"use server"; "use server";
import { read, write } from "@/app/api/file"; import { read, write } from "@/lib/server/file";
import { Node, NodeLiveData } from "@/lib/types"; import { Node, NodeLiveData } from "@/lib/types";
import axios from "axios"; import axios from "axios";
import { verifySession } from "../../core/auth/session"; import { verifySession } from "../../core/auth/session";
@@ -30,7 +30,7 @@ export async function getLiveNodes(): Promise<(NodeLiveData & Node)[]> {
headers: { Authorization: `ApiKey ${node.key}` }, headers: { Authorization: `ApiKey ${node.key}` },
}) })
).data.liveData, ).data.liveData,
})) })),
); );
} }
@@ -1,9 +1,9 @@
import { read } from "@/app/api/file"; import { read } from "@/lib/server/file";
import { NodeJsonTemplate } from "@/app/api/types"; import { NodeJsonTemplate } from "@/lib/server/types";
import { NextRequest } from "next/server"; import { NextRequest } from "next/server";
export default async function testAuth( export default async function testAuth(
req: NextRequest req: NextRequest,
): Promise<null | string> { ): Promise<null | string> {
const auth = req.headers const auth = req.headers
.get("Authorization") .get("Authorization")