bonus-edge-proxy/src/web/web_server.h

56 lines
1.4 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.

// 文件名: src/web/web_server.h
#ifndef WEB_SERVER_H
#define WEB_SERVER_H
#include "crow.h" // 引入 Crow 库的头文件
#include "systemMonitor/system_monitor.h"
#include <thread>
/**
* @brief Web服务器模块
* * 负责启动一个后台HTTP服务器为前端提供RESTful API接口。
* 它在自己的线程中运行以避免阻塞主程序的io_context事件循环。
*/
class WebServer {
public:
/**
* @brief 构造函数
* @param monitor SystemMonitor的引用用于获取系统状态数据
* @param port Web服务器监听的端口号默认为8080
*/
WebServer(SystemMonitor::SystemMonitor& monitor, uint16_t port = 8080);
/**
* @brief 析构函数
* * 确保在程序退出时Web服务线程能够被安全地停止和清理。
*/
~WebServer();
// 禁止拷贝和赋值,因为该类管理着一个线程资源
WebServer(const WebServer&) = delete;
WebServer& operator=(const WebServer&) = delete;
/**
* @brief 在后台线程中启动Web服务
*/
void start();
/**
* @brief 停止Web服务并等待线程退出
*/
void stop();
private:
/**
* @brief 设置所有的API路由URL路径
*/
void setup_routes();
crow::SimpleApp m_app; // Crow 应用实例
SystemMonitor::SystemMonitor& m_monitor;
uint16_t m_port;
std::thread m_thread; // 运行Web服务的后台线程
};
#endif // WEB_SERVER_H