Skip to content

Your First Service

Let's build a complete HTTP service step by step. By the end of this guide, you will have a working API server.

Hello World

Create a file called main.cpp:

cpp
#include <framework.hpp>
#include <iostream>


using namespace framework;
using namespace framework::clients::http;

int main() {
    framework::app app;

    app.register_endpoint(
        http_verb_t::get,
        "/hello",
        [](const http_request& req) -> http_response_t {
            auto res = helpers::make_base_http_response(
                req, http_status_t::ok);
            auto body = helpers::make_base_http_payload(
                200, "Hello from Service Framework!");
            helpers::finalize_response(res, body);
            return res;
        }
    );

    std::cout << "Starting on http://localhost:8080" << std::endl;
    app.run_http_service(8080);
    app.run();

    return 0;
}

Compile and run:

bash
mkdir build && cd build
cmake .. && make
./my_service

Test it:

bash
curl http://localhost:8080/hello
# → {"status":200,"message":"Hello from Service Framework!"}

Adding More Routes

cpp

using namespace framework;
using namespace framework::clients::http;

app.register_endpoint(
    http_verb_t::get,
    "/users",
    [](const http_request& req) -> http_response_t {
        auto res = helpers::make_base_http_response(
            req, http_status_t::ok);
        auto body = helpers::make_base_http_payload(200, "ok");
        body["users"] = boost::json::parse(R"([{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}])");
        helpers::finalize_response(res, body);
        return res;
    }
);

What's Next?

Now that you have a working service, you can explore: