include this server header
#include "api/server.hpp"create a server instance
int main() {
api::Server server;
}to add a GET route
server.get("/customers", [](api::Request) -> api::Response {
// do stuff and return a response
});to build a response
api::Response response(200);
response.set_body(R"({"customers": ["jerry", "peter", "dennis"]})");a complete GET route looks like this
server.get("/customers", [](api::Request) -> api::Response {
api::Response response(200);
response.set_body(R"({"customers": ["jerry", "peter", "dennis"]})");
return response;
});or you can return a response via .json files with load_response
return api::load_response("./database/patrons.json"); to return an error
return api::Response::error(404);to add a global middleware
server.use([](api::Request&) -> std::optional<api::Response> {
// return an api::Response to short-circuit
// return { } to move to move to the next middleware/route
});use the start(port) method
server.start(8080);the repo has an examples/ folder you can check. compile and run the routing example from the project root with:
g++ examples/routing_example.cpp -o routing_example && ./routing_examplethis is a header-only library, so no compilation needed. just drop the headers into your project.
- download one of the include.zip releases.
- copy the
api/directory into your project's include folder. - include the server header in your code:
#include "api/server.hpp"
- add this repository to your project.
- add these lines to your CMakeLists.txt:
# add the framework directory
add_subdirectory(third_party/RestfulApiFramework)
# link it to your executable
target_link_libraries(your_project_name PRIVATE RestAPI::framework)- Compiler: C++17 compatible compiler (GCC 7+, Clang 5+, or MSVC 19.14+).
- Operating System: Linux / macOS (standard POSIX socket support).
- Build System: CMake 3.10+ (optional, but recommended).