Code Monkey home page Code Monkey logo

openwebbeans-peeco's Introduction

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.

openwebbeans-peeco's People

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.