|
1 |
| -# example-libressl [](https://dev.azure.com/lganzzzo/lganzzzo/_build?definitionId=13&branchName=master) |
| 1 | +# TLS-LibreSSL Example [](https://dev.azure.com/lganzzzo/lganzzzo/_build?definitionId=13&branchName=master) |
| 2 | + |
| 3 | +Example project of how-to use [oatpp-libressl](https://github.com/oatpp/oatpp-libressl) module. |
| 4 | +- Serve via HTTPS |
| 5 | +- Make client calls via HTTPS. |
| 6 | +- Using oatpp Async API. |
| 7 | + |
| 8 | +#### More about oat++: |
| 9 | +- Website: [https://oatpp.io](https://oatpp.io) |
| 10 | +- Docs: [https://oatpp.io/docs/start](https://oatpp.io/docs/start) |
| 11 | +- Oat++ Repo: [https://github.com/oatpp/oatpp](https://github.com/oatpp/oatpp) |
| 12 | + |
| 13 | +## Overview |
| 14 | +This project is using `oatpp` and `oatpp-libressl` modules. |
| 15 | + |
| 16 | +### Project layout |
| 17 | + |
| 18 | +``` |
| 19 | +
|
| 20 | +|- CMakeLists.txt // project loader script. load and build dependencies |
| 21 | +|- main/ // main project directory |
| 22 | +| | |
| 23 | +| |- CMakeLists.txt // projects CMakeLists.txt |
| 24 | +| |- src/ // source folder |
| 25 | +| |- test/ // test folder |
| 26 | +| |
| 27 | +|- cert/ // folder with test certificates |
| 28 | +
|
| 29 | +``` |
| 30 | +``` |
| 31 | +- src/ |
| 32 | + | |
| 33 | + |- controller/ // Folder containing Controller where all endpoints are declared |
| 34 | + |- client/ // HTTP client is here. Used in "proxy" endpoint /api/get |
| 35 | + |- dto/ // DTOs are declared here |
| 36 | + |- AppComponent.hpp // Service config |
| 37 | + |- Logger.hpp // Application Logger |
| 38 | + |- App.cpp // main() is here |
| 39 | + |
| 40 | +``` |
| 41 | + |
| 42 | +--- |
| 43 | + |
| 44 | +### Build and Run |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +#### Using CMake |
| 49 | +*Requires* LibreSSL installed. You may refer to this sh script - how to install libressl - |
| 50 | +[install-libressl.sh](https://github.com/oatpp/oatpp-libressl/blob/master/utility/install-deps/install-libressl.sh) |
| 51 | + |
| 52 | +``` |
| 53 | +$ mkdir build && cd build |
| 54 | +$ cmake .. |
| 55 | +$ make run ## Download, build, and install all dependencies. Run project |
| 56 | +
|
| 57 | +``` |
| 58 | + |
| 59 | +#### In Docker |
| 60 | + |
| 61 | +``` |
| 62 | +$ docker build -t example-libressl . |
| 63 | +$ docker run -p 8443:8443 -t example-libressl |
| 64 | +``` |
| 65 | + |
| 66 | +--- |
| 67 | + |
| 68 | +### Configure AppComponent |
| 69 | + |
| 70 | +Configure server secure connection provider |
| 71 | + |
| 72 | +```c++ |
| 73 | + |
| 74 | +/** |
| 75 | + * Create ConnectionProvider component which listens on the port |
| 76 | + */ |
| 77 | +OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, serverConnectionProvider)([] { |
| 78 | + /* non_blocking connections should be used with AsyncHttpConnectionHandler for AsyncIO */ |
| 79 | + auto config = oatpp::libressl::Config::createDefaultServerConfig("cert/test_key.pem", "cert/test_cert.crt"); |
| 80 | + return oatpp::libressl::server::ConnectionProvider::createShared(config, 8443, true /* true for non_blocking */); |
| 81 | +}()); |
| 82 | + |
| 83 | +``` |
| 84 | +
|
| 85 | +Configure client secure connection provider |
| 86 | +
|
| 87 | +```c++ |
| 88 | +OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ClientConnectionProvider>, sslClientConnectionProvider) ([] { |
| 89 | + auto config = oatpp::libressl::Config::createShared(); |
| 90 | + tls_config_insecure_noverifycert(config->getTLSConfig()); |
| 91 | + tls_config_insecure_noverifyname(config->getTLSConfig()); |
| 92 | + return oatpp::libressl::client::ConnectionProvider::createShared(config, "httpbin.org", 443); |
| 93 | +}()); |
| 94 | +``` |
| 95 | + |
| 96 | +### Endpoints |
| 97 | +--- |
| 98 | +"Hello Async" root endpoint with json response |
| 99 | +```c++ |
| 100 | +ENDPOINT_ASYNC("GET", "/", Root) { |
| 101 | + |
| 102 | + ENDPOINT_ASYNC_INIT(Root) |
| 103 | + |
| 104 | + Action act() override { |
| 105 | + auto dto = HelloDto::createShared(); |
| 106 | + dto->message = "Hello Async!"; |
| 107 | + dto->server = Header::Value::SERVER; |
| 108 | + dto->userAgent = request->getHeader(Header::USER_AGENT); |
| 109 | + return _return(controller->createDtoResponse(Status::CODE_200, dto)); |
| 110 | + } |
| 111 | + |
| 112 | +}; |
| 113 | +``` |
| 114 | +
|
| 115 | +result: |
| 116 | +``` |
| 117 | +$ curl -X GET "https://localhost:8443/" --insecure |
| 118 | +{"user-agent": "curl\/7.54.0", "message": "Hello Async!", "server": "oatpp\/0.19.1"} |
| 119 | +``` |
| 120 | +--- |
| 121 | +Async proxy endpoint to ```https://httpbin.org/get``` |
| 122 | +
|
| 123 | +```c++ |
| 124 | +ENDPOINT_ASYNC("GET", "/api/get", TestApiGet) { |
| 125 | +
|
| 126 | + ENDPOINT_ASYNC_INIT(TestApiGet) |
| 127 | +
|
| 128 | + Action act() override { |
| 129 | + return controller->myApiClient->apiGetAsync(this, &TestApiGet::onResponse); |
| 130 | + } |
| 131 | +
|
| 132 | + Action onResponse(const std::shared_ptr<IncomingResponse>& response){ |
| 133 | + return response->readBodyToStringAsync(this, &TestApiGet::returnResult); |
| 134 | + } |
| 135 | +
|
| 136 | + Action returnResult(const oatpp::String& body) { |
| 137 | + return _return(controller->createResponse(Status::CODE_200, body)); |
| 138 | + } |
| 139 | +
|
| 140 | +}; |
| 141 | +``` |
| 142 | + |
| 143 | +result: |
| 144 | +``` |
| 145 | +$ curl -X GET "https://localhost:8443/api/get" --insecure |
| 146 | +{ |
| 147 | + "args": {}, |
| 148 | + "headers": { |
| 149 | + "Connection": "close", |
| 150 | + "Host": "httpbin.org" |
| 151 | + }, |
| 152 | + "origin": "176.37.47.230", |
| 153 | + "url": "https://httpbin.org/get" |
| 154 | +} |
| 155 | +``` |
0 commit comments