Manapi Http

Quick Start

Getting Started with Coding

Preparation

After installation, include the library in your CMakeLists.txt. Import the ManapiHttp library into your project.

Using System Libraries (Default)

find_package(manapihttp REQUIRED)
 
target_link_libraries(${PROJECT_NAME} manapihttp)

Using Conan

find_package(manapihttp REQUIRED)
 
target_link_libraries(${PROJECT_NAME} manapihttp::manapihttp)

Build

Once all preparations are complete, let's write a "Hello, World!" program.

#include <iostream>
#include <format>
 
#include <manapihttp/ManapiInitTools.hpp>
#include <manapihttp/ManapiHttp.hpp>
#include <manapihttp/fs/ManapiFilesystem.hpp>
 
int main(int argc, char *argv[]) {
    /* Enable low-level log tracing */
    manapi::init_tools::log_trace_init(manapi::debug::LOG_TRACE_LOW);
    /* Create 2 threads for blocking I/O system calls */
    manapi::async::context::threadpoolfs(2);
    /* Disable several signals */
    manapi::async::context::gbs(manapi::async::context::blockedsignals());
    /* Create main context */
    auto ctx = manapi::async::context::create().unwrap();
    /* HTTP context for multiple HTTP routers (thread-safe) */
    auto router_ctx = manapi::net::http::server_ctx::create().unwrap();
 
    ctx->run([router_ctx](std::function<void()> bind) -> void {
        using http = manapi::net::http::server;
        auto router = manapi::net::http::server::create(router_ctx).unwrap();
 
        router.GET("/", [](http::req &req, http::uresp resp) -> void {
            resp->text("Hello, World!").unwrap();
        }).unwrap();
 
        router.GET("/stop", [](http::req &req, http::resp &resp) -> manapi::future<> {
            auto res = co_await manapi::async::current()->stop();
            resp->text(std::format("status: {}", res.msg())).unwrap();
        }).unwrap();
 
        manapi::async::run([router]() mutable -> manapi::future<> {
            auto res = co_await router->config_object({
                {"pools", {
                    {"address", "127.0.0.1"},
                    {"port", "8888"},
                    {"transport", "tcp"},
                    {"http", manapi::json::array({"1.1"})}
                }}
            });
            res.unwrap();
 
            res = co_await router->start();
            res.unwrap();
        });
        
        bind();
    });
 
    return 0;
}

On this page