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,75 @@
|
||||
import { NextRequest } from 'next/server'
|
||||
import { CompactedMonitorStateWrapper, getFromStore } from '@/worker/src/store'
|
||||
|
||||
export const runtime = 'edge'
|
||||
|
||||
type BadgePayload = {
|
||||
schemaVersion: 1
|
||||
label: string
|
||||
message: string
|
||||
color: string
|
||||
isError?: boolean
|
||||
}
|
||||
|
||||
const jsonHeaders = {
|
||||
'Content-Type': 'application/json',
|
||||
'Cache-Control': 'no-store, max-age=0, must-revalidate',
|
||||
}
|
||||
|
||||
function errorBadge(label: string, message: string): BadgePayload {
|
||||
return {
|
||||
schemaVersion: 1,
|
||||
label,
|
||||
message,
|
||||
color: 'lightgrey',
|
||||
isError: true,
|
||||
}
|
||||
}
|
||||
|
||||
export default async function handler(req: NextRequest): Promise<Response> {
|
||||
try {
|
||||
const url = new URL(req.url)
|
||||
|
||||
const monitorId = url.searchParams.get('id')
|
||||
const label = url.searchParams.get('label') ?? monitorId ?? 'UptimeFlare'
|
||||
|
||||
const upMsg = url.searchParams.get('up') ?? 'UP'
|
||||
const downMsg = url.searchParams.get('down') ?? 'DOWN'
|
||||
const colorUp = url.searchParams.get('colorUp') ?? 'brightgreen'
|
||||
const colorDown = url.searchParams.get('colorDown') ?? 'red'
|
||||
|
||||
if (!monitorId) {
|
||||
return new Response(JSON.stringify(errorBadge(label, 'no-monitor')), {
|
||||
headers: jsonHeaders,
|
||||
status: 400,
|
||||
})
|
||||
}
|
||||
|
||||
const compactedState = new CompactedMonitorStateWrapper(
|
||||
await getFromStore(process.env as any, 'state')
|
||||
)
|
||||
|
||||
const lastIncident = compactedState.getIncident(
|
||||
monitorId,
|
||||
compactedState.incidentLen(monitorId) - 1
|
||||
)
|
||||
const isUp = lastIncident?.end !== null
|
||||
|
||||
const badge: BadgePayload = {
|
||||
schemaVersion: 1,
|
||||
label,
|
||||
message: isUp ? upMsg : downMsg,
|
||||
color: isUp ? colorUp : colorDown,
|
||||
}
|
||||
|
||||
return new Response(JSON.stringify(badge), {
|
||||
headers: jsonHeaders,
|
||||
})
|
||||
} catch (err) {
|
||||
console.error('Error rendering badge API:', err)
|
||||
return new Response(JSON.stringify(errorBadge('status', 'error')), {
|
||||
headers: jsonHeaders,
|
||||
status: 500,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -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