small stuff to /server/id

This commit is contained in:
2025-07-21 12:54:04 -05:00
parent 96f174dd0e
commit c353a037d6
10 changed files with 47 additions and 20 deletions
+4 -3
View File
@@ -1,6 +1,6 @@
import axios, { HttpStatusCode } from "axios"; import axios, { HttpStatusCode } from "axios";
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
import { PORT, servers } from "../servers/servers"; import { currentServers, PORT, servers } from "../servers/servers";
import validate from "../auth/auth"; import validate from "../auth/auth";
import Service from "@/types/service"; import Service from "@/types/service";
@@ -27,14 +27,15 @@ export async function GET(request: Request) {
} }
); );
const server = servers.data[parseInt(index)]; const server = currentServers[parseInt(index)];
axios.get(`http://${server.ip}:${PORT}/apps`, {params: {auth: 'my-key'}}).then((apps) => { axios.get(`http://${server.ip}:${PORT}/apps`, {params: {auth: 'my-key'}}).then((apps) => {
allServices[parseInt(index)] = apps.data; allServices[parseInt(index)] = apps.data;
}); });
return NextResponse.json({ return NextResponse.json({
...server,
message: "ok", message: "ok",
apps: allServices[parseInt(index)] || [], services: allServices[parseInt(index)] || [],
}); });
} }
+3 -2
View File
@@ -20,8 +20,6 @@ export async function GET(request: Request) {
if (query) { if (query) {
let tokens = (query || "").toLocaleLowerCase().split(" "); let tokens = (query || "").toLocaleLowerCase().split(" ");
console.log(tokens);
return NextResponse.json({ return NextResponse.json({
message: "ok", message: "ok",
servers: currentServers.filter((s) => servers: currentServers.filter((s) =>
@@ -62,6 +60,7 @@ export async function POST(request: Request) {
ip: body.ip, ip: body.ip,
coordinates: body.coordinates, coordinates: body.coordinates,
location: body.location, location: body.location,
apps: body.apps,
}); });
currentServers.push({ currentServers.push({
@@ -69,6 +68,7 @@ export async function POST(request: Request) {
ip: body.ip, ip: body.ip,
coordinates: body.coordinates, coordinates: body.coordinates,
location: body.location, location: body.location,
apps: body.apps,
status: "offline", status: "offline",
cpu: 0, cpu: 0,
memory: 0, memory: 0,
@@ -81,6 +81,7 @@ export async function POST(request: Request) {
ip: body.ip, ip: body.ip,
coordinates: body.coordinates, coordinates: body.coordinates,
location: body.location, location: body.location,
apps: body.apps,
}, servers.data.length - 1); }, servers.data.length - 1);
servers.write(); servers.write();
-1
View File
@@ -30,7 +30,6 @@ export const useServer = (server: ServerAPI, index: number) => {
currentServers[index].status = 'online'; currentServers[index].status = 'online';
}).catch((e) => { }).catch((e) => {
currentServers[index].status = 'offline'; currentServers[index].status = 'offline';
console.error(e.data);
}) })
}, 2000); }, 2000);
}; };
+9 -5
View File
@@ -106,20 +106,23 @@ export default function ServerComponent({
server, server,
index, index,
session, session,
nonInteractive,
}: { }: {
server: Server; server: Server;
index: number; index: number;
session: Session | null; session: Session | null;
nonInteractive?: boolean;
}) { }) {
const [deleteAlert, setDeleteAlert] = useState(false); const [deleteAlert, setDeleteAlert] = useState(false);
return ( return (
<> <>
<ContextMenu> <ContextMenu>
<ContextMenuTrigger> <ContextMenuTrigger disabled={nonInteractive}>
<Card <Card
className="w-full transition-transform duration-500 hover:scale-102 hover:cursor-pointer !select-none" className={cn("w-full transition-transform duration-500 !select-none", nonInteractive ? "" : "hover:scale-102 hover:cursor-pointer")}
onClick={() => { onClick={() => {
if (nonInteractive) return;
redirect(`/servers/${index}`); redirect(`/servers/${index}`);
}} }}
> >
@@ -183,7 +186,7 @@ export default function ServerComponent({
</ContextMenuItem> </ContextMenuItem>
<ContextMenuItem <ContextMenuItem
className="text-destructive" className="text-destructive"
onClick={() => setDeleteAlert(true)} onClick={() => {if (nonInteractive) return;setDeleteAlert(true)}}
> >
<Trash className="text-current" /> <Trash className="text-current" />
Delete Delete
@@ -204,7 +207,8 @@ export default function ServerComponent({
<AlertDialogCancel>Cancel</AlertDialogCancel> <AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction <AlertDialogAction
className="text-destructive-foreground bg-destructive" className="text-destructive-foreground bg-destructive"
onClick={() => onClick={() => {
if (nonInteractive) return;
session session
?.removeServer(index) ?.removeServer(index)
.then(() => { .then(() => {
@@ -215,7 +219,7 @@ export default function ServerComponent({
`Error deleting server: ` + e.response.data.message `Error deleting server: ` + e.response.data.message
); );
}) })
} }}
> >
Delete Delete
</AlertDialogAction> </AlertDialogAction>
+8
View File
@@ -311,3 +311,11 @@ body {
.offline { .offline {
color: var(--chart-4) !important; color: var(--chart-4) !important;
} }
.running {
color: var(--chart-3) !important;
}
.exited {
color: var(--chart-4) !important;
}
+15 -6
View File
@@ -3,12 +3,16 @@ import useSession from "@/hooks/useSession";
import Server from "@/types/server"; import Server from "@/types/server";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import ServiceComponent from "./service"; import ServiceComponent from "./service";
import ServerComponent from "@/app/dashboard/server";
import Service, { AppService } from "@/types/service";
interface Server2 extends Server { interface Server2 extends Server {
apps: { name?: string }[] services: Service[],
apps: AppService[],
} }
export default function ServerPage() { export default function ServerPage(params: { server: string }) {
const index = parseInt(params.server) || 0;
const session = useSession(); const session = useSession();
const [server, setServer] = useState<Server2 | undefined>(); const [server, setServer] = useState<Server2 | undefined>();
@@ -24,10 +28,15 @@ export default function ServerPage() {
}, [session]); }, [session]);
return server && ( return server && (
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-5"> <div className="gap-4 flex flex-col">
{server.apps.map((a, i) => ( <div>
<ServiceComponent key={i} service={a} index={i} session={session} / > <ServerComponent server={server} session={session} index={index} nonInteractive />
))} </div>
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-5">
{server.services.map((a, i) => (
<ServiceComponent key={i} service={a} index={i} session={session} / >
))}
</div>
</div> </div>
); );
} }
+2 -1
View File
@@ -101,7 +101,8 @@ export default function ServiceComponent({
<CardTitle>{service.name}</CardTitle> <CardTitle>{service.name}</CardTitle>
{(service.description && <CardDescription className="flex justify-between"> {(service.description && <CardDescription className="flex justify-between">
{service.description} {(service as any).cpu} <div><p className={service.active_state}>{service.active_state[0].toUpperCase() + service.active_state.slice(1)}</p> {service.description}</div>
{(service as any).cpu}
</CardDescription>)} </CardDescription>)}
</div> </div>
</div> </div>
+1 -1
View File
@@ -19,7 +19,7 @@ export default function useSession() {
setSession(new Session(sessionId)); setSession(new Session(sessionId));
}) })
.catch((e) => { .catch((e) => {
toast.error(e.response.data.message); toast.error(e.response?.data.message || "Unknown error occurred: "+e);
router.replace("/login"); router.replace("/login");
}); });
else { else {
+3
View File
@@ -1,8 +1,11 @@
import { AppService } from "./service";
export interface ServerAPI { export interface ServerAPI {
name: string; name: string;
ip: string; ip: string;
location?: string; location?: string;
coordinates?: [number, number]; coordinates?: [number, number];
apps: AppService[];
} }
export default interface Server extends ServerAPI { export default interface Server extends ServerAPI {
+2 -1
View File
@@ -1 +1,2 @@
export default interface Service { name?: string, description?: string }; export interface AppService { name?: string, description?: string };
export default interface Service { name?: string, description?: string, active_state: string };