Giter Club home page Giter Club logo

v60's Introduction

v60

v60 is an experimental HTTP server framework written in C++20.

Quick start

#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!
*/

URL Parameters

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.

Groups and bindings

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.

Building

v60 uses a bunch of C++20 features requires quite recent compiler.

The toolchains I’ve tested with are:

  1. MSVC 19.28

  2. 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:

MSVC

mkdir build
cd build
cmake -G "Visual Studio 16 2019" -A x64 ..
cmake --build . --target backend --config Debug

GCC/Clang

mkdir build
cd build
cmake -G Ninja ..
ninja backend

v60's People

Contributors

fatihbakir avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.