oatpp/README.md

167 lines
6.0 KiB
Markdown
Raw Normal View History

2019-10-07 07:57:22 +08:00
# Oat++ [![oatpp build status](https://dev.azure.com/lganzzzo/lganzzzo/_apis/build/status/oatpp.oatpp)](https://dev.azure.com/lganzzzo/lganzzzo/_build?definitionId=1) [![Language grade: C/C++](https://img.shields.io/lgtm/grade/cpp/g/oatpp/oatpp.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/oatpp/oatpp/context:cpp) [![Join the chat at https://gitter.im/oatpp-framework/Lobby](https://badges.gitter.im/oatpp-framework/Lobby.svg)](https://gitter.im/oatpp-framework/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
2018-03-13 12:04:53 +08:00
2019-10-28 10:09:56 +08:00
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.
2018-10-18 07:54:45 +08:00
2019-05-10 02:42:34 +08:00
- [Website](https://oatpp.io/)
- [Docs](https://oatpp.io/docs/start/)
- [Api Reference](https://oatpp.io/api/latest/)
2019-10-28 10:09:56 +08:00
- [Supported Platforms](https://oatpp.io/supported-platforms/)
2019-05-20 10:16:00 +08:00
- Latest Benchmarks: [5 Million WebSockets](https://oatpp.io/benchmark/websocket/5-million/)
2018-07-09 05:11:10 +08:00
2018-10-15 19:26:33 +08:00
**Contributors wanted!**
2019-05-10 02:42:34 +08:00
- See [Contributing to Oat++](CONTRIBUTING.md)
**Join the community**
- Join discussion on **Gitter**. [oat++ framework/Lobby](https://gitter.im/oatpp-framework/Lobby)
- Follow us on **Twitter** for latest news. [@oatpp_io](https://twitter.com/oatpp_io)
2019-05-11 19:11:08 +08:00
- Join community on **Reddit**. [r/oatpp](https://www.reddit.com/r/oatpp/)
2018-10-15 19:26:33 +08:00
2019-10-28 10:09:56 +08:00
## API - High Level Overview
2018-03-13 12:04:53 +08:00
2019-10-28 10:09:56 +08:00
### Object Mapping
2018-03-13 12:04:53 +08:00
2019-10-28 10:09:56 +08:00
For more info see [Data Transfer Object (DTO)](https://oatpp.io/docs/components/dto/).
2018-10-14 17:49:59 +08:00
2019-10-28 10:09:56 +08:00
#### Declare DTO
2018-10-14 17:49:59 +08:00
2019-10-28 10:09:56 +08:00
```cpp
class UserDto : public oatpp::data::mapping::type::Object {
2018-10-14 17:49:59 +08:00
2019-10-28 10:09:56 +08:00
DTO_INIT(UserDto, Object)
2018-10-14 17:49:59 +08:00
2019-10-28 10:09:56 +08:00
DTO_FIELD(Int64, id);
DTO_FIELD(String, name);
2018-10-14 17:49:59 +08:00
2019-10-28 10:09:56 +08:00
};
2018-10-14 17:49:59 +08:00
```
2019-10-28 10:09:56 +08:00
#### Serialize DTO Using ObjectMapper
2018-10-14 17:49:59 +08:00
2019-10-28 10:09:56 +08:00
```cpp
using namespace oatpp::parser::json::mapping;
2018-10-14 17:49:59 +08:00
2019-10-28 10:09:56 +08:00
auto user = UserDto::createShared();
user->id = 1;
user->name = "Ivan";
2018-10-14 17:49:59 +08:00
2019-10-28 10:09:56 +08:00
auto objectMapper = ObjectMapper::createShared();
auto json = objectMapper->writeToString(user);
2018-10-14 17:49:59 +08:00
```
2019-10-28 10:09:56 +08:00
### API Controller - Request Mapping
2018-10-14 17:49:59 +08:00
2019-10-28 10:09:56 +08:00
For more info see [Api Controller](https://oatpp.io/docs/components/api-controller/)
2018-10-14 17:49:59 +08:00
2019-10-28 10:09:56 +08:00
#### Declare Endpoint
2018-10-14 17:49:59 +08:00
2019-10-28 10:09:56 +08:00
```cpp
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));
}
2018-10-14 17:49:59 +08:00
```
2019-10-28 10:09:56 +08:00
#### Add CORS for Endpoint
2018-10-14 17:49:59 +08:00
2019-10-28 10:09:56 +08:00
For more info see [Api Controller / CORS](https://oatpp.io/docs/components/api-controller/#cors)
2018-10-14 17:49:59 +08:00
2019-10-28 10:09:56 +08:00
```cpp
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));
}
2018-10-14 17:49:59 +08:00
```
2019-10-28 10:09:56 +08:00
#### Endpoint with Authorization
2018-10-14 17:49:59 +08:00
2019-10-28 10:09:56 +08:00
For more info see [Api Controller / Authorization](https://oatpp.io/docs/components/api-controller/#authorization-basic)
2018-10-14 17:49:59 +08:00
2019-10-28 10:09:56 +08:00
```cpp
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));
}
2018-10-14 17:49:59 +08:00
```
2019-10-28 10:09:56 +08:00
### API Client - Retrofit / Feign Like Client
2018-10-14 17:49:59 +08:00
2019-10-28 10:09:56 +08:00
For more info see [Api Client](https://oatpp.io/docs/components/api-client/)
2018-10-14 17:49:59 +08:00
2019-10-28 10:09:56 +08:00
#### Declare Client
2018-10-14 17:49:59 +08:00
2019-10-28 10:09:56 +08:00
```cpp
class UserService : public oatpp::web::client::ApiClient {
public:
2018-10-14 17:49:59 +08:00
2019-10-28 10:09:56 +08:00
API_CLIENT_INIT(UserService)
API_CALL("GET", "/users", getUsers)
API_CALL("GET", "/users/{userId}", getUserById, PATH(Int64, userId))
2018-10-14 17:49:59 +08:00
};
```
2019-10-28 10:09:56 +08:00
#### Using API Client
```cpp
auto response = userService->getUserById(id);
auto user = response->readBodyToDto<dto::UserDto>(objectMapper);
2018-10-14 17:49:59 +08:00
```
2019-10-28 10:09:56 +08:00
### Swagger-UI Annotations
For more info see [Endpoint Annotation And API Documentation](https://oatpp.io/docs/components/api-controller/#endpoint-annotation-and-api-documentation)
#### Additional Endpoint Info
2018-10-15 16:49:04 +08:00
2019-10-28 10:09:56 +08:00
```cpp
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";
2018-10-15 16:49:04 +08:00
}
2019-10-28 10:09:56 +08:00
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));
2018-10-15 16:49:04 +08:00
}
```
2019-10-28 10:09:56 +08:00
### Read Next
2018-03-13 12:04:53 +08:00
2019-10-28 10:09:56 +08:00
- [Well Structured Project](https://oatpp.io/docs/start/step-by-step/#well-structured-project)
- [Build For Unix/Linux](https://oatpp.io/docs/installation/unix-linux/)
- [Build For Windows](https://oatpp.io/docs/installation/windows/)
2018-03-15 12:22:27 +08:00
### Examples:
2019-01-29 11:00:13 +08:00
- [Media-Stream (Http-Live-Streaming)](https://github.com/oatpp/example-hls-media-stream) - Example project of how-to build HLS-streaming server using oat++ Async-API.
- [CRUD](https://github.com/oatpp/example-crud) - Example project of how-to create basic CRUD endpoints.
2019-01-30 09:40:40 +08:00
- [AsyncApi](https://github.com/oatpp/example-async-api) - Example project of how-to use asynchronous API for handling large number of simultaneous connections.
2019-01-30 06:38:28 +08:00
- [ApiClient-Demo](https://github.com/oatpp/example-api-client) - Example project of how-to use Retrofit-like client wrapper (ApiClient) and how it works.
2019-01-30 08:44:02 +08:00
- [TLS-Libressl](https://github.com/oatpp/example-libressl) - Example project of how-to setup secure connection and serve via HTTPS.
2019-01-30 08:43:02 +08:00
- [Consul](https://github.com/oatpp/example-consul) - Example project of how-to use oatpp::consul::Client. Integration with Consul.
2019-02-27 01:00:05 +08:00
- [PostgreSQL](https://github.com/oatpp/example-postgresql) - Example of a production grade entity service storing information in PostgreSQL. With Swagger-UI and configuration profiles.
2019-06-25 08:52:17 +08:00
- [WebSocket](https://github.com/oatpp/example-websocket) - Collection of oatpp WebSocket examples.