DEGoodmanWilson / luna

Yet another web framework—this time in modern C++!
https://luna.goodman-wilson.com
MIT License
101 stars 22 forks source link
api c-plus-plus webframework

Build Status Coverage Status badge

Luna

Full Documentation

A web application and API framework in modern C++

A web application and API framework in modern C++, Luna is designed to be easy to use and integrate with any C++ project that needs to serve HTTP endpoints. Luna’s core philosophy is that it should be easy to use correctly and difficult to use incorrectly. Of course, it should be robust as well.

You are writing in C++ (because C++ is awesome), and your app needs to provide a lightweight HTTP server to communicate with other web services. libmicrohttpd is super-awesome, except for that idiomatically C API. Luna is an idiomatically C++ wrapper for libmicrohttpd that leans on modern C++ programming concepts.

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. Of course, you can set headers and mime types, too.

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;

    // Handle GET requests to "localhost:8080/endpoint"
    // Respond with a tiny bit of fun JSON
    auto router = server.create_router("/");

    router->set_mime_type("application/json"); //the default is "text/html; charset=utf-8"

    router->handle_request(request_method::GET, "/endpoint",
                          [](auto request) -> response
    {
        return {"{\"made_it\": true}"};
    });

    server.start(); //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