Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
Config.hpp
Go to the documentation of this file.
1#pragma once
3#include <string>
4#include <vector>
5#include <cstdint>
6#include <chrono>
7
8namespace orbit::config {
9
10enum class EventEngine {
11 Epoll,
12 IoUring,
13 // io_uring when the kernel supports it (Linux 5.7+, fast poll),
14 // otherwise epoll; decided once when the App starts listening.
15 Auto
16};
17
18enum class HttpVersion {
19 Http1_1,
20 Http2,
21 Http3
22};
23
26 std::string cert_file;
27 std::string key_file;
28};
29
31 uint16_t port{8080};
32 // Address to listen on: "0.0.0.0" (all IPv4 interfaces), "::" (all IPv6
33 // and IPv4, dual-stack), "127.0.0.1" / "::1" (local only), or a host name.
34 std::string host{"0.0.0.0"};
35 int backlog{0}; // Pending-connection queue length; 0 = SOMAXCONN
36 // Open connections served at once; 0 = unlimited. Beyond it, new
37 // connections wait in the listen backlog until a slot frees up.
38 size_t max_connections{0};
39 // Threads running handlers. 0 = one per CPU this process may run on
40 // (its affinity mask, so containers and taskset are respected).
41 size_t worker_threads{4};
42 // Event loops (reactors), each on its own thread with its own listening
43 // socket (SO_REUSEPORT; the kernel spreads connections across them).
44 // A connection stays on the loop that accepted it, TLS included. Handlers
45 // still run on the worker_threads pool. Linux only: elsewhere 1 is used.
46 // 0 (the default) = one per CPU in the process's affinity mask.
47 size_t event_loops{0};
48 // Pin event loop i to the i-th CPU of the affinity mask (Linux). The
49 // first loop runs on the thread that called listen(), which is pinned too.
50 bool cpu_affinity{false};
51 // Logging is process-wide (utils::Logger). An App applies these only when
52 // they differ from the defaults, so creating an App never resets a level
53 // or format set earlier by Logger::init()/set_format() or by another App.
54 std::string log_level{"INFO"};
55 std::string log_format{"text"}; // "text" or "json" (one JSON object per line)
56 std::string static_dir{"./public"};
57 size_t max_body_size{10485760}; // Default 10 MB limit
58 std::string ssl_cert{""};
59 std::string ssl_key{""};
60 // More certificates, chosen by the name the client asks for (SNI). Each
61 // answers for the DNS names in its subjectAltName (or its CN); clients
62 // asking for any other name, or none, get ssl_cert.
63 std::vector<TlsCertificate> sni_certificates;
64 // How often to check the certificate and key files for changes and
65 // reload them; 0 = never. SIGHUP and App::reload_tls() reload at once.
66 std::chrono::seconds tls_reload_interval{0};
67 EventEngine engine{EventEngine::Epoll}; // Default to our new fast backend!
69
70 // Connection timeouts. A value of zero disables that timeout.
71 std::chrono::seconds header_timeout{10}; // From the first byte of a request until its headers are complete; not extended by further bytes
72 std::chrono::seconds keep_alive_timeout{10}; // Idle time allowed between requests on a persistent connection
73 std::chrono::seconds idle_timeout{30}; // Longest pause while receiving a body or writing a response
74 std::chrono::seconds websocket_idle_timeout{0}; // Idle time on WebSocket/raw streams; 0 = never (use pings to detect dead peers)
75 // Server-sent WebSocket pings; 0 = none. With websocket_idle_timeout set
76 // longer than this, live peers (which answer with a pong) stay connected
77 // and dead ones are closed.
78 std::chrono::seconds websocket_ping_interval{0};
79
80 // Request limits (HTTP/1.1). Oversized headers get 431, bodies 413.
81 size_t max_header_bytes{8192}; // Request line plus all headers
82 size_t max_request_line{4096}; // Method, target and version
83 size_t max_headers{100}; // Header fields per request
84 size_t websocket_max_message_size{16 * 1024 * 1024}; // Per message, after decompression; larger closes with 1009
85 // On SIGTERM/SIGINT (or App::shutdown()), how long in-flight requests may
86 // finish before remaining connections are closed. A second signal stops at once.
87 std::chrono::seconds shutdown_timeout{30};
88
90 enum class ParseMode {
91 Lenient,
92 Strict,
93 };
94
102 static ServerConfig parse(int argc, char* argv[], ParseMode mode = ParseMode::Lenient);
105 static ServerConfig parse(int argc, char* argv[], ServerConfig base, ParseMode mode = ParseMode::Lenient);
106};
107
108} // namespace config
Definition Config.hpp:8
EventEngine
Definition Config.hpp:10
HttpVersion
Definition Config.hpp:18
Definition Config.hpp:30
std::chrono::seconds websocket_ping_interval
Definition Config.hpp:78
std::chrono::seconds header_timeout
Definition Config.hpp:71
std::chrono::seconds shutdown_timeout
Definition Config.hpp:87
size_t worker_threads
Definition Config.hpp:41
std::vector< TlsCertificate > sni_certificates
Definition Config.hpp:63
bool cpu_affinity
Definition Config.hpp:50
std::chrono::seconds idle_timeout
Definition Config.hpp:73
std::chrono::seconds keep_alive_timeout
Definition Config.hpp:72
std::string host
Definition Config.hpp:34
std::string ssl_key
Definition Config.hpp:59
int backlog
Definition Config.hpp:35
std::string log_level
Definition Config.hpp:54
size_t websocket_max_message_size
Definition Config.hpp:84
static ServerConfig parse(int argc, char *argv[], ServerConfig base, ParseMode mode=ParseMode::Lenient)
uint16_t port
Definition Config.hpp:31
size_t max_connections
Definition Config.hpp:38
size_t max_headers
Definition Config.hpp:83
static ServerConfig parse(int argc, char *argv[], ParseMode mode=ParseMode::Lenient)
Reads the server's command-line flags (see –help).
std::chrono::seconds tls_reload_interval
Definition Config.hpp:66
std::chrono::seconds websocket_idle_timeout
Definition Config.hpp:74
EventEngine engine
Definition Config.hpp:67
std::string static_dir
Definition Config.hpp:56
HttpVersion http_version
Definition Config.hpp:68
size_t max_request_line
Definition Config.hpp:82
size_t max_body_size
Definition Config.hpp:57
std::string ssl_cert
Definition Config.hpp:58
size_t event_loops
Definition Config.hpp:47
std::string log_format
Definition Config.hpp:55
size_t max_header_bytes
Definition Config.hpp:81
ParseMode
How parse() treats a flag it does not know.
Definition Config.hpp:90
@ Strict
Report it and exit(1): the whole command line is Orbit's.
@ Lenient
Report it on stderr and carry on: the application may handle its own flags.
A PEM certificate chain and its private key.
Definition Config.hpp:25
std::string cert_file
Definition Config.hpp:26
std::string key_file
Definition Config.hpp:27