mirror of
https://github.com/ChenQihan666/Lolia-Nodes-Status-Pages.git
synced 2026-08-14 07:52:34 +08:00
Initial commit
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
import { maintenances, workerConfig } from '@/uptime.config'
|
||||
import { NextRequest } from 'next/server'
|
||||
import { CompactedMonitorStateWrapper, getFromStore } from '@/worker/src/store'
|
||||
|
||||
export const runtime = 'edge'
|
||||
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Methods': 'GET, OPTIONS',
|
||||
'Access-Control-Allow-Headers': 'Content-Type',
|
||||
}
|
||||
|
||||
export default async function handler(req: NextRequest): Promise<Response> {
|
||||
const compactedState = new CompactedMonitorStateWrapper(
|
||||
await getFromStore(process.env as any, 'state')
|
||||
)
|
||||
|
||||
if (compactedState.data.lastUpdate === 0) {
|
||||
return new Response(JSON.stringify({ error: 'No data available' }), {
|
||||
status: 500,
|
||||
headers,
|
||||
})
|
||||
}
|
||||
|
||||
let monitors: any = {}
|
||||
|
||||
for (let monitor of workerConfig.monitors) {
|
||||
const lastIncident = compactedState.getIncident(
|
||||
monitor.id,
|
||||
compactedState.incidentLen(monitor.id) - 1
|
||||
)
|
||||
|
||||
const isUp = lastIncident?.end !== null
|
||||
const latency = compactedState.getLastLatency(monitor.id)
|
||||
monitors[monitor.id] = {
|
||||
up: isUp,
|
||||
latency: latency.ping,
|
||||
location: latency.loc,
|
||||
message: isUp ? 'OK' : lastIncident?.error[lastIncident.error.length - 1],
|
||||
}
|
||||
}
|
||||
|
||||
let ret = {
|
||||
up: compactedState.data.overallUp,
|
||||
down: compactedState.data.overallDown,
|
||||
updatedAt: compactedState.data.lastUpdate,
|
||||
monitors,
|
||||
maintenances,
|
||||
}
|
||||
|
||||
return new Response(JSON.stringify(ret), {
|
||||
headers,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user