Skip to content
/ lighttp Public

Lightweight asynchronous HTTP/WS client/server

License

Notifications You must be signed in to change notification settings

Kripth/lighttp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

}