14#ifdef ORBIT_ENABLE_HTTP3
192#define ORBIT_DEFINE_ROUTER_TEMPLATES(METHOD) \
193 template <typename Handler> \
194 App& METHOD(const std::string& path, Handler&& handler) { \
195 return METHOD(path, routing::wrap_handler(std::forward<Handler>(handler))); \
197 template <typename Handler> \
198 App& METHOD(const std::string& path, std::vector<routing::Middleware> mws, Handler&& handler) { \
199 return METHOD(path, std::move(mws), routing::wrap_handler(std::forward<Handler>(handler))); \
208#undef ORBIT_DEFINE_ROUTER_TEMPLATES
218 router_.
ws(path, std::move(mws), std::move(handler));
247 if (!
thread_pool_)
throw std::runtime_error(
"Server not started");
340 const std::string& version =
"1.0.0",
341 const std::string& docs_path =
"/docs",
342 const std::string& json_path =
"/swagger.json",
343 const std::string& assets_url =
"https://unpkg.com/swagger-ui-dist@5.11.0");
402#ifdef ORBIT_ENABLE_HTTP3
403 std::unique_ptr<network::UdpSocket> quic_socket_;
404 std::unique_ptr<QuicConnectionManager> quic_manager_;
#define ORBIT_DEFINE_ROUTER_TEMPLATES(METHOD)
Definition App.hpp:192
Definition ThreadPool.hpp:13
Definition OpenApi.hpp:20
A builder class for fluent route configuration.
Definition Router.hpp:63
Manages routing of HTTP requests to their appropriate handlers.
Definition Router.hpp:46
openapi::OpenApiRegistry & openapi()
The OpenAPI registry for this router tree (shared by its groups).
Definition Router.hpp:58
std::vector< std::string > validate_routes() const
Finds routes registered more than once: the same HTTP method and exact path pattern (text,...
void ws(const std::string &path, WsHandler handler)
void group(const std::string &prefix, std::function< void(Router &)> callback)
Creates a route group with a specific prefix.
RouteBuilder route(const std::string &path, http::HttpMethod method=http::HttpMethod::GET)
Initiates a route builder for a specific path and method.
Definition Router.hpp:95
The main application class for the Orbit Framework.
Definition App.hpp:36
App & get(const std::string &path, routing::RouteHandler handler)
Registers a GET route.
std::mutex loop_mutex_
Definition App.hpp:427
std::vector< std::thread > loop_threads_
Definition App.hpp:413
App & ws(const std::string &path, std::vector< routing::Middleware > mws, routing::WsHandler handler)
Registers a WebSocket route.
Definition App.hpp:217
void shutdown()
Stops the server gracefully (what SIGTERM/SIGINT do): stop accepting, mark the app not ready,...
bool is_draining() const
True once shutdown() (or SIGTERM/SIGINT) has started draining.
Definition App.hpp:388
App & patch(const std::string &path, routing::RouteHandler handler)
Registers a PATCH route.
std::unique_ptr< concurrency::ThreadPool > thread_pool_
Definition App.hpp:417
App & options(const std::string &path, routing::RouteHandler handler)
Registers an OPTIONS route.
App & ws(const std::string &path, routing::WsHandler handler)
Definition App.hpp:222
App & del(const std::string &path, routing::RouteHandler handler)
Registers a DELETE route.
TimerHandle run_every(std::chrono::milliseconds interval, std::function< void()> callback)
Calls callback every interval, on a worker thread, from when the server starts (or now,...
std::vector< size_t > connections_per_event_loop() const
Retrieves the application's thread pool.
App & operator=(const App &)=delete
void listen()
Starts the server and blocks the current thread.
void stop()
Stops the server immediately: open connections are dropped. Safe to call from any thread,...
App(const config::ServerConfig &config)
Constructs a new App instance.
config::ServerConfig config_
Definition App.hpp:397
App & put(const std::string &path, std::vector< routing::Middleware > mws, routing::RouteHandler handler)
Registers a PUT route with middleware.
App & use(const std::string &prefix, routing::Middleware m)
Runs m only for requests under prefix ("/api" and "/api/...").
const config::ServerConfig & effective_config() const
The configuration in effect: once listen() has started, "auto" settings (event_loops = 0,...
Definition App.hpp:243
App & patch(const std::string &path, std::vector< routing::Middleware > mws, routing::RouteHandler handler)
Registers a PATCH route with middleware.
App & post(const std::string &path, std::vector< routing::Middleware > mws, routing::RouteHandler handler)
Registers a POST route with middleware.
App & post(const std::string &path, routing::RouteHandler handler)
Registers a POST route.
App & enable_metrics(const std::string &path="/metrics")
Enables Prometheus metrics endpoint.
App & options(const std::string &path, std::vector< routing::Middleware > mws, routing::RouteHandler handler)
Registers an OPTIONS route with middleware.
App & not_found(routing::RouteHandler handler)
Answers requests that match no route instead of "404 Not Found".
TimerHandle run_after(std::chrono::milliseconds delay, std::function< void()> callback)
void shutdown(std::chrono::seconds timeout)
std::vector< std::unique_ptr< EventLoop > > event_loops_
Definition App.hpp:412
App & enable_health_checks(const std::string &liveness="/healthz", const std::string &readiness="/readyz")
Adds health endpoints for orchestrators such as Kubernetes: liveness answers 200 while the process ru...
App & put(const std::string &path, routing::RouteHandler handler)
Registers a PUT route.
App & group(const std::string &prefix, std::function< void(routing::Router &)> callback)
Creates a route group with a specific prefix.
Definition App.hpp:80
std::vector< std::unique_ptr< Listener > > listeners_
Definition App.hpp:401
bool stop_requested_
Definition App.hpp:428
std::vector< std::string > validate_routes() const
Definition App.hpp:264
void on_error(routing::ErrorHandler handler)
Registers a global error handler for the application.
std::vector< std::function< void(App &)> > stop_hooks_
Definition App.hpp:421
std::unique_ptr< Scheduler > scheduler_
Definition App.hpp:419
std::chrono::steady_clock::time_point next_tls_check_
Definition App.hpp:430
std::atomic< bool > draining_
Definition App.hpp:431
unsigned seen_signal_seq_
Definition App.hpp:429
std::atomic< bool > stop_hooks_ran_
Definition App.hpp:423
App & use(routing::Middleware m)
Registers a global middleware.
std::unique_ptr< network::TlsContext > tls_context_
Definition App.hpp:406
App & get(const std::string &path, std::vector< routing::Middleware > mws, routing::RouteHandler handler)
Registers a GET route with middleware.
std::vector< std::function< void(App &)> > start_hooks_
Definition App.hpp:420
const routing::Router & router() const
The router every request goes through (orbit::testing::Client uses it).
Definition App.hpp:259
App & on_stop(std::function< void(App &)> hook)
Runs hook once when the server stops: at the first shutdown() (or SIGTERM/SIGINT) before connections ...
App & del(const std::string &path, std::vector< routing::Middleware > mws, routing::RouteHandler handler)
Registers a DELETE route with middleware.
std::atomic< bool > started_
Definition App.hpp:422
std::atomic< size_t > open_connections_
Definition App.hpp:411
void hot_reload()
Triggers a hot reload of the server configuration and routes.
routing::Router router_
Definition App.hpp:398
App & enable_openapi(const std::string &title="Orbit Framework API", const std::string &version="1.0.0", const std::string &docs_path="/docs", const std::string &json_path="/swagger.json", const std::string &assets_url="https://unpkg.com/swagger-ui-dist@5.11.0")
Enables OpenAPI documentation and Swagger UI.
openapi::OpenApiRegistry & openapi()
This app's OpenAPI registry: its routes, plus schemas added with openapi().register_schema()....
Definition App.hpp:256
routing::Router::RouteBuilder route(const std::string &path, http::HttpMethod method=http::HttpMethod::GET)
Initiates a route builder for a specific path and method.
Definition App.hpp:91
App & on_start(std::function< void(App &)> hook)
Runs hook once the server is up: listeners bound, worker pool and timers running, just before the eve...
concurrency::ThreadPool & get_thread_pool()
Definition App.hpp:245
bool reload_tls(std::string *error=nullptr)
Re-reads the TLS certificate and key files (ssl_cert, ssl_key and sni_certificates)....
A timer from App::run_every() or App::run_after().
Definition Timer.hpp:15
HttpMethod
Represents standard HTTP methods.
Definition HttpRequest.hpp:28
std::function< void(http::websocket::WebSocketConnection &)> WsHandler
Definition Router.hpp:20
std::function< bool(http::HttpRequest &, std::shared_ptr< http::ResponseWriter >)> Middleware
Definition Router.hpp:19
std::function< void(http::HttpRequest &, std::shared_ptr< http::ResponseWriter >)> RouteHandler
Definition Router.hpp:18
std::function< void(const std::exception &, http::HttpRequest &, std::shared_ptr< http::ResponseWriter >)> ErrorHandler
Definition Router.hpp:21
Definition Http2Session.hpp:18