Skip to content

Commit

Permalink
Feature/improve docs
Browse files Browse the repository at this point in the history
- Alias boost::beast::http directly, less confusing
- Install all examples
  • Loading branch information
luketokheim authored Dec 15, 2022
1 parent 13cc2c7 commit 172bd1c
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 32 deletions.
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ RUN cmake .. -GNinja \
RUN cmake --build .

# Install
RUN cmake --install .
RUN strip /usr/local/bin/usrv-hello
RUN cmake --install . --strip

FROM scratch as runtime

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ And a main function.
#include <httpmicroservice/service.hpp>
namespace asio = boost::asio;
namespace http = boost::beast::http;
namespace usrv = httpmicroservice;
namespace http = httpmicroservice::http;
int main()
{
Expand Down Expand Up @@ -131,11 +131,11 @@ libraries with [find_package](https://cmake.org/cmake/help/latest/command/find_p

```console
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake
cmake --build .
cmake --build . --config=Release
```

Run tests.

```console
ctest
ctest -C Release
```
6 changes: 5 additions & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ add_executable(usrv-echo echo.cpp)
target_link_libraries(
usrv-echo PRIVATE httpmicroservice::httpmicroservice)

install(TARGETS usrv-echo RUNTIME)

add_executable(usrv-hello hello.cpp)
target_link_libraries(
usrv-hello PRIVATE httpmicroservice::httpmicroservice)

install(TARGETS usrv-hello RUNTIME)

add_executable(usrv-producer producer.cpp)
target_link_libraries(
usrv-producer PRIVATE httpmicroservice::httpmicroservice)

install(TARGETS usrv-hello RUNTIME)
install(TARGETS usrv-producer RUNTIME)
3 changes: 2 additions & 1 deletion examples/echo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include <exception>

namespace asio = boost::asio;
namespace http = boost::beast::http;
namespace usrv = httpmicroservice;
namespace http = httpmicroservice::http;

// Handle POST requests, echo back the body contents
usrv::response post(const usrv::request& req)
Expand Down Expand Up @@ -41,6 +41,7 @@ usrv::response get(const usrv::request& req)
return usrv::response{http::status::not_found, req.version()};
}

// Convert from boost::string_view
std::string_view target{req.target().data(), req.target().size()};

usrv::response res{http::status::ok, req.version()};
Expand Down
2 changes: 1 addition & 1 deletion examples/hello.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#include <exception>

namespace asio = boost::asio;
namespace http = boost::beast::http;
namespace usrv = httpmicroservice;
namespace http = httpmicroservice::http;

asio::awaitable<usrv::response> hello(usrv::request req)
{
Expand Down
2 changes: 1 addition & 1 deletion examples/producer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#include <thread>

namespace asio = boost::asio;
namespace usrv = httpmicroservice;
namespace http = boost::beast::http;
namespace usrv = httpmicroservice;

// Runs on the pool executor which runs in its own thread.
asio::awaitable<usrv::response> producer(usrv::request req)
Expand Down
7 changes: 3 additions & 4 deletions include/httpmicroservice/service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include <boost/asio/awaitable.hpp>
#include <boost/asio/co_spawn.hpp>
#include <boost/asio/detached.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/use_awaitable.hpp>
Expand All @@ -20,11 +19,11 @@ namespace asio = boost::asio;
socket stream connection.
loop {
// Incoming socket connection
stream = accept()
// One coroutine per session. When it is complete, call the Reporter
// with the session stats.
stats = await session(stream)
// HTTP request/response loop
co_spawn session(stream)
}
*/
template <typename Acceptor, typename Handler, typename Reporter>
Expand Down
23 changes: 5 additions & 18 deletions tests/test_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
#include <string>
#include <thread>

namespace asio = boost::asio;
namespace http = boost::beast::http;
namespace usrv = httpmicroservice;

TEST_CASE("async_run", "[service]")
{
using namespace std::chrono_literals;
namespace asio = boost::asio;
namespace http = boost::beast::http;
namespace usrv = httpmicroservice;
using tcp = boost::asio::ip::tcp;

constexpr auto kBodyNumBytes = 1000;
Expand Down Expand Up @@ -118,9 +119,6 @@ TEST_CASE("async_run", "[service]")

TEST_CASE("make_co_handler", "[service]")
{
namespace asio = boost::asio;
namespace usrv = httpmicroservice;

asio::io_context ioc;

const auto tid = std::this_thread::get_id();
Expand Down Expand Up @@ -154,10 +152,6 @@ TEST_CASE("make_co_handler", "[service]")

TEST_CASE("async_run_functor", "[service]")
{
namespace asio = boost::asio;
namespace http = boost::beast::http;
namespace usrv = httpmicroservice;

constexpr auto kPort = 8080;

struct Handler {
Expand All @@ -183,10 +177,6 @@ TEST_CASE("async_run_functor", "[service]")

TEST_CASE("async_run_context", "[service]")
{
namespace asio = boost::asio;
namespace http = boost::beast::http;
namespace usrv = httpmicroservice;

constexpr auto kPort = 8080;

auto handler = [](auto req) -> asio::awaitable<usrv::response> {
Expand All @@ -203,11 +193,8 @@ TEST_CASE("async_run_context", "[service]")

TEST_CASE("integration", "[service]")
{
namespace asio = boost::asio;
namespace http = boost::beast::http;
namespace usrv = httpmicroservice;
using tcp = boost::asio::ip::tcp;
using namespace std::chrono_literals;
using tcp = boost::asio::ip::tcp;

constexpr auto kPort = 8081;

Expand Down
2 changes: 1 addition & 1 deletion tests/test_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <vector>

namespace asio = boost::asio;
namespace http = httpmicroservice::http;
namespace http = boost::beast::http;
namespace usrv = httpmicroservice;

TEST_CASE("async_read_some", "[mock_sock]")
Expand Down

0 comments on commit 172bd1c

Please sign in to comment.