mirror of
https://gitee.com/zyjblog/oatpp.git
synced 2025-01-05 17:42:23 +08:00
Update README.md
This commit is contained in:
parent
73f872b92b
commit
52d1e3bb66
128
README.md
128
README.md
@ -21,6 +21,134 @@ Organic. Pure C++.
|
|||||||
- Simple Test framework
|
- Simple Test framework
|
||||||
- HTTP_1.1 (2.0 comes shortly)
|
- HTTP_1.1 (2.0 comes shortly)
|
||||||
|
|
||||||
|
## 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
|
||||||
|
```
|
||||||
|
|
||||||
## How to start
|
## How to start
|
||||||
|
|
||||||
Grab any project from [examples](https://github.com/oatpp/oatpp-examples), and follow README
|
Grab any project from [examples](https://github.com/oatpp/oatpp-examples), and follow README
|
||||||
|
Loading…
Reference in New Issue
Block a user