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

how to use middleware to change requestpath #1867

Open
NoGroceries opened this issue Apr 24, 2019 · 5 comments
Open

how to use middleware to change requestpath #1867

NoGroceries opened this issue Apr 24, 2019 · 5 comments

Comments

@NoGroceries
Copy link

NoGroceries commented Apr 24, 2019

I want to trim the common prefix of requestUrl in the middleware ,

if ok :=strings.HasPrefix(requestURI,"/api");ok { c.Request.URL.Path = strings.TrimPrefix(requestURI,"/api") c.Request.RequestURI = strings.TrimPrefix(requestURI,"/api") }
but it don't work

Description

Screenshots

@dmarkham
Copy link
Contributor

Hello @NoGroceries,
I would like to help you debug this but i will need a little more info..

  1. I would like you to share a full example of the code your using and how your adding it into the router.
  2. Where Do you want it to see the change? In the logs, other middleware that runs after you, the handlerfunc, Static File serving, cookie path, etc.
  3. Maybe you can explain your motivation and that will help me understand!

@NoGroceries
Copy link
Author

Hello@dmarkham
Thank you for your reply
Our team is developing a software. It has a gateway and multiple micro services。All requests go through gateway, the front end uses ngnix and the same URL prefix.but the gateway need to remove the same prefix,the rest request url is the real url

@dmarkham
Copy link
Contributor

dmarkham commented May 9, 2019

Ok so the request getting to gin looks like GET /api/users/12345 you then want to trim off /api in middleware and then have it route from that point forward internally to /users/12345?

If I understand correctly i'm not sure you can do this. By time your running middleware I think the system has already picked a handlerFunc. Maybe someone else has some ideas.

@yyq2013
Copy link

yyq2013 commented May 26, 2019

Is it what you want to do? like url rewrite?
I just write one for to ignore contextpath, if it in url.

import (
	"github.com/gin-gonic/gin"
	"github.com/golang/glog"
	"net/http"
	"strings"
)

type SkipContextPathRouter struct {
	ContextPath string
	Start       int
	*gin.Engine
}

func (r *SkipContextPathRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	if strings.HasPrefix(req.RequestURI, r.ContextPath) {
		glog.Infof("request url:%s, will skip prefix:%s", req.RequestURI, r.ContextPath)
		// remove context path before gin router process
		uri := req.RequestURI[r.Start:]
		req.RequestURI = uri
		req.URL.Path = req.URL.Path[r.Start:]
	}
	r.Engine.ServeHTTP(w, req)
}

contextPathToSkip := "/api/v1/"
	skipContextPathRouter := &router.SkipContextPathRouter{
		ContextPath: contextPathToSkip,
		Start:       len(contextPathToSkip),
		Engine:      ginRouter,
	}
	s := &http.Server{
		Addr:           ":8080",
		Handler:        skipContextPathRouter,
		MaxHeaderBytes: 1 << 20,
	}

@hrabkin
Copy link

hrabkin commented Nov 21, 2024

It doesn't work for me as well,

func AppRewriter() gin.HandlerFunc {
	return func(c *gin.Context) {
		prefix := "/app"
		c.Request.URL.Path = strings.TrimPrefix(c.Request.URL.Path, prefix)
		c.Request.RequestURI = c.Request.URL.Path
		c.Next()
	}
}

router := gin.Default()
router.Use(AppRewriter())

router.POST("/update-option", updateOption)

I call 127.0.0.1/app/update-option but get 404

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

4 participants