View the Project on GitHub DEGoodmanWilson/luna
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.
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;
});
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});
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}};
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}};
debug_output
: Enable libmicrohttpd debugging. Under the covers, Luna is a wrapper around libmicrohttpd, and sometimes
it is useful to turn this option on to debug why a server won’t start. Of course, this option does nothing unless you
have specified a logging callback.
Default: false
https_mem_key
: A string containing the private key to use for TLS. Must be used in conjunction with https_mem_cert
https_mem_cert
: A string containing the certificate to use for TLS. Must be used in conjunction with https_mem_key
use_thread_per_connection
: Use an independent thread for each connection. Incompatible with use_epoll_if_available
for reasons. Generally not recommended, but you can give it a try if you like.
Default: false
thread_pool_size
: Use more than one thread to serve connections. This is a good option to play with.
Default: 1
thread_stack_size
: Things and stuff
Default: system default
use_epoll_if_available
: Use epoll
if available, or poll
otherwise. Propably a good thing to try as well, even if you’re not on Linux.
Default: false
enable_internal_file_cache
: Cache file descriptors. Keeps files open, so they are faster to serve. This means of course that local changes to the filesystem will generally be ignored.
internal_file_cache_keep_alive
: How long to hold a file in the cache before invalidating it. Once this interval has passed, the next request for this file will fetch it fresh of the disk. 30 minutes is the default. Only has meaning of you’re using enable_internal_file_cache{true}
.
accept_policy_cb
: You can choose to accept or reject connections on the basis of their address. The default is to accept all incoming connections regardless of origin.
Signature: bool cb(const struct sockaddr *, socklen_t)
error_handler_cb
: Render a custom error page.
Signature: void cb(response &response, request_method method, const std::string &path)
unescaper_cb
: You don’t like the default URL unescaping algorithm? Offer up your own!
Signature: std::string cb(const std::string& text)
connection_memory_limit
:
connection_limit
:
connection_timeout
:
per_ip_connection_limit
:
sockaddr_ptr
:
listen_socket
:
nonce_nc_size
:
connection_memory_increment
:
tcp_fastopen_queue_size
:
listening_address_reuse
: