networknt / light-4j

A fast, lightweight and more productive microservices framework
Apache License 2.0
3.61k stars 633 forks source link

gzip compression #88

Closed samek closed 7 years ago

samek commented 7 years ago

Hi,

how can I enable compression in the server ?

stevehu commented 7 years ago

I never done that before but I did a search and found this link. It looks like we need to add another handler to wrap our business hander.

https://stackoverflow.com/questions/28295752/compressing-undertow-server-responses

Here is an example on how to wrap your handle to serve files from file system as a web server.

https://github.com/networknt/light-example-4j/blob/master/webserver/src/main/java/com/networknt/webserver/handler/WebServerHandlerProvider.java

Let me know how it goes. Thanks.

samek commented 7 years ago

Works.

eg.

public` HttpHandler getHandler() {

    return new EncodingHandler(new ContentEncodingRepository()
            .addEncodingHandler("gzip",
                    new GzipEncodingProvider(), 50,
                    Predicates.parse("max-content-size[5]")))
            .setNext(
                    Handlers.routing()

                            .add(Methods.GET, "/v2/fronts/{front_id}", new FrontGetHandler(Config.getInstance().getMapper()))

                            .add(Methods.GET, "/v2/articles/{article_id}", new ArticleGetHandler(Config.getInstance().getMapper()))

                            .add(Methods.GET,"/v2/medias/{media_id}", new MediaGetHandler(Config.getInstance().getMapper()))

                            .add(Methods.GET,"/v2/menus/{site_id}", new MenuGetHandler(Config.getInstance().getMapper()))

                            .add(Methods.GET,"/v2/sections/{section_id}", new SectionGetHandler(Config.getInstance().getMapper()))

            );
} 
stevehu commented 7 years ago

@samek Thanks for your confirmation. I will put your findings into the FAQ so that other people can follow your path.

stevehu commented 7 years ago

@samek I have added a FAQ section in the document site. Please review and let me know if anything needs to be added/updated. Thanks.

https://networknt.github.io/light-4j/faq/

samek commented 7 years ago

So .. number should definitely be bigger than 5. In my example if max-content-size[5] means it looks at Content-Length: of output on when to enable it. Which is too small. (the gziped response is actually bigger than the response it self would be in really low size outputs).

So I guess if we're going to publish an example this should be noted/changed. What I found on web was that you should enable it above 150 even tho that nginx default is 20.

Otherwise it looks good.

stevehu commented 7 years ago

@samek That makes perfect sense. I updated the FAQ to reflect your comment. Thanks.