Orbit Framework
An ultra-modern, asynchronous, cross-platform C++ Web Framework.
Loading...
Searching...
No Matches
App.hpp
Go to the documentation of this file.
1#pragma once
2
10#ifdef ORBIT_ENABLE_HTTP3
12#else
13namespace server { class QuicConnectionManager; }
14#endif
15#include <memory>
16
17namespace server {
18
26class App {
27public:
34
35 // Delete copy constructors
36 App(const App&) = delete;
37 App& operator=(const App&) = delete;
38
39 // Middleware
45
52
53 // Route Grouping
60 App& group(const std::string& prefix, std::function<void(routing::Router&)> callback) {
61 router_.group(prefix, std::move(callback));
62 return *this;
63 }
64
72 return router_.route(path, method);
73 }
74
75 // Fluent routing API
82 App& get(const std::string& path, routing::RouteHandler handler);
90 App& get(const std::string& path, std::vector<routing::Middleware> mws, routing::RouteHandler handler);
91
98 App& post(const std::string& path, routing::RouteHandler handler);
106 App& post(const std::string& path, std::vector<routing::Middleware> mws, routing::RouteHandler handler);
107
114 App& put(const std::string& path, routing::RouteHandler handler);
122 App& put(const std::string& path, std::vector<routing::Middleware> mws, routing::RouteHandler handler);
123
130 App& patch(const std::string& path, routing::RouteHandler handler);
138 App& patch(const std::string& path, std::vector<routing::Middleware> mws, routing::RouteHandler handler);
139
146 App& del(const std::string& path, routing::RouteHandler handler);
154 App& del(const std::string& path, std::vector<routing::Middleware> mws, routing::RouteHandler handler);
155
162 App& options(const std::string& path, routing::RouteHandler handler);
170 App& options(const std::string& path, std::vector<routing::Middleware> mws, routing::RouteHandler handler);
171
172#define ORBIT_DEFINE_ROUTER_TEMPLATES(METHOD) \
173 template <typename Handler> \
174 App& METHOD(const std::string& path, Handler&& handler) { \
175 return METHOD(path, routing::wrap_handler(std::forward<Handler>(handler))); \
176 } \
177 template <typename Handler> \
178 App& METHOD(const std::string& path, std::vector<routing::Middleware> mws, Handler&& handler) { \
179 return METHOD(path, std::move(mws), routing::wrap_handler(std::forward<Handler>(handler))); \
180 }
181
188#undef ORBIT_DEFINE_ROUTER_TEMPLATES
189
190 // WebSockets
197 App& ws(const std::string& path, routing::WsHandler handler) {
198 router_.ws(path, std::move(handler));
199 return *this;
200 }
201
208 if (!event_loop_) throw std::runtime_error("Server not started");
209 return event_loop_->get_thread_pool();
210 }
211
212 // Metrics
218 App& enable_metrics(const std::string& path = "/metrics");
219
220 // OpenAPI & Swagger UI
229 App& enable_openapi(const std::string& title = "Orbit Framework API",
230 const std::string& version = "1.0.0",
231 const std::string& docs_path = "/docs",
232 const std::string& json_path = "/swagger.json");
233
234 // Start the server (blocking)
238 void listen();
239
240 // Stop the server gracefully
244 void stop();
245
246 // Hot reload the server
251
252private:
253 config::ServerConfig config_;
254 routing::Router router_;
255 std::unique_ptr<Listener> listener_;
256#ifdef ORBIT_ENABLE_HTTP3
257 std::unique_ptr<network::UdpSocket> quic_socket_;
258 std::unique_ptr<QuicConnectionManager> quic_manager_;
259#endif
260 std::unique_ptr<network::TlsContext> tls_context_;
261 std::unique_ptr<EventLoop> event_loop_;
262};
263
264} // namespace server
#define ORBIT_DEFINE_ROUTER_TEMPLATES(METHOD)
Definition App.hpp:172
Definition ThreadPool.hpp:12
A builder class for fluent route configuration.
Definition Router.hpp:43
Manages routing of HTTP requests to their appropriate handlers.
Definition Router.hpp:30
void ws(const std::string &path, WsHandler handler)
Definition Router.cpp:214
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:75
void group(const std::string &prefix, std::function< void(Router &)> callback)
Creates a route group with a specific prefix.
Definition Router.cpp:48
The main application class for the Orbit Framework.
Definition App.hpp:26
App & options(const std::string &path, routing::RouteHandler handler)
Registers an OPTIONS route.
App & operator=(const App &)=delete
App & put(const std::string &path, routing::RouteHandler handler)
Registers a PUT route.
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:71
App & post(const std::string &path, std::vector< routing::Middleware > mws, routing::RouteHandler handler)
Registers a POST route with middleware.
App & ws(const std::string &path, routing::WsHandler handler)
Registers a WebSocket route.
Definition App.hpp:197
concurrency::ThreadPool & get_thread_pool()
Retrieves the application's thread pool.
Definition App.hpp:207
void hot_reload()
Triggers a hot reload of the server configuration and routes.
void stop()
Gracefully stops the server.
App & group(const std::string &prefix, std::function< void(routing::Router &)> callback)
Creates a route group with a specific prefix.
Definition App.hpp:60
App & patch(const std::string &path, routing::RouteHandler handler)
Registers a PATCH route.
App & use(routing::Middleware m)
Registers a global middleware.
App & patch(const std::string &path, std::vector< routing::Middleware > mws, routing::RouteHandler handler)
Registers a PATCH route with middleware.
App & del(const std::string &path, std::vector< routing::Middleware > mws, routing::RouteHandler handler)
Registers a DELETE route with middleware.
App & options(const std::string &path, std::vector< routing::Middleware > mws, routing::RouteHandler handler)
Registers an OPTIONS route with middleware.
App(const App &)=delete
App & put(const std::string &path, std::vector< routing::Middleware > mws, routing::RouteHandler handler)
Registers a PUT route with middleware.
App & del(const std::string &path, routing::RouteHandler handler)
Registers a DELETE route.
void listen()
Starts the server and blocks the current thread.
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")
Enables OpenAPI documentation and Swagger UI.
App & enable_metrics(const std::string &path="/metrics")
Enables Prometheus metrics endpoint.
App(const config::ServerConfig &config)
Constructs a new App instance.
void on_error(routing::ErrorHandler handler)
Registers a global error handler for the application.
App & get(const std::string &path, std::vector< routing::Middleware > mws, routing::RouteHandler handler)
Registers a GET route with middleware.
App & get(const std::string &path, routing::RouteHandler handler)
Registers a GET route.
App & post(const std::string &path, routing::RouteHandler handler)
Registers a POST route.
Definition Config.hpp:5
HttpMethod
Represents standard HTTP methods.
Definition HttpRequest.hpp:17
std::function< bool(http::HttpRequest &, std::shared_ptr< http::ResponseWriter >)> Middleware
Definition Router.hpp:17
std::function< void(const std::exception &, http::HttpRequest &, std::shared_ptr< http::ResponseWriter >)> ErrorHandler
Definition Router.hpp:19
std::function< void(http::HttpRequest &, std::shared_ptr< http::ResponseWriter >)> RouteHandler
Definition Router.hpp:16
std::function< void(http::websocket::WebSocketConnection &)> WsHandler
Definition Router.hpp:18
Definition Http2Session.hpp:17
Definition Config.hpp:18