serverino.config

Configuration module for serverino.

Types 22

enumFallthrough : bool

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!"; }

Yes = trueContinue with the next function
No = falseStop the chain
structpriority

UDA. Set @endpoint priority

Fields
long priority
enumendpoint
enumonDaemonStart
enumonDaemonStop
enumonWorkerStart
enumonWorkerStop
enumonServerInit
enumonWebSocketUpgrade
enumonWebSocketStart
enumonWebSocketStop
enumonWorkerException

UDA. 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
}

enumrequestScope

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;
}

structroute(alias T)

UDA. You can use to filter requests using a function bool(Request request) { } Example:

@endpoint
@route!(x => x.path.startsWith("/api"))
void api(Request r, Output o) { ... }

Methods
bool apply(const Request r) static
aliasroute(string path) = route!(r => comparePath!path(r))

UDA. You can use to filter requests using a path. Example:

@endpoint
@route!"/hello.html"
void api(Request r, Output o) { ... }

structHttps

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.

Fields
Certificate[] _certificates
string[] _errors
Methods
Https add(string certPath, string keyPath) ref @safe returnAdd a certificate to the set. Chainable.
Https add(Certificate certificate) ref @safe returnDitto
Https 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.
Https opOpAssign(string op : "~")(Https other) ref @safe returnDitto
size_t length() @nogc nothrow @safe constHow many certificates are in this set?
bool empty() @nogc nothrow @safe constIs this set empty?
const(Certificate)[] certificates() @nogc nothrow @safe constThe certificates, in insertion order. The first one is the default one.
Constructors
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)`, ...)
Nested Templates
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;
}

Fields
DaemonConfig daemonConfig
WorkerConfig workerConfig
string[] failedListeners
int returnCode
bool forceExit
Methods
ServerinoConfig create() staticCreate a new instance of ServerinoConfig
ServerinoConfig 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 setWorkers(size_t val) @safe ref returnSame as setMaxWorkers(v); setMinWorkers(v);
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 setWorkerUser(string s = string.init) @safe ref returnFor example: "www-data"
ServerinoConfig setWorkerGroup(string s = string.init) @safe ref returnFor example: "www-data"
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 disableLoggerOverride() @safe ref returnDitto
ServerinoConfig enableKeepAlive(bool enable = true, Duration timeout = 3.seconds) @safe ref returnEnable/Disable keep-alive for http/1.1
ServerinoConfig enableKeepAlive(Duration timeout) @safe ref returnDitto
ServerinoConfig disableKeepAlive() @safe ref returnDitto
ServerinoConfig enableWorkerBacklog(size_t depth = 4) @safe ref returnLets the daemon queue up to `depth` requests on a worker that is still busy.
ServerinoConfig disableWorkerBacklog() @safe ref returnDitto
ServerinoConfig enableRemoteIp(bool enable = true) @safe ref returnAdd a x-remote-ip header
ServerinoConfig disableRemoteIp() @safe ref return
ServerinoConfig enableServerSignature(bool enable = true) @safe ref returnEnable/Disable serverino signature
ServerinoConfig disableServerSignature() @safe ref returnDitto
ServerinoConfig 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()
Constructors
Nested Templates
ListenerProtocolProtocol used by listener
Fields
PAUSE_DURATION
size_t pausedCount
bool connectionClosed
size_t fdLimit
size_t fdThreshold
bool paused
CoarseTime resumeAt
Address address
size_t index
Socket socket
Https.Certificate[] certificates
Methods
void onAcceptFailed(int err)
void pause()
void resume()
void warnOnce(string msg) static
Constructors
this(size_t index, Address address, Https.Certificate[] certificates = null)
Fields
Duration maxRequestTime
Duration maxHttpWaiting
Duration maxWorkerLifetime
Duration maxWorkerIdling
Duration maxDynamicWorkerIdling
bool keepAlive
string user
string group
bool serverSignature
Fields
LogLevel logLevel
size_t maxRequestSize
Duration maxHttpWaiting
Duration keepAliveTimeout
size_t daemonInstances
size_t minWorkers
size_t maxWorkers
size_t workerBacklog
int listenerBacklog
size_t maxOpenFiles
bool withRemoteIp
bool overrideLogger
bool autoReload
Listener[] listeners

Functions 1

fnvoid route(string s)

Templates 1

tmplcomparePath(string _path)