luna

View the Project on GitHub DEGoodmanWilson/luna

A web application and API framework in modern C++

Home

Using Luna

Changelog

Luna has a lot of options. Most of them are based on the options to the underlying libmicrohttpd library that Luna wraps. Options are pretty straightforward to set when creating a server. Read on to learn how to set options, and to learn more about the options available to be set.

Global configuration options

Logger

The Luna logger is a functional type that you provide. To set it, you can pass to luna::set_logger() a function pointer, a non-static class method via std::bind, an std::function object, or a lambda with the following signature

void (luna::log_level, const std::string &)

For example, to log messages to stdout, we could write a lambda:

luna::set_logger([](luna::log_level level, const std::string &message)
{
    std::cout << to_string(level) < ": " << message << std::endl;
});

Server configuration options

As luna::server is the object through which all interactions happen, configuration options are set via the server constructor.

using namespace luna;
server server_1{server::debug_output{true}};
auto server_2 = std::make_unique<server>(server::debug_output{true});

Named configuration options and ordering

All options are passed to the server constructor using the named option pattern: Each option is set using the option name, and the order that options are passed does not matter. Any options not explicitly set are given sensible defaults.

As an example of the named option pattern, let’s configure a server with a connection timeout of 1,000ms, and debug output enabled:

server my_server{server::connection_timeout{1000}, server::debug_output{true}};

Because with the named option pattern order doesn’t matter, we could have just as easily said

server my_server{server::debug_output{true}, server::connection_timeout{1000}};

Options that are callbacks

Some options are for configuring callbacks that you provide. These are easy to set up with C++ lambdas, std::bind, or even plain old function pointers.

For example, accept_policy_cb is an option deciding when to accept or reject a connection.

bool reject_all_connections(const struct sockaddr *add, socklen_t len)
{
    // reject all connections, regardless of origin:
    return false;
}

...

server my_server{server::accept_policy_cb{&reject_all_connections}};

Configuration options reference

Common options

HTTPS / TLS options

Threading options

File cacheing options

Callback options

Options undocumented at the moment because I haven’t gotten around to it yet


< Prev—TLS/HTTPS | Next—Using the project template >