Lighttp is a lightweight asynchronous HTTP and WebSocket server library for the D programming language with simple API.
import std.file;
import lighttp;
void main(string[] args) {
Server server = new Server();
server.host("0.0.0.0");
server.host("::");
server.router.add(new Router());
server.router.add(Get("welcome"), new Resource("text/html", read("welcome.html")));
server.run();
}
class Router {
// GET /
@Get("") getIndex(ServerResponse response) {
response.body = "Welcome to lighttp!";
}
// GET /image/uhDUnsj => imageId = "uhDUnsj"
@Get("image", "([a-zA-Z0-9]{7})") getImage(ServerResponse response, string imageId) {
if(exists("images/" ~ imageId)) {
response.contentType = MimeTypes.jpeg;
response.body = read("images/" ~ imageId);
} else {
response.status = StatusCodes.notFound;
}
}
}