|
| 1 | +#include <iostream> |
| 2 | + |
1 | 3 | #include <ManapiHttp.hpp> |
2 | 4 | #include <ManapiInitTools.hpp> |
3 | 5 |
|
4 | | -int main () { |
5 | | - /* creates 2 threads for blocking I/O syscalls */ |
6 | | - manapi::async::context::threadpoolfs(2); |
7 | | - /* disable several signals */ |
8 | | - manapi::async::context::gbs (manapi::async::context::blockedsignals()); |
9 | | - /* creates 4 additional threads for 4 additional event loops */ |
10 | | - auto ctx = manapi::async::context::create(4).unwrap(); |
11 | | - /* HTTP context for multiple HTTP routers (threadsafe) */ |
12 | | - auto router_ctx = manapi::net::http::server_ctx::create().unwrap(); |
13 | | - /* runs main event loop and 4 additional event loops */ |
14 | | - ctx->run(4, [router_ctx] (std::function<void()> bind) -> void { |
15 | | - using http = manapi::net::http::server; |
| 6 | +#define FOLDER "/home/Timur/Downloads/anime-main/" |
| 7 | +int main() { |
| 8 | + manapi::init_tools::log_trace_init(manapi::debug::LOG_TRACE_HARD); |
16 | 9 |
|
17 | | - auto router = manapi::net::http::server::create(router_ctx).unwrap(); |
| 10 | + manapi::async::context::threadpoolfs(4); |
| 11 | + manapi::async::context::gbs(manapi::async::context::blockedsignals()); |
18 | 12 |
|
19 | | - router.GET("/[test]/+custom", [] (http::req &req, http::uresp resp) mutable -> void { |
20 | | - resp->text(std::format("{}, test={}", std::string{req.url()}, req.param("test").unwrap())); |
21 | | - }).unwrap(); |
| 13 | + auto ctx = manapi::async::context::create(0).unwrap(); |
22 | 14 |
|
23 | | - router.GET("/user/[zone]-[id]", [] (http::req &req, http::uresp resp) mutable -> void { |
24 | | - resp->text(std::format("zone={} id={}", req.param("zone").unwrap(), |
25 | | - req.param("id").unwrap())).unwrap(); |
| 15 | + auto server_ctx = manapi::net::http::server_ctx::create().unwrap(); |
| 16 | + ctx->run(0, [server_ctx] (auto cb) -> void { |
| 17 | + using http = manapi::net::http::server; |
| 18 | + auto route = manapi::net::http::server::create(server_ctx).unwrap(); |
| 19 | + |
| 20 | + route.GET ("/", [] (http::req &req, http::uresp resp) -> void { |
| 21 | + resp->file (manapi::filesystem::path::join(FOLDER, "index.html")).unwrap(); |
26 | 22 | }).unwrap(); |
27 | 23 |
|
28 | | - manapi::async::run([router] () mutable -> manapi::future<> { |
29 | | - manapi::unwrap(co_await router.config_object({ |
30 | | - {"pools", manapi::json::array({ |
31 | | - { |
32 | | - {"address", "127.0.0.1"}, |
33 | | - {"http", manapi::json::array({"1.1"})}, |
34 | | - {"port", "8888"} |
35 | | - } |
36 | | - })}, |
37 | | - {"save_config", false} |
38 | | - })); |
39 | | - |
40 | | - manapi::unwrap(co_await router.start()); |
| 24 | + route.GET ("/", FOLDER).unwrap(); |
| 25 | + |
| 26 | + manapi::async::run ([route] () mutable -> manapi::future<> { |
| 27 | + manapi::unwrap(co_await route.config(manapi::filesystem::path::join(".", "config.json"))); |
| 28 | + manapi::unwrap(co_await route.start ()); |
41 | 29 | }); |
42 | 30 |
|
43 | | - /* bind event loop in the current context */ |
44 | | - bind(); |
| 31 | + cb(); |
45 | 32 | }).unwrap(); |
46 | 33 |
|
47 | 34 | manapi::clear_tools::curl_library_clear(); |
|
0 commit comments