ORE Studio 0.0.4
Loading...
Searching...
No Matches
route.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2025 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_HTTP_DOMAIN_ROUTE_HPP
21#define ORES_HTTP_DOMAIN_ROUTE_HPP
22
23#include <regex>
24#include <string>
25#include <vector>
26#include <optional>
27#include <functional>
28#include <boost/asio/awaitable.hpp>
29#include "ores.http.api/domain/http_method.hpp"
30#include "ores.http.api/domain/http_request.hpp"
31#include "ores.http.api/domain/http_response.hpp"
32
33namespace ores::http::domain {
34
38struct query_param final {
39 std::string name;
40 std::string type = "string";
41 std::string format; // Optional format (e.g., uuid, date)
42 bool required = false;
43 std::string description;
44 std::optional<std::string> default_value;
45};
46
53struct request_body_schema final {
54 std::string content_type = "application/json";
55 bool required = true;
56 std::string json_schema; // JSON schema from rfl::json::to_schema<T>()
57 std::string example_json; // Example JSON from generator
58};
59
66struct response_schema final {
67 std::string status_code = "200";
68 std::string description = "Successful response";
69 std::string content_type = "application/json";
70 std::string json_schema; // JSON schema from rfl::json::to_schema<T>()
71 std::string example_json; // Example JSON from generator
72};
73
77using request_handler = std::function<
78 boost::asio::awaitable<http_response>(const http_request&)>;
79
83struct route final {
87 http_method method;
88
92 std::string pattern;
93
97 std::regex regex;
98
102 std::vector<std::string> param_names;
103
107 request_handler handler;
108
112 bool requires_auth = false;
113
117 std::vector<std::string> required_roles;
118
122 std::string summary;
123
127 std::string description;
128
132 std::vector<std::string> tags;
133
137 std::vector<query_param> query_params;
138
142 std::optional<request_body_schema> body_schema;
143
147 std::optional<response_schema> success_response_schema;
148
153 bool match(const std::string& path,
154 std::unordered_map<std::string, std::string>& path_params) const;
155};
156
157}
158
159#endif
Represents an incoming HTTP request.
Definition http_request.hpp:35
Describes a query parameter for OpenAPI.
Definition route.hpp:38
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
bool requires_auth
Whether this route requires authentication.
Definition route.hpp:112
std::string description
OpenAPI description for documentation.
Definition route.hpp:127
std::vector< std::string > param_names
Names of path parameters in order.
Definition route.hpp:102
std::vector< std::string > required_roles
Required roles for accessing this route (empty = any authenticated user).
Definition route.hpp:117
std::vector< std::string > tags
OpenAPI tags for grouping.
Definition route.hpp:132
request_handler handler
Handler function for the route.
Definition route.hpp:107
http_method method
HTTP method for this route.
Definition route.hpp:87
std::string summary
OpenAPI summary for documentation.
Definition route.hpp:122
std::optional< request_body_schema > body_schema
Request body schema for POST/PUT/PATCH routes.
Definition route.hpp:142
std::string pattern
Original path pattern (e.g., "/users/{id}").
Definition route.hpp:92
std::vector< query_param > query_params
Query parameters for this route.
Definition route.hpp:137
std::regex regex
Compiled regex for matching.
Definition route.hpp:97
std::optional< response_schema > success_response_schema
Response schema for the 200 response.
Definition route.hpp:147
bool match(const std::string &path, std::unordered_map< std::string, std::string > &path_params) const
Attempts to match the given path and extracts parameters.
Definition route.cpp:24