http_server.hpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #pragma once
  2. #include <string>
  3. #include <memory>
  4. #include <thread>
  5. #include <httplib.h>
  6. #include "auth/auth_middleware.hpp"
  7. namespace smartbotic::webserver {
  8. struct HttpServerConfig {
  9. int port = 8080;
  10. std::string static_files_path = "./webui/dist";
  11. int thread_pool_size = 8;
  12. int max_upload_mb = 32;
  13. };
  14. class HttpServer {
  15. public:
  16. explicit HttpServer(const HttpServerConfig& config);
  17. ~HttpServer();
  18. // Start/stop server
  19. void start();
  20. void stop();
  21. // Get underlying httplib server for route registration
  22. httplib::Server& server() { return server_; }
  23. // Register route with auth middleware
  24. void registerRoute(const std::string& method,
  25. const std::string& pattern,
  26. auth::AuthMiddleware::Handler handler,
  27. auth::AuthMiddleware* auth = nullptr,
  28. const std::string& required_role = "");
  29. // Static file serving setup
  30. void setupStaticFiles();
  31. bool isRunning() const { return running_; }
  32. private:
  33. HttpServerConfig config_;
  34. httplib::Server server_;
  35. std::thread server_thread_;
  36. bool running_ = false;
  37. };
  38. } // namespace smartbotic::webserver