JonathanILevi / lighttp

Lightweight asynchronous HTTP/WS client/server

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

lighttp Logo

DUB Package Build Status

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

}

About

Lightweight asynchronous HTTP/WS client/server

License:MIT License


Languages

Language:D 99.1%Language:HTML 0.9%