mirror of
https://github.com/hamster1963/nezha-dash.git
synced 2025-04-24 21:10:45 +08:00
feat: add cloudflare support
This commit is contained in:
parent
b02a8604be
commit
4208f16199
@ -1,22 +1,13 @@
|
|||||||
import ServerList from "@/components/ServerList";
|
import ServerList from "@/components/ServerList";
|
||||||
import ServerOverview from "@/components/ServerOverview";
|
import ServerOverview from "@/components/ServerOverview";
|
||||||
import { GetNezhaData } from "@/lib/serverFetch";
|
|
||||||
|
|
||||||
import { SWRConfig } from "swr";
|
export const runtime = 'edge';
|
||||||
|
|
||||||
export default function Home() {
|
export default function Home() {
|
||||||
return (
|
return (
|
||||||
<SWRConfig
|
<div className="mx-auto grid w-full max-w-5xl gap-4 md:gap-6">
|
||||||
value={{
|
<ServerOverview />
|
||||||
fallback: {
|
<ServerList />
|
||||||
"/api/server": GetNezhaData(),
|
</div>
|
||||||
},
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<div className="mx-auto grid w-full max-w-5xl gap-4 md:gap-6">
|
|
||||||
<ServerOverview />
|
|
||||||
<ServerList />
|
|
||||||
</div>
|
|
||||||
</SWRConfig>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
import { GetNezhaData } from "@/lib/serverFetch";
|
import { NezhaAPI, ServerApi } from "@/app/types/nezha-api";
|
||||||
|
import { MakeOptional } from "@/app/types/utils";
|
||||||
|
import getEnv from "@/lib/env-entry";
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
|
export const runtime = 'edge';
|
||||||
|
|
||||||
export async function GET(_: Request) {
|
export async function GET(_: Request) {
|
||||||
try {
|
try {
|
||||||
const response = await GetNezhaData();
|
const response = await GetNezhaData();
|
||||||
@ -15,3 +19,57 @@ export async function GET(_: Request) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function GetNezhaData() {
|
||||||
|
|
||||||
|
var nezhaBaseUrl = getEnv("NezhaBaseUrl");
|
||||||
|
if (!nezhaBaseUrl) {
|
||||||
|
console.log("NezhaBaseUrl is not set");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove trailing slash
|
||||||
|
if (nezhaBaseUrl[nezhaBaseUrl.length - 1] === "/") {
|
||||||
|
nezhaBaseUrl = nezhaBaseUrl.slice(0, -1);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const response = await fetch(nezhaBaseUrl + "/api/v1/server/details", {
|
||||||
|
headers: {
|
||||||
|
Authorization: getEnv("NezhaAuth") as string,
|
||||||
|
},
|
||||||
|
next: {
|
||||||
|
revalidate: 0,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const nezhaData = (await response.json()).result as NezhaAPI[];
|
||||||
|
const data: ServerApi = {
|
||||||
|
live_servers: 0,
|
||||||
|
offline_servers: 0,
|
||||||
|
total_bandwidth: 0,
|
||||||
|
result: [],
|
||||||
|
};
|
||||||
|
const timestamp = Date.now() / 1000;
|
||||||
|
data.result = nezhaData.map(
|
||||||
|
(element: MakeOptional<NezhaAPI, "ipv4" | "ipv6" | "valid_ip">) => {
|
||||||
|
if (timestamp - element.last_active > 300) {
|
||||||
|
data.offline_servers += 1;
|
||||||
|
element.online_status = false;
|
||||||
|
} else {
|
||||||
|
data.live_servers += 1;
|
||||||
|
element.online_status = true;
|
||||||
|
}
|
||||||
|
data.total_bandwidth += element.status.NetOutTransfer;
|
||||||
|
|
||||||
|
delete element.ipv4;
|
||||||
|
delete element.ipv6;
|
||||||
|
delete element.valid_ip;
|
||||||
|
|
||||||
|
return element;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
return data;
|
||||||
|
} catch (error) {
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
}
|
@ -1,62 +0,0 @@
|
|||||||
"use server";
|
|
||||||
|
|
||||||
import { NezhaAPI, ServerApi } from "@/app/types/nezha-api";
|
|
||||||
import { MakeOptional } from "@/app/types/utils";
|
|
||||||
import { error } from "console";
|
|
||||||
import { unstable_noStore as noStore } from "next/cache";
|
|
||||||
import getEnv from "./env-entry";
|
|
||||||
|
|
||||||
export async function GetNezhaData() {
|
|
||||||
noStore();
|
|
||||||
|
|
||||||
var nezhaBaseUrl = getEnv("NezhaBaseUrl");
|
|
||||||
if (!nezhaBaseUrl) {
|
|
||||||
error("NezhaBaseUrl is not set");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove trailing slash
|
|
||||||
if (nezhaBaseUrl[nezhaBaseUrl.length - 1] === "/") {
|
|
||||||
nezhaBaseUrl = nezhaBaseUrl.slice(0, -1);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
const response = await fetch(nezhaBaseUrl + "/api/v1/server/details", {
|
|
||||||
headers: {
|
|
||||||
Authorization: getEnv("NezhaAuth") as string,
|
|
||||||
},
|
|
||||||
next: {
|
|
||||||
revalidate: 0,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const nezhaData = (await response.json()).result as NezhaAPI[];
|
|
||||||
const data: ServerApi = {
|
|
||||||
live_servers: 0,
|
|
||||||
offline_servers: 0,
|
|
||||||
total_bandwidth: 0,
|
|
||||||
result: [],
|
|
||||||
};
|
|
||||||
const timestamp = Date.now() / 1000;
|
|
||||||
data.result = nezhaData.map(
|
|
||||||
(element: MakeOptional<NezhaAPI, "ipv4" | "ipv6" | "valid_ip">) => {
|
|
||||||
if (timestamp - element.last_active > 300) {
|
|
||||||
data.offline_servers += 1;
|
|
||||||
element.online_status = false;
|
|
||||||
} else {
|
|
||||||
data.live_servers += 1;
|
|
||||||
element.online_status = true;
|
|
||||||
}
|
|
||||||
data.total_bandwidth += element.status.NetOutTransfer;
|
|
||||||
|
|
||||||
delete element.ipv4;
|
|
||||||
delete element.ipv6;
|
|
||||||
delete element.valid_ip;
|
|
||||||
|
|
||||||
return element;
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
return data;
|
|
||||||
} catch (error) {
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user