20#ifndef ORES_HTTP_NET_ROUTER_HPP
21#define ORES_HTTP_NET_ROUTER_HPP
27#include <rfl/json.hpp>
28#include "ores.http.api/domain/route.hpp"
29#include "ores.logging/make_logger.hpp"
31namespace ores::http::net {
38 route_builder(domain::http_method method,
const std::string& pattern);
74 const std::string& type =
"string",
75 const std::string& format =
"",
76 bool required =
false,
77 const std::string& desc =
"",
78 const std::optional<std::string>& default_value = std::nullopt);
93 std::function<T()> example_generator =
nullptr,
95 const std::string& content_type =
"application/json") {
98 schema.content_type = content_type;
99 schema.required = required;
100 schema.json_schema = rfl::json::to_schema<T>();
102 if (example_generator) {
103 schema.example_json = rfl::json::write(example_generator());
106 body_schema_ = schema;
123 std::function<T()> example_generator =
nullptr,
124 const std::string& desc =
"Successful response",
125 const std::string& content_type =
"application/json") {
128 schema.status_code =
"200";
129 schema.description = desc;
130 schema.content_type = content_type;
131 schema.json_schema = rfl::json::to_schema<T>();
133 if (example_generator) {
134 schema.example_json = rfl::json::write(example_generator());
137 success_response_schema_ = schema;
147 domain::http_method method_;
148 std::string pattern_;
149 domain::request_handler handler_;
150 bool requires_auth_ =
false;
151 std::vector<std::string> required_roles_;
152 std::string summary_;
153 std::string description_;
154 std::vector<std::string> tags_;
155 std::vector<domain::query_param> query_params_;
156 std::optional<domain::request_body_schema> body_schema_;
157 std::optional<domain::response_schema> success_response_schema_;
200 std::optional<domain::route>
match(domain::http_method method,
201 const std::string& path,
202 std::unordered_map<std::string, std::string>& path_params)
const;
207 const std::vector<domain::route>&
routes()
const {
return routes_; }
210 inline static std::string_view logger_name =
"ores.http.net.router";
214 static auto instance = make_logger(logger_name);
218 std::vector<domain::route> routes_;
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
Describes the request body schema for OpenAPI.
Definition route.hpp:53
Describes the response schema for OpenAPI.
Definition route.hpp:66
Represents a registered route with pattern matching.
Definition route.hpp:83
Route builder for fluent route registration.
Definition router.hpp:36
route_builder & roles(std::vector< std::string > r)
Sets required roles for this route.
Definition router.cpp:86
route_builder & auth_required()
Marks this route as requiring authentication.
Definition router.cpp:81
domain::route build() const
Builds the route.
Definition router.cpp:125
route_builder & body(std::function< T()> example_generator=nullptr, bool required=true, const std::string &content_type="application/json")
Sets the request body schema from a C++ type using reflection.
Definition router.hpp:92
route_builder & query_param(const std::string &name, const std::string &type="string", const std::string &format="", bool required=false, const std::string &desc="", const std::optional< std::string > &default_value=std::nullopt)
Adds a query parameter for OpenAPI documentation.
Definition router.cpp:107
route_builder & handler(domain::request_handler h)
Sets the handler for this route.
Definition router.cpp:76
route_builder & description(std::string d)
Sets OpenAPI description.
Definition router.cpp:97
route_builder & response(std::function< T()> example_generator=nullptr, const std::string &desc="Successful response", const std::string &content_type="application/json")
Sets the response schema from a C++ type using reflection.
Definition router.hpp:122
route_builder & tags(std::vector< std::string > t)
Sets OpenAPI tags.
Definition router.cpp:102
route_builder & summary(std::string s)
Sets OpenAPI summary.
Definition router.cpp:92
HTTP request router that matches requests to handlers.
Definition router.hpp:163
route_builder delete_(const std::string &pattern)
Registers a DELETE route.
Definition router.cpp:167
void add_route(const domain::route &route)
Adds a built route to the router.
Definition router.cpp:171
route_builder put(const std::string &pattern)
Registers a PUT route.
Definition router.cpp:159
route_builder post(const std::string &pattern)
Registers a POST route.
Definition router.cpp:155
route_builder get(const std::string &pattern)
Registers a GET route.
Definition router.cpp:151
std::optional< domain::route > match(domain::http_method method, const std::string &path, std::unordered_map< std::string, std::string > &path_params) const
Attempts to match a request and returns the matching route.
Definition router.cpp:177
route_builder patch(const std::string &pattern)
Registers a PATCH route.
Definition router.cpp:163
const std::vector< domain::route > & routes() const
Returns all registered routes (for OpenAPI generation).
Definition router.hpp:207