diff --git a/README.md b/README.md index ed1f032..aa26433 100644 --- a/README.md +++ b/README.md @@ -6,13 +6,11 @@ ### 一键部署到 Vercel + [部署简易教程](https://buycoffee.top/blog/tech/nezha)

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fhamster1963%2Fnezha-dash&env=NezhaBaseUrl,NezhaAuth&project-name=nezha-dash&repository-name=nezha-dash) - - - ![screen-shot-one](/.github/shotOne.png) ![screen-shot-two](/.github/shotTwo.png) diff --git a/app/(main)/page.tsx b/app/(main)/page.tsx index 26e7e08..e7dcb53 100644 --- a/app/(main)/page.tsx +++ b/app/(main)/page.tsx @@ -1,14 +1,21 @@ import ServerList from "@/components/ServerList"; import ServerOverview from "@/components/ServerOverview"; - - +import { GetNezhaData } from "@/lib/prefetch"; +import { SWRConfig } from "swr"; export default function Home() { - return ( -
- - -
+ +
+ + +
+
); } diff --git a/app/api/server/route.ts b/app/api/server/route.ts index b08f38a..08789e0 100644 --- a/app/api/server/route.ts +++ b/app/api/server/route.ts @@ -1,56 +1,58 @@ - import { NezhaAPI, ServerApi } from "@/app/types/nezha-api"; import { MakeOptional } from "@/app/types/utils"; import { NextResponse } from "next/server"; -import { DateTime } from "luxon"; export async function GET(_: Request) { - if (!process.env.NezhaBaseUrl) { - return NextResponse.json({ error: 'NezhaBaseUrl is not set' }, { status: 400 }) - } + if (!process.env.NezhaBaseUrl) { + return NextResponse.json( + { error: "NezhaBaseUrl is not set" }, + { status: 400 }, + ); + } - // Remove trailing slash - var nezhaBaseUrl = process.env.NezhaBaseUrl; + // Remove trailing slash + var nezhaBaseUrl = process.env.NezhaBaseUrl; - if (process.env.NezhaBaseUrl[process.env.NezhaBaseUrl.length - 1] === '/') { - nezhaBaseUrl = process.env.NezhaBaseUrl.slice(0, -1); - } + if (process.env.NezhaBaseUrl[process.env.NezhaBaseUrl.length - 1] === "/") { + nezhaBaseUrl = process.env.NezhaBaseUrl.slice(0, -1); + } - try { - const response = await fetch(nezhaBaseUrl+ '/api/v1/server/details',{ - headers: { - 'Authorization': process.env.NezhaAuth as string - }, - next:{ - revalidate:1 - } - }); - const nezhaData = (await response.json()).result as NezhaAPI[]; - const data: ServerApi = { - live_servers: 0, - offline_servers: 0, - total_bandwidth: 0, - result: [] + try { + const response = await fetch(nezhaBaseUrl + "/api/v1/server/details", { + headers: { + Authorization: process.env.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) => { + if (timestamp - element.last_active > 300) { + data.offline_servers += 1; + } else { + data.live_servers += 1; } + data.total_bandwidth += element.status.NetOutTransfer; - data.result = nezhaData.map((element: MakeOptional) => { - if (DateTime.now().toUnixInteger() - element.last_active > 300) { - data.offline_servers += 1; - } else { - data.live_servers += 1; - } - data.total_bandwidth += element.status.NetOutTransfer; + delete element.ipv4; + delete element.ipv6; + delete element.valid_ip; - delete element.ipv4; - delete element.ipv6; - delete element.valid_ip; + return element; + }, + ); - return element; - }); - - return NextResponse.json(data, { status: 200 }) - } catch (error) { - return NextResponse.json({ error: error }, { status: 200 }) - } - -} \ No newline at end of file + return NextResponse.json(data, { status: 200 }); + } catch (error) { + return NextResponse.json({ error: error }, { status: 200 }); + } +} diff --git a/app/types/utils.ts b/app/types/utils.ts index 488df36..69b6f87 100644 --- a/app/types/utils.ts +++ b/app/types/utils.ts @@ -1 +1,2 @@ -export type MakeOptional = Omit & Partial>; +export type MakeOptional = Omit & + Partial>; diff --git a/bun.lockb b/bun.lockb index 23316e6..113abeb 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/lib/utils.ts b/lib/utils.ts index 4025a4e..6dcf264 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -6,15 +6,25 @@ export function cn(...inputs: ClassValue[]) { } export function formatBytes(bytes: number, decimals: number = 2) { - if (!+bytes) return '0 Bytes' + if (!+bytes) return "0 Bytes"; - const k = 1024 - const dm = decimals < 0 ? 0 : decimals - const sizes = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'] + const k = 1024; + const dm = decimals < 0 ? 0 : decimals; + const sizes = [ + "Bytes", + "KiB", + "MiB", + "GiB", + "TiB", + "PiB", + "EiB", + "ZiB", + "YiB", + ]; - const i = Math.floor(Math.log(bytes) / Math.log(k)) + const i = Math.floor(Math.log(bytes) / Math.log(k)); - return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}` + return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`; } export function getDaysBetweenDates(date1: string, date2: string): number { @@ -43,16 +53,15 @@ export const fetcher = (url: string) => }); export const nezhaFetcher = (url: string) => - fetch(url) - .then((res) => { - if (!res.ok) { - throw new Error(res.statusText); - } - return res.json(); - }) - .then((data) => data) - .catch((err) => { - console.error(err); - throw err; - }); - \ No newline at end of file + fetch(url) + .then((res) => { + if (!res.ok) { + throw new Error(res.statusText); + } + return res.json(); + }) + .then((data) => data) + .catch((err) => { + console.error(err); + throw err; + });