Add routes (untested)
This commit is contained in:
parent
db20819ad2
commit
d346ccf701
9 changed files with 132 additions and 13 deletions
28
app/api/auth/login/route.ts
Normal file
28
app/api/auth/login/route.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const { username, password } = await req.json();
|
||||
|
||||
const res = await fetch("http://localhost:3001/auth/login", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization:
|
||||
"Basic " + Buffer.from(`${username}:${password}`).toString("base64"),
|
||||
},
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
return NextResponse.json({ error: "Invalid credentials" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { token } = await res.json();
|
||||
const response = NextResponse.json({ success: true });
|
||||
response.cookies.set("token", token, {
|
||||
httpOnly: true,
|
||||
secure: true,
|
||||
sameSite: "strict",
|
||||
maxAge: 60 * 60 * 8,
|
||||
path: "/",
|
||||
});
|
||||
return response;
|
||||
}
|
||||
7
app/api/auth/logout/route.ts
Normal file
7
app/api/auth/logout/route.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import { NextResponse } from "next/server";
|
||||
|
||||
export async function POST() {
|
||||
const response = NextResponse.json({ success: true });
|
||||
response.cookies.delete("token");
|
||||
return response;
|
||||
}
|
||||
29
app/api/services/[service]/[action]/route.ts
Normal file
29
app/api/services/[service]/[action]/route.ts
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
const ALLOWED_ACTIONS = ["start", "stop", "restart"];
|
||||
|
||||
export async function POST(
|
||||
req: NextRequest,
|
||||
{ params }: { params: { service: string; action: string } },
|
||||
) {
|
||||
const token = req.cookies.get("token")?.value;
|
||||
if (!token) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
if (!ALLOWED_ACTIONS.includes(params.action)) {
|
||||
return NextResponse.json({ error: "Invalid action" }, { status: 400 });
|
||||
}
|
||||
|
||||
const res = await fetch(
|
||||
`http://localhost:3001/services/${params.service}/${params.action}`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
return NextResponse.json(await res.json(), { status: res.status });
|
||||
}
|
||||
22
app/api/services/[service]/logs/route.ts
Normal file
22
app/api/services/[service]/logs/route.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
export async function GET(
|
||||
req: NextRequest,
|
||||
{ params }: { params: { service: string } },
|
||||
) {
|
||||
const token = req.cookies.get("token")?.value;
|
||||
if (!token) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const res = await fetch(
|
||||
`http://localhost:3001/services/${params.service}/logs`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
return NextResponse.json(await res.json(), { status: res.status });
|
||||
}
|
||||
17
app/api/system/reboot/route.ts
Normal file
17
app/api/system/reboot/route.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const token = req.cookies.get("token")?.value;
|
||||
if (!token) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const res = await fetch("http://localhost:3001/system/reboot", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(await res.json(), { status: res.status });
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue