http_server.hpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. };
  13. class HttpServer {
  14. public:
  15. explicit HttpServer(const HttpServerConfig& config);
  16. ~HttpServer();
  17. // Start/stop server
  18. void start();
  19. void stop();
  20. // Get underlying httplib server for route registration
  21. httplib::Server& server() { return server_; }
  22. // Register route with auth middleware
  23. void registerRoute(const std::string& method,
  24. const std::string& pattern,
  25. auth::AuthMiddleware::Handler handler,
  26. auth::AuthMiddleware* auth = nullptr,
  27. const std::string& required_role = "");
  28. // Static file serving setup
  29. void setupStaticFiles();
  30. bool isRunning() const { return running_; }
  31. private:
  32. HttpServerConfig config_;
  33. httplib::Server server_;
  34. std::thread server_thread_;
  35. bool running_ = false;
  36. };
  37. } // namespace smartbotic::webserver