Files
status/pages/api/data.ts
T
Bennet Galleinandlyc8503 a6c8eb6e12 maintenance mode (#101)
* feat: move types

* feat: add maintenance alert

* feat: move to config/base class to have fallback values on missing exports

* fix: wrong import for types

* Revert "fix: wrong import for types"

This reverts commit ca2b2e3a68464cfa1b4aef4d99978de02591df74.

* Revert "feat: move to config/base class to have fallback values on missing exports"

This reverts commit 9932ea2590193aabd248fbba6b2c3a215da45d18.

* fix: imports

* chore: formatting

* fix: handle filtering for global status

* fix: pass props

* fix: ignore active maintenances when sending notifications

* fix: mark as warning when monitor is in maintenance

* fix: title width & warning icon

* fix: remove accordeons again

* alert config/style changes, worker code improve

* update example config

* update example config

* set monitors to be optional

---------

Co-authored-by: lyc8503 <[email protected]>
2025-05-01 01:09:53 +08:00

48 lines
1.3 KiB
TypeScript

import { workerConfig } from '@/uptime.config'
import { MonitorState } from '@/types/config'
import { NextRequest } from 'next/server'
export const runtime = 'edge'
export default async function handler(req: NextRequest): Promise<Response> {
const { UPTIMEFLARE_STATE } = process.env as unknown as {
UPTIMEFLARE_STATE: KVNamespace
}
const stateStr = await UPTIMEFLARE_STATE?.get('state')
if (!stateStr) {
return new Response(JSON.stringify({ error: 'No data available' }), {
status: 500,
headers: {
'Content-Type': 'application/json',
},
})
}
const state = JSON.parse(stateStr) as unknown as MonitorState
let monitors: any = {}
for (let monitor of workerConfig.monitors) {
const isUp = state.incident[monitor.id].slice(-1)[0].end !== undefined
monitors[monitor.id] = {
up: isUp,
latency: state.latency[monitor.id].recent.slice(-1)[0].ping,
location: state.latency[monitor.id].recent.slice(-1)[0].loc,
message: isUp ? 'OK' : state.incident[monitor.id].slice(-1)[0].error.slice(-1)[0],
}
}
let ret = {
up: state.overallUp,
down: state.overallDown,
updatedAt: state.lastUpdate,
monitors: monitors,
}
return new Response(JSON.stringify(ret), {
headers: {
'Content-Type': 'application/json',
},
})
}