Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
RouteTypes.h
Go to the documentation of this file.
1#pragma once
2#include <string>
3#include <vector>
4#include <unordered_map>
5#include <optional>
6#include <functional>
7#include <regex>
8
9class HttpRequest;
10class HttpResponse;
11
18struct RouteMatch {
19 std::vector<std::string> ordered;
20 std::unordered_map<std::string, std::string> named;
21
22 std::optional<std::string> getParam(const std::string& name) const {
23 auto it = named.find(name);
24 if (it != named.end()) return it->second;
25 return std::nullopt;
26 }
27};
28
32using RouteHandler = std::function<void(HttpRequest&, HttpResponse&, const RouteMatch&)>;
33using Middleware = std::function<bool(HttpRequest&, HttpResponse&, const RouteMatch&)>;
34
41struct Route
42{
43 std::string method;
44 std::string path;
45 std::regex compiledRegex;
49 std::vector<std::string> paramNames;
50
51 Route(const std::string& m,
52 const std::string& p,
54 bool dynamic = false,
55 bool auth = false,
56 const std::vector<std::string>& params = {})
57 : method(m), path(p), compiledRegex(p), handler(h), isDynamic(dynamic), requiresAuth(auth), paramNames(params)
58 {
59 }
60 Route() = default;
61};
std::function< bool(HttpRequest &, HttpResponse &, const RouteMatch &)> Middleware
Function signature for middleware components.
Definition Middleware.h:50
std::function< void(HttpRequest &, HttpResponse &, const RouteMatch &)> RouteHandler
Type definitions for route handlers and middleware.
Definition RouteTypes.h:32
std::function< void(HttpRequest &, HttpResponse &, const RouteMatch &)> RouteHandler
Function signature for HTTP route handlers.
Definition Router.h:41
Forward declaration for potential routing needs.
Definition HttpRequest.h:32
Represents an HTTP response object.
Represents a match of a route against an incoming HTTP request.
Definition RouteTypes.h:18
std::vector< std::string > ordered
Definition RouteTypes.h:19
std::unordered_map< std::string, std::string > named
Definition RouteTypes.h:20
std::optional< std::string > getParam(const std::string &name) const
Definition RouteTypes.h:22
Represents a single HTTP route.
Definition RouteTypes.h:42
std::vector< std::string > paramNames
Definition RouteTypes.h:49
Route()=default
bool isDynamic
Definition RouteTypes.h:47
RouteHandler handler
Definition RouteTypes.h:46
bool requiresAuth
Definition RouteTypes.h:48
Route(const std::string &m, const std::string &p, RouteHandler h, bool dynamic=false, bool auth=false, const std::vector< std::string > &params={})
Definition RouteTypes.h:51
std::string method
Definition RouteTypes.h:43
std::string path
Definition RouteTypes.h:44
std::regex compiledRegex
Definition RouteTypes.h:45