Line data Source code
1 : #ifndef THINGER_HTTP_ROUTE_HANDLER_HPP
2 : #define THINGER_HTTP_ROUTE_HANDLER_HPP
3 :
4 : #include <map>
5 : #include <vector>
6 : #include <memory>
7 : #include "../request_handler.hpp"
8 : #include "route.hpp"
9 : #include "route_builder.hpp"
10 :
11 : namespace thinger::http {
12 :
13 : class route_handler : public request_handler {
14 : public:
15 : route_handler();
16 725 : virtual ~route_handler() = default;
17 :
18 : // Access route builders for different HTTP methods
19 : route_builder operator[](method http_method);
20 :
21 : // Handle incoming requests
22 : bool handle_request(std::shared_ptr<request> request) override;
23 :
24 : // Find the matching route for a request (without executing the handler)
25 : const route* find_route(std::shared_ptr<request> req);
26 :
27 : // Handle an unmatched request (404/fallback)
28 : void handle_unmatched(std::shared_ptr<request> req);
29 :
30 : // Enable CORS support
31 : void enable_cors(bool enabled = true);
32 :
33 : // Add a catch-all handler for unmatched routes
34 : void set_fallback_handler(std::function<void(request&, response&)> handler);
35 :
36 : // Get all registered routes (useful for API documentation)
37 : const std::map<method, std::vector<route>>& get_routes() const { return routes_; }
38 :
39 : private:
40 : std::map<method, std::vector<route>> routes_;
41 : bool cors_enabled_ = false;
42 : std::function<void(request&, response&)> fallback_handler_;
43 :
44 : // Helper function to send error responses
45 : void send_error_response(std::shared_ptr<request> req, http_response::status status);
46 :
47 : // Friend class to allow route_builder to access routes_
48 : friend class route_builder;
49 : };
50 :
51 : } // namespace thinger::http
52 :
53 : #endif // THINGER_HTTP_ROUTE_HANDLER_HPP
|