serverino.config
Configuration module for serverino.
Types 22
Used as optional return type for functions with @endpoint` UDA attached. It is used to override the default behavior of serverino: if an endpoint returns Fallthrough.Yes, the next endpoint is called even if the current one has written to the output.
// Doing a request to the server will return "Hello world!"
// Will continue with the next function
@endpoint @priority(3) auto test_1(Request r, Output o) { output ~= "Hello"; return Fallthrough.Yes; }
// This blocks the chain (default behavior when output is written)
@endpoint @priority(2) auto test_2(Request r, Output o) { output ~= " world!"; }
// Never executed (blocked by test_2)
@endpoint @priority(1) auto test_3(Request r, Output o) { output ~= "Not executed!"; }UDA. Set @endpoint priority
long priorityUDA. Functions with @onWorkerException attached are called when worker throws an exception
@onWorkerException bool myExceptionHandler(Request r, Output o, Exception e)
{
o.status = 500;
info("Oh no! An exception occurred: ", e.msg);
return true; // This means the exception is handled, if false, the exception is rethrown
}UDA. Global variables marked with @requestScope are automatically reset (via destroy) at the beginning and end of each request, ensuring that no data leaks between requests. Example:
@requestScope UserData currentUser;
@endpoint
void handler(Request r, Output o) {
currentUser.name = r.get.read("name"); // reset at start of each request
o ~= currentUser.name;
}UDA. You can use to filter requests using a path. Example:
@endpoint
@route!"/hello.html"
void api(Request r, Output o) { ... }A set of TLS certificates, to be attached to a listener.
The set is accumulable and can be built from a range, so certificates don't need to be known at compile time: they can come from a directory, from the command line or from a configuration file.
// A single certificate, known upfront
config.addListener("0.0.0.0", 443, Https("fullchain.pem", "privkey.pem"));
// Every certificate found in a directory
auto certs = Https();
foreach(f; dirEntries(dir, "*.crt", SpanMode.shallow))
certs.add(f.name, f.name.setExtension(".key"));
config.addListener("0.0.0.0", 443, certs);The first certificate of the set is the default one, the others are picked using SNI.
Certificate[] _certificatesstring[] _errorsHttps add(string[] paths) ref @safe returnAdd an even number of paths, in cert/key order. Chainable.Https opOpAssign(string op : "~")(Certificate certificate) ref @safe returnAppend a certificate, or another set of certificates.const(Certificate)[] certificates() @nogc nothrow @safe constThe certificates, in insertion order. The first one is the default one.this(string[] paths...)Build a set from an even number of paths, in cert/key order.this(R certificates)Build a set from a range of `Https.Certificate`, or of anything indexable with two elements (`string[2]`, `Tuple!(string, string)`, ...)CertificateA certificate and its private key. Both must be PEM encoded.Struct used to setup serverino. You must return this struct from a function with @onServerInit UDA attached.
@onServerInit
auto configure()
{
// You can chain methods
ServerinoConfig config =
ServerinoConfig.create()
.setWorkers(5)
.enableKeepAlive();
return config;
}DaemonConfig daemonConfigWorkerConfig workerConfigstring[] failedListenersint returnCodebool forceExitServerinoConfig setLogLevel(LogLevel level = LogLevel.all) @safe ref returnSets the minimum log level to display. The default is LogLevel.all.ServerinoConfig setReturnCode(int retCode = 0, bool forceExit = false) @safe ref returnAny non-zero value will cause the server to terminate immediately. If forceExit is true, the server will terminate even if retCode is 0.ServerinoConfig setDaemonInstances(size_t val = 1) @safe ref returnSets the number of daemon instances (accept/event loops). Default is 1.ServerinoConfig setMaxWorkers(size_t val = 5) @safe ref returnSets the maximum number of worker processes.ServerinoConfig setMinWorkers(size_t val = 0) @safe ref returnSets the minimum number of worker processes.ServerinoConfig setMaxWorkerLifetime(Duration dur = 6.hours) @safe ref returnSets the maximum lifetime for a worker. After this duration, the worker is terminated.ServerinoConfig setMaxWorkerIdling(Duration dur = 1.hours) @safe ref returnSets the maximum idle time for a worker. After this duration, the worker is terminated.ServerinoConfig enableWorkersAutoReload(bool enable = true) @safe ref returnAutomatic hot reload of workers when the main executable is modified. Be careful: daemon is not reloaded and its code (eg: config) is not updated.ServerinoConfig setMaxDynamicWorkerIdling(Duration dur = 60.seconds) @safe ref returnMax time a dynamic worker can be idle. After this time, worker is terminated. This is used only if the number of workers is greater than minWorkers.ServerinoConfig setListenerBacklog(int val = 2048) @safe ref returnSets the maximum number of pending connections in the listener's backlog.ServerinoConfig setMaxOpenFiles(size_t val = 0) @safe ref returnSets the soft limit of open files (RLIMIT_NOFILE) for the daemon and its workers, capped by the hard limit (POSIX only). It is also the cap on concurrent client connections: a few fds are kept for ...ServerinoConfig setMaxRequestTime(Duration dur = 5.seconds) @safe ref returnSets the maximum duration a request can take. After this time, the worker handling the request is terminated.ServerinoConfig setMaxRequestSize(size_t bytes = 1024 * 1024 * 10) @safe ref returnSets the maximum allowable size for a request. Requests exceeding this size will return a 413 error.ServerinoConfig setHttpTimeout(Duration dur = 10.seconds) @safe ref returnSets the maximum duration the socket will wait for a request after the connection.ServerinoConfig enableLoggerOverride(bool enable = true) @safe ref returnEnables or disables the override of std.logger.ServerinoConfig enableKeepAlive(bool enable = true, Duration timeout = 3.seconds) @safe ref returnEnable/Disable keep-alive for http/1.1ServerinoConfig enableWorkerBacklog(size_t depth = 4) @safe ref returnLets the daemon queue up to `depth` requests on a worker that is still busy.ServerinoConfig enableServerSignature(bool enable = true) @safe ref returnEnable/Disable serverino signatureServerinoConfig addListener(ListenerProtocol p = ListenerProtocol.IPV4)(string address, ushort port) @safe ref returnAdd a new listener. Plain HTTP.ServerinoConfig addListener(ListenerProtocol p = ListenerProtocol.IPV4)(string address, ushort port, Https certificates) @safe ref returnAdd a new HTTPS listener, using the certificates provided. Plain and encrypted listeners can live together in the same process. Requires the `https` subconfiguration of serverino (POSIX only).void validate()this()ListenerProtocolProtocol used by listener PAUSE_DURATIONsize_t pausedCountbool connectionClosedsize_t fdLimitsize_t fdThresholdbool pausedCoarseTime resumeAtDaemonConfigPtr configAddress addresssize_t indexSocket socketHttps.Certificate[] certificatesvoid onConnectionAvailable()void onAcceptFailed(int err)void pause()void resume()void warnOnce(string msg) staticFunctions 1
void route(string s)