cocoon/dashboard.h
xfy911 d01a5d7559
Some checks failed
CI / build (push) Failing after 11s
feat: 实现管理 Dashboard(/_status + SSE 实时指标流)
- 新增 dashboard.c / dashboard.h,提供自包含 HTML 管理面板
- 复用现有 SSE 基础设施(sse.c/sse.h)实现实时指标推送
- 每 2 秒推送 JSON 指标(uptime/connections/requests/2xx/3xx/4xx/5xx),每 6 秒心跳
- 端点:/_status(HTML 面板)、/_status/events(SSE 指标流)
- 将 server.c 中的统计变量改为全局可见,供 dashboard 访问
- 新增 9 个单元测试覆盖 dashboard_handle_request / dashboard_handle_events
- 编译零警告,474 单元测试 + 111 集成测试通过

(4 个集成测试失败为既有反向代理环境问题,与本次改动无关)
2026-06-16 00:27:59 +08:00

55 lines
1.5 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* dashboard.h - 内置管理 Dashboard
*
* 提供 `/_status` HTML 管理页面和 `/_status/events` SSE 实时指标流。
*
* @author xfy
*/
#ifndef DASHBOARD_H
#define DASHBOARD_H
#include <stdint.h>
#include <stdbool.h>
#include <time.h>
#include <stdatomic.h>
#include "http.h"
/* 从 server.c 暴露的指标变量 */
extern atomic_int g_active_connections;
extern time_t g_server_start_time;
extern uint32_t g_max_connections;
extern atomic_uint g_total_requests;
extern atomic_uint g_response_2xx;
extern atomic_uint g_response_3xx;
extern atomic_uint g_response_4xx;
extern atomic_uint g_response_5xx;
extern atomic_uint g_response_200;
extern atomic_uint g_response_404;
/**
* dashboard_handle_request - 处理内置 Dashboard 页面请求
*
* 端点路径:/_status
* 返回自包含的 HTML 页面,通过 EventSource 连接 /_status/events 实时刷新指标。
*
* @param fd 客户端 socket
* @param req HTTP 请求
* @return true 请求已处理false 非 Dashboard 请求
*/
bool dashboard_handle_request(int fd, const http_request_t *req);
/**
* dashboard_sse_handle_request - 处理 Dashboard SSE 实时指标流
*
* 端点路径:/_status/events
* 以 SSE 格式每 2 秒推送一次当前服务器指标 JSON。
*
* @param fd 客户端 socket
* @param req HTTP 请求
* @return true 连接保持(已转入 SSE 模式false 非 SSE 请求或错误
*/
bool dashboard_sse_handle_request(int fd, const http_request_t *req);
#endif /* DASHBOARD_H */