oatpp/README.md

181 lines
6.1 KiB
Markdown
Raw Normal View History

2018-10-22 21:26:33 +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
2018-10-18 07:54:45 +08:00
Zero-Dependency. Performance oriented web-service-development framework.
Organic. Pure C++.
2018-07-09 05:11:10 +08:00
- Website: [https://oatpp.io](https://oatpp.io)
- Docs: [https://oatpp.io/docs/start](https://oatpp.io/docs/start)
2018-10-14 18:06:07 +08:00
- Benchmarks: [https://oatpp.io/benchmark/digital-ocean](https://oatpp.io/benchmark/digital-ocean)
2018-07-09 05:11:10 +08:00
2018-10-15 19:26:33 +08:00
**Contributors wanted!**
Pull requests and issues are welcome.
2018-03-13 12:04:53 +08:00
## Features
- Blazingly fast
- Zero Dependency, no installations
2018-03-29 13:24:13 +08:00
- **Asynchronous server (High performance. Handle 30K+ simultaneous connections)**
- Multithreaded server (Simple API)
2018-03-13 22:57:31 +08:00
- Connection agnostic. (Use whatever transport. Whatever SSL backend. Whatever sockets, pipes, files. etc. It cares about HTTP stream only)
2018-10-15 16:49:04 +08:00
- REST framework (with ability to autodocument endpoints see [oatpp-swagger](https://github.com/oatpp/oatpp-swagger))
2018-03-13 12:04:53 +08:00
- Retrofit-like client wrapper (Use whatever request executor for example cURL, or minimalistic one provided out of the box)
- Object mapping (Fast object serialization-deserialization. Currently JSON, more formats comes shortly)
- Simple dependency injection framework
2018-03-13 22:57:31 +08:00
- Simple Test framework
2018-03-13 12:04:53 +08:00
- HTTP_1.1 (2.0 comes shortly)
2018-10-14 17:49:59 +08:00
## Simple API overview
"Simple API" refers to as API used together with ```oatpp::web::server::HttpConnectionHandler``` utilizing multithreading plus blocking-IO approach.
### Create Endpoint
```c++
ENDPOINT("GET", "demo/api/hello", hello) {
return createResponse(Status::CODE_200, "Hello World!");
}
```
### Pass parameters to endpoint
```c++
ENDPOINT("GET", "demo/api/param/{param}", getWithParams,
PATH(String, param)) {
return createResponse(Status::CODE_200, "param=" + param);
}
```
### Return JSON
```c++
ENDPOINT("GET", "demo/api/json", getJson) {
auto dto = MyDto::createShared();
dto->statusCode = 200;
dto->message = "Hello json";
return createDtoResponse(Status::CODE_200, dto);
}
```
**Output:**
```
{"message": "Hello json", "statusCode": 200}
```
### Post JSON body
```c++
ENDPOINT("POST", "demo/api/json", postJson,
BODY_DTO(MyDto::ObjectWrapper, dto)) {
auto dtoMessage = dto->message;
return createResponse(Status::CODE_200, "dtoMessage: " + dtoMessage);
}
```
**Terminal:**
```
$ curl -X POST "localhost:8001/demo/api/json" -d '{"message": "hello json post"}'
dtoMessage: hello json post
```
## Async API overview
"Async API" refers to as API used together with ```oatpp::web::server::AsyncHttpConnectionHandler``` utilizing oatpp-coroutines plus non-blocking-IO approach.
### Create Endpoint Async
```c++
ENDPOINT_ASYNC("GET", "demo/api_async/hello", HelloAsync) {
ENDPOINT_ASYNC_INIT(HelloAsync)
Action act() override {
return _return(controller->createResponse(Status::CODE_200, "Hello World Async API!"));
}
};
```
### Pass parameters to endpoint Async
```c++
ENDPOINT_ASYNC("GET", "demo/api_async/param/{param}", GetWithParamsAsync) {
ENDPOINT_ASYNC_INIT(GetWithParamsAsync)
Action act() override {
auto param = request->getPathVariable("param");
return _return(controller->createResponse(Status::CODE_200, "param=" + param));
}
};
```
### Return JSON Async
```c++
ENDPOINT_ASYNC("GET", "demo/api_async/json", GetJSONAsync) {
ENDPOINT_ASYNC_INIT(GetJSONAsync)
Action act() override {
auto dto = MyDto::createShared();
dto->statusCode = 200;
dto->message = "Hello json";
return _return(controller->createDtoResponse(Status::CODE_200, dto));
}
};
```
**Output:**
```
{"message": "Hello json", "statusCode": 200}
```
### Post JSON body Async
```c++
ENDPOINT_ASYNC("POST", "demo/api_async/json", PostJSONAsync) {
ENDPOINT_ASYNC_INIT(PostJSONAsync)
Action act() override {
return request->readBodyToDtoAsync<MyDto>(this,
&PostJSONAsync::onBodyObtained,
controller->getDefaultObjectMapper());
}
Action onBodyObtained(const MyDto::ObjectWrapper& dto) {
return _return(controller->createResponse(Status::CODE_200, "dtoMessage: " + dto->message));
}
};
```
**Terminal:**
```
$ curl -X POST "localhost:8001/demo/api_async/json" -d '{"message": "hello json post"}'
dtoMessage: hello json post
```
2018-10-15 16:49:04 +08:00
### Swagger documentation
```c++
ENDPOINT_INFO(createUser) {
info->summary = "Create new User";
info->addConsumes<UserDto::ObjectWrapper>("application/json");
info->addResponse<UserDto::ObjectWrapper>(Status::CODE_200, "application/json");
}
ENDPOINT("POST", "demo/api/users", createUser,
BODY_DTO(UserDto::ObjectWrapper, userDto)) {
return createDtoResponse(Status::CODE_200, m_database->createUser(userDto));
}
```
2018-03-13 12:04:53 +08:00
## How to start
2018-03-14 04:56:11 +08:00
Grab any project from [examples](https://github.com/oatpp/oatpp-examples), and follow README
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.
2018-07-10 06:42:31 +08:00
- [AsyncApi](https://github.com/oatpp/oatpp-examples/tree/master/AsyncApi) - Example project of how-to use asynchronous API for handling large number of simultaneous connections.
- [ApiClient-Demo](https://github.com/oatpp/oatpp-examples/tree/master/ApiClient-Demo) - Example project of how-to use Retrofit-like client wrapper (ApiClient) and how it works.
- [TLS-Libressl](https://github.com/oatpp/oatpp-examples/tree/master/tls-libressl) - Example project of how-to setup secure connection and serve via HTTPS.
- [Consul](https://github.com/oatpp/oatpp-examples/tree/master/consul) - Example project of how-to use oatpp::consul::Client. Integration with Consul.