Go to file
Leonid Stryzhevskyi 3265b99eee
Update README.md
2020-08-04 04:14:10 +03:00
.github Create FUNDING.yml 2020-08-03 23:44:46 +03:00
changelog Changelog. Empty 1.2.0.md 2020-08-04 03:53:05 +03:00
cmake update installation scripts 2019-01-28 18:16:07 +02:00
src Merge branch 'master' into v_1.2.0 2020-08-04 00:40:02 +03:00
test Merge branch 'master' into v_1.2.0 2020-08-04 00:40:02 +03:00
utility better cmake scripts 2019-01-25 16:38:19 +02:00
.gitignore add check for gcc version less than 5 due to missing std::put_time 2020-06-03 12:33:42 +02:00
azure-pipelines.yml CI. Fix Build name. 2020-01-25 10:35:23 +07:00
CMakeLists.txt CMakeLists.txt. Add compat option 'OATPP_COMPAT_BUILD_NO_SET_AFFINITY' 2020-06-05 00:49:07 +03:00
CODE_OF_CONDUCT.md Create CODE_OF_CONDUCT.md 2019-05-09 20:12:15 +03:00
CONTRIBUTING.md Fix typos 2019-05-11 10:56:33 +01:00
lgtm.yml fixing lgtm.yml 2019-01-25 15:08:10 +02:00
LICENSE Initial commit 2018-03-13 03:52:44 +02:00
README.md Update README.md 2020-08-04 04:14:10 +03:00

Oat++ Logo

 

oatpp build status Language grade: C/C++ Join the chat at https://gitter.im/oatpp-framework/Lobby

Oat++

Attention

  • Version 1.2.0 is comming soon. Follow the changelog for details.
  • Version 1.1.0 introduces breaking changes. See the changelog for details.

Oat++ is a modern Web Framework for C++. It's fully loaded and contains all necessary components for effective production level development. It's also light and has a small memory footprint.

Start

About

Join Our Community

High Level Overview

API Controller And Request Mapping

For more info see Api Controller

Declare Endpoint

ENDPOINT("PUT", "/users/{userId}", putUser,
         PATH(Int64, userId),
         BODY_DTO(Object<UserDto>, userDto)) 
{
  userDto->id = userId;
  return createDtoResponse(Status::CODE_200, m_database->updateUser(userDto));
}

Add CORS for Endpoint

For more info see Api Controller / CORS

ADD_CORS(putUser)
ENDPOINT("PUT", "/users/{userId}", putUser,
         PATH(Int64, userId),
         BODY_DTO(Object<UserDto>, userDto)) 
{
  userDto->id = userId;
  return createDtoResponse(Status::CODE_200, m_database->updateUser(userDto));
}

Endpoint with Authorization

For more info see Api Controller / Authorization

using namespace oatpp::web::server::handler;
  
ENDPOINT("PUT", "/users/{userId}", putUser,
         AUTHORIZATION(std::shared_ptr<DefaultBasicAuthorizationObject>, authObject),
         PATH(Int64, userId),
         BODY_DTO(Object<UserDto>, userDto)) 
{
  OATPP_ASSERT_HTTP(authObject->userId == "Ivan" && authObject->password == "admin", Status::CODE_401, "Unauthorized");
  userDto->id = userId;
  return createDtoResponse(Status::CODE_200, m_database->updateUser(userDto));
}

Swagger-UI Annotations

For more info see Endpoint Annotation And API Documentation

Additional Endpoint Info

ENDPOINT_INFO(putUser) {
  // general
  info->summary = "Update User by userId";
  info->addConsumes<Object<UserDto>>("application/json");
  info->addResponse<Object<UserDto>>(Status::CODE_200, "application/json");
  info->addResponse<String>(Status::CODE_404, "text/plain");
  // params specific
  info->pathParams["userId"].description = "User Identifier";
}
ENDPOINT("PUT", "/users/{userId}", putUser,
         PATH(Int64, userId),
         BODY_DTO(Object<UserDto>, userDto)) 
{
  userDto->id = userId;
  return createDtoResponse(Status::CODE_200, m_database->updateUser(userDto));
}

API Client - Retrofit / Feign Like Client

For more info see Api Client

Declare Client

class UserService : public oatpp::web::client::ApiClient {
public:

  API_CLIENT_INIT(UserService)

  API_CALL("GET", "/users", getUsers)
  API_CALL("GET", "/users/{userId}", getUserById, PATH(Int64, userId))

};

Using API Client

auto response = userService->getUserById(id);
auto user = response->readBodyToDto<oatpp::Object<UserDto>>(objectMapper);

Object Mapping

For more info see Data Transfer Object (DTO).

Declare DTO

class UserDto : public oatpp::DTO {

  DTO_INIT(UserDto, DTO)

  DTO_FIELD(Int64, id);
  DTO_FIELD(String, name);

};

Serialize DTO Using ObjectMapper

using namespace oatpp::parser::json::mapping;

auto user = UserDto::createShared();
user->id = 1;
user->name = "Ivan";

auto objectMapper = ObjectMapper::createShared();
auto json = objectMapper->writeToString(user);

Output:

{
  "id": 1,
  "name": "Ivan"
}

Serialize/Deserialize Data In Free Form

While DTO objects apply strict rules on data ser/de, you can also serialize/deserialize data in free form using type oatpp::Any.

oatpp::Fields<oatpp::Any> map = {
  {"title", oatpp::String("Hello Any!")},
  {"listOfAny",
   oatpp::List<oatpp::Any>({
     oatpp::Int32(32),
     oatpp::Float32(0.32),
     oatpp::Boolean(true)
   })
  }
};

auto json = mapper->writeToString(map); 

Output:

{
  "title": "Hello Any!",
  "listOfAny": [
    32,
    0.3199999928474426,
    true
  ]
}

Examples:

REST-API

  • ApiClient-Demo - Example project of how-to use Retrofit-like client wrapper (ApiClient) and how it works.
  • AsyncApi - Example project of how-to use asynchronous API for handling large number of simultaneous connections.
  • CRUD - Example project of how-to create basic CRUD endpoints.

WebSocket

  • Can-Chat - Feature-complete rooms-based chat for tens of thousands users. Client plus Server.
  • WebSocket - Collection of oatpp WebSocket examples.
  • YUV-Websocket-Stream - Example project how-to create a YUV image stream from a V4L device (i.E. Webcam) using websockets.

IoT

  • Example-IoT-Hue - Example project how-to create an Philips Hue compatible REST-API that is discovered and controllable by Hue compatible Smart-Home devices like Amazon Alexa or Google Echo.

Streaming

TLS

  • TLS-Libressl - Example project how-to setup secure connection and serve via HTTPS.

Microservices

  • Consul - Example project of how-to use oatpp::consul::Client. Integration with Consul.
  • Microservices - Example project on how to build microservices with Oat++, and example on how to consolidate those microservices using monolithization technique.

Databases

  • MongoDB - Example project how to work with MongoDB using oatpp-mongo mondule. Project is a web-service with basic CRUD and Swagger-UI.
  • PostgreSQL - Example of a production grade entity service storing information in PostgreSQL. With Swagger-UI and configuration profiles.