• Stars
    star
    1,099
  • Rank 40,572 (Top 0.9 %)
  • Language
    C++
  • License
    Other
  • Created over 6 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Cross-platform, efficient, customizable, and robust asynchronous HTTP(S)/WebSocket server C++ library with the right balance between performance and ease of use

What Is RESTinio?

RESTinio is a C++17 library that gives you an embedded HTTP/Websocket server. It is based on standalone version of ASIO and targeted primarily for asynchronous processing of HTTP-requests. Boost::ASIO is also supported.

RESTinio itself is a header-only library, but it depends on nodejs/llhttp that is not header-only.

Currently RESTinio is in rather stable state and we think that since v.0.4.0 it is ready for production use.

A Very Basic Example Of RESTinio

Consider the task of writing a C++ application that must support some REST API, RESTinio represents our solution for that task.

Lets see how it feels like in the simplest case:

#include <restinio/core.hpp>
int main()
{
    restinio::run(
        restinio::on_this_thread()
        .port(8080)
        .address("localhost")
        .request_handler([](auto req) {
            return req->create_response().set_body("Hello, World!").done();
        }));
    return 0;
}

Server runs on the main thread, and respond to all requests with hello-world message. Of course you've got an access to the structure of a given HTTP request, so you can apply a complex logic for handling requests.

Features

  • Async request handling. Cannot get the response data immediately? That's ok, store request handle somewhere and/or pass it to another execution context and get back to it when the data is ready.
  • HTTP pipelining. Works well with async request handling. It might increase your server throughput dramatically.
  • Timeout control. RESTinio can take care of bad connection that are like: send "GET /" and then just stuck.
  • Response builders. Need chunked-encoded body - then RESTinio has a special response builder for you (obviously it is not the only builder).
  • ExpressJS-like request routing (see an example below).
  • An experimental typesafe request router that allows avoiding problems of ExpressJS-like router with help of static checks from C++ compiler.
  • A possibility to chain several request-handlers (somewhat similar to ExpressJS's middleware).
  • Working with query string parameters.
  • Several ready-to-use helpers for working with HTTP headers (for example, the support for HTTP headers related to file uploading).
  • Supports sending files and its parts (with sendfile on linux/unix and TransmitFile on windows).
  • Supports compression (deflate, gzip).
  • Supports TLS (HTTPS).
  • Basic websocket support. Simply restinio::websocket::basic::upgrade() the request handle and start websocket session on a corresponding connection.
  • Can run on external asio::io_context. RESTinio is separated from execution context.
  • Some tune options. One can set acceptor and socket options. When running RESTinio on a pool of threads connections can be accepted in parallel.

Enhanced Example With Express Router

#include <restinio/core.hpp>

using namespace restinio;

template<typename T>
std::ostream & operator<<(std::ostream & to, const optional_t<T> & v) {
    if(v) to << *v;
    return to;
}

int main() {
    // Create express router for our service.
    auto router = std::make_unique<router::express_router_t<>>();
    router->http_get(
            R"(/data/meter/:meter_id(\d+))",
            [](auto req, auto params) {
                const auto qp = parse_query(req->header().query());
                return req->create_response()
                        .set_body(
                                fmt::format("meter_id={} (year={}/mon={}/day={})",
                                        cast_to<int>(params["meter_id"]),
                                        opt_value<int>(qp, "year"),
                                        opt_value<int>(qp, "mon"),
                                        opt_value<int>(qp, "day")))
                        .done();
            });

    router->non_matched_request_handler(
            [](auto req){
                return req->create_response(restinio::status_not_found()).connection_close().done();
            });

    // Launching a server with custom traits.
    struct my_server_traits : public default_single_thread_traits_t {
        using request_handler_t = restinio::router::express_router_t<>;
    };

    restinio::run(
            restinio::on_this_thread<my_server_traits>()
                    .address("localhost")
                    .request_handler(std::move(router)));

    return 0;
}

License

RESTinio is distributed under BSD-3-CLAUSE license.

How To Use It?

The full documentation for RESTinio can be found here.

More

More Repositories

1

sobjectizer

An implementation of Actor, Publish-Subscribe, and CSP models in one rather small C++ framework. With performance, quality, and stability proved by years in the production.
C++
448
star
2

json_dto

A small header-only library for converting data between json representation and c++ structs
C++
118
star
3

so5extra

so5extra is a collection of various SObjectizer's extensions. so5extra is built on top of SObjectizer and intended to simplify development of SObjectizer-based applications.
C++
22
star
4

shrimp-demo

Shrimp is a small HTTP-server that provides shrunk images generated from a specified set.
C++
19
star
5

arataga

A performant socks5/http1.1 proxy-server targeted for serving a large number of entry-points
C++
16
star
6

restinio-crud-example

It is a simple example of CRUD application based on RESTinio library
C++
7
star
7

restinio-conan

Conan package for RESTinio library
Python
6
star
8

sobjectizer-conan

Conan package for SObjectizer framework
Python
5
star
9

mosquitto_transport

mosquitto_transport is an experiment of writing SObjectizer-based wrapper around mosquitto library.
C++
4
star
10

noexcept-ctcheck

A few C++ macros for checking noexcept-ness at the compile time
C++
2
star
11

restinio-long-output-sample

A simple RESTinio-based HTTP-server that responds with arbitrary long responses to incoming request at requested speed.
C++
2
star
12

sobjectizer-fetchcontent-demo

Example of adding SObjectizer to a project via CMake's FetchContent
C++
2
star
13

so5extra-conan

Conan package for so_5_extra extension for SObjectizer framework
Python
2
star
14

restinio-conan-example

An example of RESTinio usage via Conan
Dockerfile
1
star
15

libmosquitto_mxxru

Ruby
1
star
16

sobjectizer-conan-example

An example of SObjectizer usage via Conan
Dockerfile
1
star
17

so5_tricky_thread_pool_disp_en

This is a demo of the creation of own thread_pool dispatcher for SObjectizer-5.6.
C++
1
star
18

rapidjson_mxxru

A light MxxRu wrapper for RapidJSON library.
Ruby
1
star
19

json_dto-conan

Conan package for json_dto library
Python
1
star
20

habrhabr_article_2_ru

Демо-примеры для вводных статей о SObjectizer на Хабре.
C++
1
star
21

so5_mosqtt_demo_ru

C++
1
star
22

so5-dining-philosophers

Several implementations of solutions for "Dining Philosophers" problem built by using Actor and CSP models on top of SObjectizer framework
C++
1
star