luna

View the Project on GitHub DEGoodmanWilson/luna

A web application and API framework in modern C++

Home

Using Luna

Changelog

TLS/HTTPS

Really, if you’re deploying your site using, e.g. Now.sh, or behind CloudFlare, there isn’t much call for you to explicitly add support for TLS to your Luna-built API. But, if you really must, read on.

Luna provides full TLS support out of the box. You need only feed Luna two things: A server certificate and a private key. Acquiring these assets is a bit beyond the scope of this document, I am very sorry to say, but a quick Google search should get you started. (But see examples/TLS.cpp for a fully working example that uses self-signed keys valid for localhost—feel free to use those keys for development purposes).

Anyway, once you have those two things, you need only pass them to Luna, and Luna will take care of the rest.

#include <luna/luna.h>

using namespace luna;

const char* key_pem = R"key(
-----BEGIN RSA PRIVATE KEY-----
foobar
-----END RSA PRIVATE KEY-----
)key";

const char* cert_pem = R"key(
-----BEGIN CERTIFICATE-----
bazqux
-----END CERTIFICATE-----
)key";


int main(void)
{
    // Naturally, this is where you pass the necessary TLS assets to Luna
    server server{server::https_mem_key{key_pem}, server::https_mem_cert{cert_pem}};
    
    auto router = server.create_router();
   
    router->handle_request(request_method::GET,
                          "/hello_world",
                          [](auto req) -> response
                          {
                              return {"<h1>Hello, World!</h1>"};
                          });

    server.start();

    // Open at https://localhost:8080/hello_world
}

Planned improvements

Support for Let’s Encrypt is on the table for implementation, with the aim of making acquiring the necessary assets—the server certificate and private key—even simpler. Watch this space.


< Prev—Serving static assets | Next—Configuration reference >