serverino 0.2.3

Small and ready-to-go http/https server


To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:

serverino BUILD & TEST

  • Ready-to-go http/https server
  • Multi-process
  • Dynamic number of workers
  • Zero dub dependencies
  • Just one C dependency (optional, if you use https)

A simple webserver in three lines

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

Documentation you need

Defining more than one endpoint

Every function marked with `@endpoint is called until one writes something to output. The calling order is defined by @priority`

import serverino;
mixin ServerinoMain;

// This function will never block the execution of other endpoints since it doesn't write anything
// In this case `output` param is not needed and this works too: `@priority(10) @endpoint void logger(Request req)`
@priority(10) @endpoint void logger(Request req, Output output) 
{ 
   import std.experimental.logger; // std.experimental.logger works fine!
   log(req.uri);
}

// This endpoint (default priority == 0) handles the homepage
// Request and Output can be used in @safe code
@safe 
@endpoint void hello(Request req, Output output) 
{ 
   // Skip this endpoint if uri is not "/"
   if (req.uri != "/") return;

   output ~= "Hello world!";
}

// This function will be executed only if `hello(...)` doesn't write anything to output.
@priority(-10000) @endpoint void notfound(const Request req, Output output) 
{
   output.status = 404;
   output ~= "Not found";
}

@onServerInit UDA

Use `@onServerInit` to configure your server

// Try also `setup(string args[])` if you need to read arguments passed to your application
@onServerInit ServerinoConfig setup()
{
   ServerinoConfig sc = ServerinoConfig.create(); // Config with default params
   sc.addListener("127.0.0.1", 8080);
   sc.addListener("127.0.0.1", 8081);
   sc.setWorkers(2); 
   // etc...
   
   return sc;
}

@onWorkerStart, @onWorkerStop UDAs

/+ 
 When a worker is created, this function is called.
 Useful to init database connection & everything else.
+/
@onWorkerStart
auto start()
{
   // Connect to db...
}

Use serverino across multiple modules

module test;
import serverino;

@endpoint void f1(Request r, Output o) { ... }
module other;
import serverino;

@endpoint void f2(Request r, Output o) { ... }
module main;
import serverino;
import test, other;

mixin ServerinoMain!(other, test); // Current module is always processed

Enable https

Follow these instructions to install libretls. Enable `WithTLS` subconfiguration in your dub project.

dub.sdl:

subConfiguration "serverino" "WithTLS"

dub.json

"subConfigurations" : { "serverino" : "WithTLS" }

Add a https listener:

@onServerInit ServerinoConfig setup()
{
   ServerinoConfig sc = ServerinoConfig.create(); // Config with default params
   
   // https. Probably you need to run server as *root* to access cert files.
   // Please run workers as unprivileged user using setWorkerUser/setWorkerGroup
   // sc.setWorkerUser("www-data"); sc.setWorkerGroup("www-data");
   sc.addListener("127.0.0.1", 8082, "path-to-your/cert.pem", "path-to-your/privkey.pem");
   
   // http if you don't set certs
   sc.addListener("127.0.0.1", 8083);
  
   return sc;
}
Authors:
  • Andrea Fontana
Dependencies:
none
Versions:
0.2.3 2022-Jun-15
0.2.2 2022-May-22
0.2.1 2022-May-13
0.2.0 2022-May-09
0.1.2 2022-May-09
Show all 8 versions
Download Stats:
  • 0 downloads today

  • 7 downloads this week

  • 14 downloads this month

  • 32 downloads total

Score:
1.5
Short URL:
serverino.dub.pm