uvco 0.1
Loading...
Searching...
No Matches
http_server.h
Go to the documentation of this file.
1// uvco (c) 2025 Lewin Bormann. See LICENSE for specific terms.
2
3#pragma once
4
5#include <functional>
6#include <string>
7#include <string_view>
8#include <unordered_map>
9#include <utility>
10#include <vector>
11
12#include <fmt/core.h>
13#include <fmt/format.h>
14
16#include "uvco/tcp.h"
17
18namespace uvco::examples {
19
23
25 std::string_view method;
26 std::string_view path;
27 std::string_view version;
28 std::vector<std::pair<std::string_view, std::string_view>> headers;
29};
30
32 int statusCode = 200;
33 std::string statusMessage = "OK";
34 std::vector<std::pair<std::string_view, std::string>> headers;
35 std::string body;
36
38
39 std::string toString();
40};
41
42using HttpHandler = std::function<Promise<HttpResponse>(const HttpRequest &)>;
43
46class Router {
47public:
48 void addRoute(const std::string &path, HttpHandler handler);
49
50 [[nodiscard]] HttpHandler getHandler(const std::string &path) const;
51
52private:
53 std::unordered_map<std::string, HttpHandler> routes_;
54};
55
58
63
65
66} // namespace uvco::examples
Definition promise.h:49
Definition tcp.h:61
Definition http_server.h:46
std::unordered_map< std::string, HttpHandler > routes_
Definition http_server.h:53
void addRoute(const std::string &path, HttpHandler handler)
Definition http_server.cc:37
HttpHandler getHandler(const std::string &path) const
Definition http_server.cc:41
std::function< Promise< HttpResponse >(const HttpRequest &)> HttpHandler
Definition http_server.h:42
Promise< void > httpServer(TcpServer &server, Router router)
Definition http_server.cc:208
Promise< HttpResponse > notFoundHandler(const HttpRequest &)
A handler returning a 404 error.
Definition http_server.cc:49
Definition http_server.cc:19
Definition http_server.h:24
std::string_view method
Definition http_server.h:25
std::string_view path
Definition http_server.h:26
std::vector< std::pair< std::string_view, std::string_view > > headers
Definition http_server.h:28
std::string_view version
Definition http_server.h:27
HttpResponse()
Definition http_server.cc:21
std::vector< std::pair< std::string_view, std::string > > headers
Definition http_server.h:34
std::string statusMessage
Definition http_server.h:33
std::string body
Definition http_server.h:35
std::string toString()
Definition http_server.cc:23
int statusCode
Definition http_server.h:32