Oatpp Backend Framework
- When want to build a RESTful API Backend Web App, nowdays there are a hot framework in
Github
that named Oatpp. and the offical website
Setup the environment
install Oatpp
environment
Optional 1: complied oatpp
shell
git clone https://github.com/oatpp/oatpp.git
cd oatpp/
mkdir build && cd build
cmake .. -DCMAKE_TOOLCHAIN_FILE=../vcpkg/scripts/buildsystems/vcpkg.cmake
sudo make && sudo make install
Optional 2: install oatapp
by VCPG, which is a package management way supported by Microsoft
shell
git clone https://github.com/microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg install oatpp
Build the Backend App
shell
mkdir my_oatapp_api
build the dir as the following
css
my_oatpp_api/
├── CMakeLists.txt
├── src/
│ ├── AppComponent.hpp
│ ├── Controller.hpp
│ ├── main.cpp
│ └── dto/
│ ├── MessageDto.hpp
Resource
- App.cpp
c++
#include "AppComponent.hpp"
#include "Controller.hpp"
#include "oatpp/core/macro/component.hpp"
#include "oatpp/network/Server.hpp"
int main() {
oatpp::base::Environment::init();
AppComponent components;
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, connectionProvider);
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ConnectionHandler>, connectionHandler);
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);
OATPP_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, objectMapper);
auto helloController = std::make_shared<HelloController>(objectMapper);
router->addController(helloController);
oatpp::network::Server server(connectionProvider, connectionHandler);
OATPP_LOGI("Oatpp", "Server running on port 8000...");
server.run();
oatpp::base::Environment::destroy();
return 0;
}
- AppComponent.hpp
cpp
#pragma once
#include "oatpp/network/tcp/server/ConnectionProvider.hpp"
#include "oatpp/web/server/HttpConnectionHandler.hpp"
#include "oatpp/web/server/HttpRouter.hpp"
#include "oatpp/core/macro/component.hpp"
#include "oatpp/core/data/mapping/ObjectMapper.hpp"
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
class AppComponent {
public:
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, serverConnectionProvider)([] {
oatpp::network::Address address("0.0.0.0", 8000, oatpp::network::Address::IP_4);
return oatpp::network::tcp::server::ConnectionProvider::createShared(address);
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, httpRouter)([] {
return oatpp::web::server::HttpRouter::createShared();
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ConnectionHandler>, serverConnectionHandler)([] {
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);
return oatpp::web::server::HttpConnectionHandler::createShared(router);
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, apiObjectMapper)([] {
return oatpp::parser::json::mapping::ObjectMapper::createShared();
}());
};
Controller.hpp
cpp
#pragma once
#include "oatpp/web/server/api/ApiController.hpp"
#include "oatpp/core/macro/codegen.hpp"
#include "oatpp/core/macro/codegen.hpp"
#include "dto/MessageDto.hpp"
#include OATPP_CODEGEN_BEGIN(ApiController)
class HelloController : public oatpp::web::server::api::ApiController {
public:
HelloController(const std::shared_ptr<oatpp::data::mapping::ObjectMapper>& objectMapper)
: oatpp::web::server::api::ApiController(objectMapper) {}
ENDPOINT("GET", "/hello", hello) {
return createResponse(Status::CODE_200, "Hello, Oat++!");
}
ENDPOINT("GET", "/echo/{msg}", echo, PATH(String, msg)) {
return createResponse(Status::CODE_200, "You said: " + msg);
}
ENDPOINT("GET", "/json", getJson) {
auto dto = MessageDto::createShared();
dto->message = "Hello, Oat++ JSON!";
return createDtoResponse(Status::CODE_200, dto);
}
};
#include OATPP_CODEGEN_END(ApiController)
- MessageDto.hpp
cpp
#include "oatpp/core/macro/codegen.hpp"
#include "oatpp/core/macro/codegen.hpp"
#include OATPP_CODEGEN_BEGIN(DTO)
class MessageDto : public oatpp::DTO {
DTO_INIT(MessageDto, DTO)
DTO_FIELD(String, message);
};
#include OATPP_CODEGEN_END(DTO)
CMAKE
- CMakeLists.txt
cmake
cmake_minimum_required(VERSION 3.15)
project(my_oatpp_api VERSION 1.0 LANGUAGES CXX)
# using C++ 17
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# setup vcpkg toolchain
# set(CMAKE_TOOLCHAIN_FILE "../vcpkg/scripts/buildsystems/vcpkg.cmake")
# find oatpp package (vcpkg will supports the config file)
find_package(oatpp 1.3.0 REQUIRED)
file(GLOB DTO_HEADERS "src/dto/*.hpp")
# points the source file
set(SRC
src/App.cpp
src/AppComponent.hpp
src/Controller.hpp
${DTO_HEADERS}
)
# create the bin file
add_executable(${PROJECT_NAME} ${SRC})
# link to the `oatpp` package
target_link_libraries(${PROJECT_NAME} PRIVATE oatpp::oatpp oatpp::oatpp-test)
# Generate Oatpp API controller
# if using codegen endpoint define,you can open the next lines
# oatpp_generate_api_controller_api(${PROJECT_NAME})
Build the Project
- Build
shell
mkdir build && cd build
cmake ..
make
- Run the bin file
shell
./my_oatpp_api
- Terminal
shell
(myenv) dipeng-xu@Unknown-Mac build % ./my_oatpp_api
I |2025-10-08 21:27:07 1759930027513565| Oatpp:Server running on port 8000...