initial project push
32 files changed
tree: 92caaa69cb471b91f284d1a0138cb5233d14332b
  1. .gitignore
  2. LICENSE
  3. NOTICE
  4. README.md
  5. api/
  6. impl/
  7. pom.xml
  8. showcase/
README.md

Peeco: an OWB centric HTTP microserver

This repository is one of the results of a GSOC 2020 (Google Summer of Code) project for OpenWebBeans at ASF. Its intention is to provide a very small and lightweight HTTP server with CDI functionalities at its core, that should hopefully become natively runnable soon, as a GraalVM native-image. For the server components, Netty is used. This repository contains the API, implementation and a showcase in the respective sub-modules.

Here's an example for how to configure and build this server:

public HttpServer init()
{
	return new HttpServer.Builder()
			.port(0) //applies a random valid port
			.ssl(false)
			.host("localhost")
			.build();
}

And here's how you can use the API with the HttpHandler annotation:

@Inject private HttpServer httpServer;

@HttpHandler(method = {HttpMethod.GET}, url = "/hello/*", matching = Matching.WILDCARD)
public CompletionStage<Response> helloWorld(Request request)
{
	String responseContent = "Hello World from " + httpServer.getHost() + ":" + httpServer.getPort() + " !";
	ByteArrayInputStream output = new ByteArrayInputStream(responseContent.getBytes(StandardCharsets.UTF_8));

	return CompletableFuture.supplyAsync(() ->
	{
		Response response = new Response();
		response.addHeader("statusCode", "200");
		response.addHeader("content-type", "text/html");
		response.setOutput(output);
		return response;
	});
}

This is a reactive example with CompletionStage; you can also straight up use Response as the method return type. The formal requirements of a valid handler method and annotation are:

  • return type must be CompletionStage<Response> or Response
  • first method parameter must be Request
  • Matching.WILDCARD needs a star at the end of the given url

For more examples please refer to the respective showcase. The implementation is found here.

PeecoExtension.java collects all methods that are annotated with our @HttpHandler and starts up the Netty Server. The flow of the request/response handling is then happening in PeecoChannelHandler.java.