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>
orResponse
- 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
.