luna

View the Project on GitHub DEGoodmanWilson/luna

An embedded HTTP(S) server and web framework in modern C++

Getting Started

Using Luna

Changelog

Build Status Coverage Status badge

Luna

An embedded HTTP(S) server and web framework in modern C++, Luna is designed to produce low latency, high throughput we applications with an easy to use interface. You can integrate Luna with any C++ project that needs to serve HTTP endpoints, or build entire webapps from scratch. Luna’s core philosophy is that it should be easy to use correctly and difficult to use incorrectly. Of course, it should be highly performant and robust as well.

Use cases

Either way, Luna is a great choice. HTTP server creation, start, shut down, and deletion are all handled behind the scenes with the magic of RAII. Starting a server is automatic with instantiating a server object, and allowing your server object to fall out of scope is all that is needed to cleanly shut it down. There is nothing else for you to keep track of, or allocate.

Adding endpoints to your server is likewise meant to be simple. Nominate an endpoint with a string or regex and an HTTP verb, and pass in a lambda or other std::functional-compatible object (function pointers, bound class member functions), and return a string containing the desired response body. This kind of flexibility means Luna works with heavily object-oriented code—or more procedural or even functional programming styles, as you prefer.

Of course, you can set headers and mime types, validate parameters, generate custom error pages, all that good stuff too.

Luna also offers a mechanism for inserting middlware for pre-processing and post-process of requests globally.

Example code

But don’t take my word for it. Here is some code for serving a simple JSON snippet from a single endpoint.

#include <string>
#include <luna/luna.h>

using namespace luna;

int main(void)
{
    //start a server delivering JSON by default on the default port 8080
    server server{
        server::mime_type{"application/json"}, //the default is "text/html; charset=UTF-8"
    };

    // Handle GET requests to "localhost:8080/endpoint"
    // Respond with a tiny bit of fun JSON
    server.handle_request(request_method::GET, "/endpoint",
                          [](auto request) -> response
    {
        return {"{\"made_it\": true}"};
    });

    server.await(); //run forever, basically, or until the server decides to kill itself.
}

Prerequisites

A C++14 capable compiler (tested against gcc 4.9, clang 3.6), CMake 2.8. Conan for installing dependencies.

License

MIT

Disclaimer

This library has only begun, and is a constant state of flux.