#include "debug_server.hpp" #include #include #include #include #include #include using namespace Pistache; namespace Debugger { // Forward declare external services template<> std::unique_ptr CreateService(); template<> std::unique_ptr CreateService(); template<> std::unique_ptr CreateService(); class DebugServerImpl { std::vector> services; public: DebugServerImpl() { services.push_back(CreateService()); services.push_back(CreateService()); services.push_back(CreateService()); } void Run(unsigned port) { Address addr(Ipv4::any(), static_cast(port)); Http::Endpoint endpoint(addr); endpoint.init(Http::Endpoint::options().flags(Tcp::Options::NoDelay | Tcp::Options::ReuseAddr)); Rest::Router router; // Register services for (auto& service : services) { service->RegisterRoutes(router); } // Run server endpoint.setHandler(router.handler()); endpoint.serve(); } Service& GetService(const std::type_info& service_type) const { auto it = ranges::find_if(services, [&](const auto& service) -> bool { auto& the_service = *service; return (service_type == typeid(the_service)); }); if (it == services.end()) { throw std::runtime_error("Could not find service" + std::string { service_type.name() }); } return **it; } }; DebugServer::DebugServer() { impl = std::make_unique(); } DebugServer::~DebugServer() = default; void DebugServer::Run(unsigned port) { impl->Run(port); } Service& DebugServer::GetService(const std::type_info& service_type) const { return impl->GetService(service_type); } } // namespace Debugger