Module serverino

Serverino is a small and ready-to-go http server, in D. It is multiplatform and compiles with DMD, LDC and GDC.

The following example shows how to create a server that responds to all requests with the request dump, in a few lines of code.

import serverino;
mixin ServerinoMain;
void hello(const Request req, Output output) { output ~= req.dump(); }

Request is the request object received, and Output is the output you can write to.

A more complete example is the following:

import serverino;
mixin ServerinoMain;

@onServerInit ServerinoConfig setup()
{
   ServerinoConfig sc = ServerinoConfig.create(); // Config with default params
   sc.addListener("127.0.0.1", 8080);
   sc.setWorkers(2);
   // etc...

   return sc;
}

@endpoint
void dump(const Request req, Output output) { output ~= req.dump(); }

@endpoint @priority(1)
@route!"/hello"
void hello(const Request req, Output output) { output ~= "Hello, world!"; }

The function decorated with onServerInit is called when the server is initialized, and it is used to configure the server. It must return a ServerinoConfig object. In this example, the server is configured to listen on localhost:8080, with 2 workers.

Every function decorated with endpoint is an endpoint. They are called in order of priority assigned with priority (default is 0). The first endpoint that write something to the output is the one that will respond to the request.

The route attribute can be used to filter the requests that are passed to the endpoint, using a uri or a bool delegate(Request r) argument. In this example, only requests to /hello are passed to the hello endpoint. The route attribute can be used multiple times to specify multiple routes also using a delegate.