|
| 1 | +--- |
| 2 | + |
| 3 | +title: Routers |
| 4 | +description: Creating the First Pages |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +## Server Context |
| 9 | + |
| 10 | +In Manapi Http, you can use HTTP with multithreading. |
| 11 | +Because of this, you need to use `manapi::net::http::server_ctx`. |
| 12 | + |
| 13 | +To create it, use `manapi::net::http::server_ctx::create` before starting the context. |
| 14 | + |
| 15 | +```c++ |
| 16 | +/* HTTP context for multiple HTTP routers (thread-safe) */ |
| 17 | +auto router_ctx = manapi::net::http::server_ctx::create().unwrap(); |
| 18 | +``` |
| 19 | + |
| 20 | +You can then use the context in HTTP routers with `manapi::net::http::server::create`. |
| 21 | + |
| 22 | +```c++ |
| 23 | +#include <manapihttp/ManapiHttp.hpp> |
| 24 | +#include <manapihttp/ManapiInitTools.hpp> |
| 25 | + |
| 26 | +int main() { |
| 27 | + /* creates 2 threads for blocking I/O syscalls */ |
| 28 | + manapi::async::context::threadpoolfs(2); |
| 29 | + /* disable several signals */ |
| 30 | + manapi::async::context::gbs(manapi::async::context::blockedsignals()); |
| 31 | + /* creates 4 additional threads for 4 additional event loops */ |
| 32 | + auto ctx = manapi::async::context::create(4).unwrap(); |
| 33 | + /* HTTP context for multiple HTTP routers (thread-safe) */ |
| 34 | + auto router_ctx = manapi::net::http::server_ctx::create().unwrap(); |
| 35 | + /* runs main event loop and 4 additional event loops */ |
| 36 | + ctx->run(4, [router_ctx](std::function<void()> bind) -> void { |
| 37 | + using http = manapi::net::http::server; |
| 38 | + |
| 39 | + auto router = manapi::net::http::server::create(router_ctx).unwrap(); |
| 40 | + |
| 41 | + router.GET("/", [](http::req &req, http::uresp resp) mutable -> void { |
| 42 | + resp->text("Hello World!").unwrap(); |
| 43 | + resp.finish(); // optional to define here |
| 44 | + }).unwrap(); |
| 45 | + |
| 46 | + manapi::async::run([router]() mutable -> manapi::future<> { |
| 47 | + manapi::unwrap(co_await router.config_object({ |
| 48 | + {"pools", manapi::json::array({ |
| 49 | + { |
| 50 | + {"address", "127.0.0.1"}, |
| 51 | + {"http", manapi::json::array({"1.1"})}, |
| 52 | + {"port", "8888"} |
| 53 | + } |
| 54 | + })}, |
| 55 | + {"save_config", false} |
| 56 | + })); |
| 57 | + |
| 58 | + manapi::unwrap(co_await router.start()); |
| 59 | + }); |
| 60 | + |
| 61 | + /* bind event loop in the current context */ |
| 62 | + bind(); |
| 63 | + }).unwrap(); |
| 64 | + |
| 65 | + manapi::clear_tools::curl_library_clear(); |
| 66 | + manapi::clear_tools::ev_library_clear(); |
| 67 | + manapi::clear_tools::ssl_library_clear(); |
| 68 | + |
| 69 | + return 0; |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +`manapi::net::http::server` supports many methods: |
| 74 | + |
| 75 | +```c++ |
| 76 | +router.GET("/", ...).unwrap(); |
| 77 | +router.POST("/", ...).unwrap(); |
| 78 | +router.HEAD("/", ...).unwrap(); |
| 79 | +router.OPTIONS("/", ...).unwrap(); |
| 80 | +router.PUT("/", ...).unwrap(); |
| 81 | +router.PATCH("/", ...).unwrap(); |
| 82 | +``` |
| 83 | + |
| 84 | +<Callout title="Note">You can also use custom methods with `manapi::net::http::server::handler`.</Callout> |
| 85 | + |
| 86 | +### Sharing Pages |
| 87 | + |
| 88 | +You can share files using the `manapi::net::http::server::GET` method. |
| 89 | + |
| 90 | +```c++ |
| 91 | +router.GET("/", "/path/to/folder", [](http::req &req, http::uresp resp) |
| 92 | + -> void { }).unwrap(); |
| 93 | +``` |
| 94 | +
|
| 95 | +### Layer Pages |
| 96 | +
|
| 97 | +You can create layers using the `+layer` keyword in the URL. |
| 98 | +
|
| 99 | +```c++ |
| 100 | +router.GET("/admin/+layer", [](http::req &req, http::uresp resp) mutable -> void { |
| 101 | + if (req.ip_data().ip != "127.0.0.1") { |
| 102 | + req.stop_propagation(); |
| 103 | + resp->text("error").unwrap(); |
| 104 | + } |
| 105 | +}).unwrap(); |
| 106 | +
|
| 107 | +router.GET("/admin/next/+layer", [](http::req &req, http::uresp resp) mutable -> void { |
| 108 | + if (req.ip_data().port != "5555") { |
| 109 | + req.stop_propagation(); |
| 110 | + resp->text("error").unwrap(); |
| 111 | + } else { |
| 112 | + // If this code is reached, |
| 113 | + // the user has IP 127.0.0.1 and port 5555. |
| 114 | + } |
| 115 | +}).unwrap(); |
| 116 | +``` |
| 117 | + |
| 118 | +<Callout title="Note">Use `req.stop_propagation()` to prevent further execution of subsequent layers.</Callout> |
| 119 | + |
| 120 | +### Error Pages |
| 121 | + |
| 122 | +Display error pages for server errors or when a page does not exist. |
| 123 | + |
| 124 | +```c++ |
| 125 | +router.GET("/+error", [](http::req &req, http::uresp resp) mutable -> void { |
| 126 | + resp->text("something went wrong").unwrap(); |
| 127 | +}).unwrap(); |
| 128 | +``` |
| 129 | + |
| 130 | +### Custom Page |
| 131 | + |
| 132 | +Show a page for URLs that start with a specified string. |
| 133 | + |
| 134 | +```c++ |
| 135 | +router.GET("/[test]/+custom", [](http::req &req, http::uresp resp) mutable -> void { |
| 136 | + resp->text(std::format("{}, test={}", |
| 137 | + std::string{req.url()}, req.param("test").unwrap())).unwrap(); |
| 138 | +}).unwrap(); |
| 139 | +``` |
| 140 | + |
| 141 | +### URL Parameters |
| 142 | + |
| 143 | +Use `req.param()` to retrieve data from the URL. |
| 144 | + |
| 145 | +```c++ |
| 146 | +resp->text(std::format("zone={} id={}", req.param("zone").unwrap(), |
| 147 | + req.param("id").unwrap())).unwrap(); |
| 148 | +``` |
0 commit comments