Skip to content

Commit f9daf62

Browse files
committed
feat: add HTTP server integration sample
1 parent 812f2d4 commit f9daf62

5 files changed

Lines changed: 67 additions & 2 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
66
set(ASYNCIO_VERSION 1.1.0)
77

88
option(BUILD_SAMPLES "Build asyncio samples" ON)
9+
option(BUILD_INTEGRATION_SAMPLES "Build asyncio integration samples" OFF)
910
option(BUILD_SHARED_LIBS "Build using shared libraries" OFF)
1011
option(ASYNCIO_EMBED_CA_CERT "Use built-in CA certificates instead of system certificates" OFF)
1112

sample/CMakeLists.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
if (BUILD_INTEGRATION_SAMPLES)
2+
find_package(httplib CONFIG REQUIRED)
3+
endif ()
4+
15
add_executable(asyncio_tcp_client tcp/client.cpp)
26
target_link_libraries(asyncio_tcp_client PRIVATE asyncio-main)
37

@@ -10,8 +14,13 @@ target_link_libraries(asyncio_tls_client PRIVATE asyncio-main)
1014
add_executable(asyncio_tls_server tls/server.cpp)
1115
target_link_libraries(asyncio_tls_server PRIVATE asyncio-main)
1216

13-
add_executable(asyncio_http http/main.cpp)
14-
target_link_libraries(asyncio_http PRIVATE asyncio-main)
17+
add_executable(asyncio_http_client http/client.cpp)
18+
target_link_libraries(asyncio_http_client PRIVATE asyncio-main)
19+
20+
if (BUILD_INTEGRATION_SAMPLES)
21+
add_executable(asyncio_http_server http/server.cpp)
22+
target_link_libraries(asyncio_http_server PRIVATE asyncio-main httplib::httplib)
23+
endif ()
1524

1625
add_executable(asyncio_ws ws/main.cpp)
1726
target_link_libraries(asyncio_ws PRIVATE asyncio-main)

sample/http/server.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <asyncio/thread.h>
2+
#include <asyncio/signal.h>
3+
#include <asyncio/error.h>
4+
#include <zero/cmdline.h>
5+
#include <httplib.h>
6+
7+
asyncio::task::Task<void> asyncMain(const int argc, char *argv[]) {
8+
zero::Cmdline cmdline;
9+
10+
cmdline.add<std::string>("ip", "IP address to bind");
11+
cmdline.add<std::uint16_t>("port", "Port number to listen on");
12+
13+
cmdline.parse(argc, argv);
14+
15+
const auto ip = cmdline.get<std::string>("ip");
16+
const auto port = cmdline.get<std::uint16_t>("port");
17+
18+
httplib::Server server;
19+
20+
server.Get(
21+
"/hi",
22+
[](const httplib::Request &, httplib::Response &response) {
23+
response.set_content("Hello World!", "text/plain");
24+
}
25+
);
26+
27+
auto signal = asyncio::Signal::make();
28+
29+
co_await race(
30+
asyncio::toThread(
31+
[&] {
32+
if (!server.listen(ip, port)) {
33+
#ifdef _WIN32
34+
throw std::system_error{WSAGetLastError(), std::system_category()};
35+
#else
36+
throw std::system_error{errno, std::system_category()};
37+
#endif
38+
}
39+
},
40+
[&](std::thread::native_handle_type) -> std::expected<void, std::error_code> {
41+
server.stop();
42+
return {};
43+
}
44+
),
45+
asyncio::task::spawn([&]() -> asyncio::task::Task<void> {
46+
co_await asyncio::error::guard(signal.on(SIGINT));
47+
})
48+
);
49+
}

vcpkg.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
"tests"
1616
],
1717
"features": {
18+
"integration-samples": {
19+
"description": "Build integration samples",
20+
"dependencies": [
21+
"cpp-httplib"
22+
]
23+
},
1824
"tests": {
1925
"description": "Build Tests",
2026
"dependencies": [

0 commit comments

Comments
 (0)