Giter Site home page Giter Site logo

origin928 / oatpp Goto Github PK

View Code? Open in Web Editor NEW

This project forked from oatpp/oatpp

0.0 0.0 0.0 1.86 MB

🌱Modern Web Framework for C++. High performance, simple API, cross platform, zero dependency.

Home Page: https://oatpp.io/

License: Apache License 2.0

C++ 98.37% CMake 1.59% Shell 0.04%

oatpp's Introduction

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

Oat++ is the 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 small memory footprint.

Contributors wanted!

Join the 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(dto::UserDto::ObjectWrapper, 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(dto::UserDto::ObjectWrapper, 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(dto::UserDto::ObjectWrapper, 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<dto::UserDto::ObjectWrapper>("application/json");
  info->addResponse<dto::UserDto::ObjectWrapper>(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(dto::UserDto::ObjectWrapper, 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<dto::UserDto>(objectMapper);

Object Mapping

For more info see Data Transfer Object (DTO).

Declare DTO

class UserDto : public oatpp::data::mapping::type::Object {

  DTO_INIT(UserDto, Object)

  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);

Read Next

Examples:

  • Media-Stream (Http-Live-Streaming) - Example project of how-to build HLS-streaming server using oat++ Async-API.
  • CRUD - Example project of how-to create basic CRUD endpoints.
  • AsyncApi - Example project of how-to use asynchronous API for handling large number of simultaneous connections.
  • ApiClient-Demo - Example project of how-to use Retrofit-like client wrapper (ApiClient) and how it works.
  • TLS-Libressl - Example project of how-to setup secure connection and serve via HTTPS.
  • Consul - Example project of how-to use oatpp::consul::Client. Integration with Consul.
  • PostgreSQL - Example of a production grade entity service storing information in PostgreSQL. With Swagger-UI and configuration profiles.
  • WebSocket - Collection of oatpp WebSocket examples.

oatpp's People

Contributors

lganzzzo avatar bamkrs avatar dmcamens-legrand avatar apple-ihack-geek avatar florianrhiem avatar sauntor avatar healeycodes avatar rwtolbert avatar david-novak-legrand avatar mensong avatar mheyman avatar azure-pipelines[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.