gin icon indicating copy to clipboard operation
gin copied to clipboard

feature request: support SCRIPT_NAME in wsgi spec

Open Rainshaw opened this issue 1 year ago • 4 comments

Description

Normally we access the WebApp like http://localhost:7420/ but we may want it to look something like http://example.com/gin. That’s quite common when trying to wrap multiple systems into something that looks like one website to the browser.

There is a SCRIPT_NAME in wsgi spec, which support this scenery, but it is only used in Python and Ruby.

I'm wondering if I made a PR for this, will it be merged? If so, I'd like to work on it.

And I'd like to hear your suggestions on the name of variable of this in Nginx, maybe X-Script-Name or something else?

Rainshaw avatar Nov 11 '22 07:11 Rainshaw

Hi @RainshawGao

Normally we access the WebApp like http://localhost:7420/ but we may want it to look something like http://example.com/gin.

Usually, we will add a reverse proxy(eg: Nginx) behind these systems, does it not works for your scenario?

liuliqiang avatar Nov 14 '22 23:11 liuliqiang

@liuliqiang yeh, we can use a Nginx to achieve this, but it still remains some problems, one is the link generated by gin is always in root path, so it is a wrong link, because the link should have /gin as its prefix. The SCRIPT_NAME in wsgi spec is used to deal with this, it splits the url into SCRIPT_NAME and PATH_INFO, where PATH_INFO is the path for gin to route, and SCRIPT_NAME is for generate new url as its prefix.

Rainshaw avatar Nov 15 '22 02:11 Rainshaw

Hi @RainshawGao , I can image two solutions for your description:

Solution 1: Change the code

Use the router group provided by gin, the example code: https://github.com/gin-gonic/examples/blob/master/group-routes/routes/main.go

Solution 2: Use Nginx rewrite config

        location /gin/api/v1 {
            rewrite ^/gin/(.*)?$ /$1 break;
            proxy_pass              http://127.0.0.1:5000;
        }

such when you request http://127.0.0.1/gin/api/v1 the request send to your application will be http://127.0.0.1:5000/api/v1.

I don't known whether it can help?

liuliqiang avatar Nov 16 '22 03:11 liuliqiang

Hi @liuliqiang , thanks for your reply.

You may not understand what I mean. I want the url generated by gin to be able to automatically bring the prefix. this requires gin to store a prefix in somewhere for generate url.

solution1 has to change code. when we want to move a new subpath, both nginx conf and app code need changing. this solution store a fixed prefix in router group, and not elegant enough.

solution2 didn't solve, I know how to reverse an app by Nginx, but this is not the pain point.

SCRIPT_NAME can be dynamic, what nginx pass to gin, what gin would use. And in this way, app developer dont need to know which path the app is using.

Rainshaw avatar Nov 16 '22 03:11 Rainshaw