http_server.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #ifndef JTJAI_MEDIA_HTTP_SERVER_H
  2. #define JTJAI_MEDIA_HTTP_SERVER_H
  3. #include <string>
  4. #include <vector>
  5. #include <map>
  6. #include <functional>
  7. #include <memory>
  8. #include <thread>
  9. #include <atomic>
  10. #include <boost/asio.hpp>
  11. #include <boost/json.hpp>
  12. namespace jtjai_media {
  13. // 视频文件信息结构
  14. struct VideoFileInfo {
  15. std::string filename; // 文件名
  16. std::string full_path; // 完整路径
  17. std::string timestamp_dir; // 所属时间戳目录
  18. int64_t file_size; // 文件大小(字节)
  19. std::string created_time; // 创建时间
  20. std::string stream_index; // 流索引(从文件名推断)
  21. };
  22. // HTTP请求结构
  23. struct HttpRequest {
  24. std::string method; // GET, POST, DELETE等
  25. std::string path; // 请求路径
  26. std::string query_string; // 查询字符串
  27. std::string body; // 请求体
  28. std::map<std::string, std::string> headers; // 请求头
  29. };
  30. // HTTP响应结构
  31. struct HttpResponse {
  32. int status_code = 200; // 状态码
  33. std::string status_message = "OK"; // 状态消息
  34. std::string content_type = "application/json"; // 内容类型
  35. std::string body; // 响应体
  36. HttpResponse() = default;
  37. HttpResponse(int code, const std::string& msg, const std::string& content)
  38. : status_code(code), status_message(msg), body(content) {}
  39. };
  40. class HttpServer {
  41. public:
  42. explicit HttpServer(const std::string& output_directory, int port = 8080);
  43. ~HttpServer();
  44. // 启动服务器
  45. bool start();
  46. // 停止服务器
  47. void stop();
  48. // 检查服务器是否在运行
  49. bool is_running() const { return is_running_.load(); }
  50. // 获取服务器端口
  51. int get_port() const { return port_; }
  52. private:
  53. std::string output_directory_; // 输出目录
  54. int port_; // 监听端口
  55. std::atomic<bool> is_running_; // 运行状态
  56. std::unique_ptr<std::thread> server_thread_; // 服务器线程
  57. // 服务器主循环
  58. void server_main_loop();
  59. // 处理客户端连接
  60. void handle_client(boost::asio::ip::tcp::socket socket);
  61. // 解析HTTP请求
  62. HttpRequest parse_request(const std::string& request_data);
  63. // 构建HTTP响应
  64. std::string build_response(const HttpResponse& response);
  65. // 路由处理
  66. HttpResponse route_request(const HttpRequest& request);
  67. // API处理函数
  68. HttpResponse handle_list_videos(const HttpRequest& request);
  69. HttpResponse handle_list_timestamps(const HttpRequest& request);
  70. HttpResponse handle_delete_video(const HttpRequest& request);
  71. HttpResponse handle_delete_timestamp(const HttpRequest& request);
  72. HttpResponse handle_get_video_info(const HttpRequest& request);
  73. HttpResponse handle_video_file(const HttpRequest& request);
  74. HttpResponse handle_static_file(const HttpRequest& request);
  75. // 工具函数
  76. std::vector<std::string> list_timestamp_directories();
  77. std::vector<VideoFileInfo> list_videos_in_directory(const std::string& dir_path);
  78. std::vector<VideoFileInfo> list_all_videos();
  79. bool delete_file(const std::string& file_path);
  80. bool delete_directory(const std::string& dir_path);
  81. std::string get_file_created_time(const std::string& file_path);
  82. int64_t get_file_size(const std::string& file_path);
  83. std::map<std::string, std::string> parse_query_string(const std::string& query);
  84. std::string url_decode(const std::string& str);
  85. HttpResponse generate_index_page();
  86. HttpResponse generate_api_doc();
  87. };
  88. } // namespace jtjai_media
  89. #endif // JTJAI_MEDIA_HTTP_SERVER_H