v60 is an experimental HTTP server framework written in C++20.
#include <v60/v60.hpp>
int main() {
using namespace v60;
auto root = get<"/hello">([](Request auto req, Response auto resp) -> task<void> {
co_await resp.send("Hello from v60!");
});
server serv(std::move(root));
serv.listen(8080);
}
/*
# Build and run it
curl http://localhost:8080/hello
Hello from v60!
*/
See examples/parameter.cpp for an example
The request URL can be pattern matched and accessed with a type safe and compile time checked interface.
// OK, magic!
get</"user/:userId/hello">([](Request auto req, Response auto resp) -> task<void> {
co_await resp.send("Hello there, " + std::string(get<"userId">(req.params)));
});
// Will fail to compile since there is no user_id parameter!
get</"user/:userId/hello">([](Request auto req, Response auto resp) -> task<void> {
co_await resp.send("Hello there, " + std::string(get<"user_id">(req.params)));
});
The parameters are matched with regular expressions compiled at compile time with the amazing CTRE library.
See examples/backend.cpp for an example
auto name_route = get<"/name">([](Request auto req, Response auto resp) -> task<void> {
co_await resp.send("Hello " + std::string(get<"userId">(req.params)) + ", your name is foo");
});
auto age_route = get<"/age">([](Request auto req, Response auto resp) -> task<void> {
co_await resp.send("Hello " + std::string(get<"userId">(req.params)) + ", your age is 42");
});
auto user_routes = group(name_route, age_route);
auto user = bind<"/user/:userId">(user_routes);
Like with regular URL parameters, bound parameters are also checked. So if you try to access a non-existent parameter in a sub-route, it’ll not compile.
v60 uses a bunch of C++20 features requires quite recent compiler.
The toolchains I’ve tested with are:
-
MSVC 19.28
-
Clang 12 + libcxx (trunk)
GCC-10 failed to build it, but I did not test trunk, though I’d expect it to work.
You’ll need a very recent boost (I tested with 1.75).
If you have a compiler with the necessary features:
mkdir build
cd build
cmake -G "Visual Studio 16 2019" -A x64 ..
cmake --build . --target backend --config Debug