How to redirect URI-based requests in Netty?

I am new to Netty and understand this in more detail. I am working on creating a simple HTTP server. One thing I want to do is handle URI-based routing. I looked around the examples and found several approaches and wanted to see what made the most sense.

  • You have a route handler that will add / remove others based on URIs in HTTPMessage. This seems inefficient if I have to do this for every single request.

  • Ask for the HTTPMessage and HTTPContent route handler inside another object, which will then be passed to the appropriate handler. For example, I can have an InfoHandler that extends the SimpleChannelInboundHandler and the router's InfoHTTPRequest object. Thus, the pipeline remains fixed, and I do not change it on the fly - I create more objects, though.

  • You have one route handler that only has methods for handling different endpoints. I can have a handleInfo method, a handleUpdate method, etc., Moreover, each of them has its own implementation and refers to its own dependencies.

PS - I am using Netty 4.0, and most of my concepts came from various online research and reading Netty In Action.

+4
source share
1 answer

I use a fixed pipeline that is only responsible for decoding / encoding requests / responses (and optional aggregation, compression, static headers, etc.).

The last handler in the pipeline goes to a custom RequestResolver (common for supporting non-HTTP types), which looks something like this:

public interface RequestResolver<T> {
    void execute(@Nonnull ChannelHandlerContext ctx, @Nonnull T req);
}

The request responder is responsible for deciding how to process the request (i.e., if necessary, route it), and usually proceeds to one or more of the actions registered on it or returns 404. It has nothing to do with such a pipeline, except that it accepts ctx with which it is necessary to respond to requests in the queue.

Netty 4 , -01, , RequestResolver Java, Clojure, Clout Compojure.

+1

Source: https://habr.com/ru/post/1529848/


All Articles