Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override default 404 handling #95

Closed
fatso83 opened this issue Jan 27, 2021 · 2 comments
Closed

Override default 404 handling #95

fatso83 opened this issue Jan 27, 2021 · 2 comments

Comments

@fatso83
Copy link

fatso83 commented Jan 27, 2021

As mentioned in #94 (comment) I was able to integrate this project successfully in an existing Netty based application, just by picking out the relevant pieces. At the moment, I add the netty-http handlers only if the current request matches a root context (like /api/v1). This is because the default logic in this library is to return HTTP 404 if none of the routes match.

That does not work well with Single Page Applications with client side routing that relies on having index.html returned for all non-matching routes on the server!

This is the current setup:

        pipeline.addLast(restApi,
                new SimpleChannelInboundHandler<HttpRequest>(false) {
                    @Override
                    protected void channelRead0(ChannelHandlerContext ctx, HttpRequest msg) throws Exception {
                        final var hasRouter = pipeline.get("router") != null;

                        if (msg.uri().startsWith("/api")) {

                            if (!hasRouter) {
                                final RequestRouter requestRouter = setupRequestRouter(singletonList(new PingHandler()));

                                final var pipeline = ctx.pipeline();
                                pipeline.addAfter(restApi, "router", requestRouter);
                                pipeline.addAfter("router", "dispatcher", new HttpDispatcher());
                            }
                        } else {
                            if (hasRouter) {
                                pipeline.remove("router");
                                pipeline.remove("dispatcher");
                            }
                        }
                        ctx.fireChannelRead(msg);
                    }
                });

What I envision is making today's non-matching routes handling user configurable. To get this to work for us, we would only need to make the handler pass the request on to the next Netty handler in the queue, so a simple boolean flag would suffice.

I am not totally sure how the handling in RequestRouter compares to the one in HttpResourceHandler, though.

In any case, I could make the necessary changes, but is this something the project would/could accept?

@chtyim
Copy link
Contributor

chtyim commented Jan 27, 2021

You can achieve the default handling by adding a method with path @Path("/**") as a catch-all handler. The path matching logic in the library matches from most specific to generic wildcard.

@fatso83
Copy link
Author

fatso83 commented Jan 27, 2021

That was not what I envisioned, but maybe I could get that to work ... 🤔 What I originally wanted was to let the request follow the Chain-of-responsibility pattern to the next handler in the chain, if none of the netty-http HttpRequestHandlers could handle the request. Today the netty-http handler is in the middle of the chain, with some handlers following it, but if I let this be the very last handler I guess that would work, if I just rewrite some of the existing Netty Handlers that follow it into netty-http HttpRequestHandlers. Thanks for the tip.

@fatso83 fatso83 closed this as completed Apr 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants