Gin是否支持重写HTTP请求并将其转发到特定的API组?

huangapple go评论83阅读模式
英文:

Is Gin support rewrite HTTP requests and forward them to the specific API groups?

问题

我的团队打算重构我们的gin http服务器,以支持按域名进行路由。例如,登录的旧URL是https://www.example.com/login,而新的URL将是https://login.example.com/。我打算重写HTTP请求并将它们转发到旧的/login组,以便所有复杂而混乱的验证和中间件都可以应用到新的样式上。

文档中似乎没有相关示例。

英文:

My team is going to refactor our gin http server to support routing by domain. For example, the old url for login is https://www.example.com/login, and the new url will be https://login.example.com/. I was going to rewrite http requests and forward them to the old /login group so that all the validations and middlewares, which are very complicate and messy, could be apply to the new style.

Seems like there is no examples in the document.

答案1

得分: 1

我会帮你翻译以下内容:

我赞同使用反向代理(例如nginx、Caddy等)在你的服务前面进行路由。

我也可以理解,如果你想对路由有更多的控制,你可以使用中间件。gin引擎支持通过routerInstance.Use(middlewareFunc)使用中间件函数。

中间件只是一个返回gin.HandlerFunc的函数。我在网上找到了一组示例:https://sosedoff.com/2014/12/21/gin-middleware.html

英文:

I'd second the suggestion to use a reverse proxy (e.g. nginx, Caddy, etc) in front of your services to do the routing.

I can also understand if you would like to have a little bit more control over the routing, you can use middlewares. The gin engine supports the use of middleware functions via routerInstance.Use(middlewareFunc).

A middleware is simply a function that returns gin.HandlerFunc. I found this set of examples on the Web: https://sosedoff.com/2014/12/21/gin-middleware.html

huangapple
  • 本文由 发表于 2021年11月8日 17:15:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/69881080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定