proxy

Proxy中间件,可以将指定的请求转发至另外的服务,并可重写url。

Example

  1. package main
  2. import (
  3. "net/url"
  4. "github.com/vicanso/elton"
  5. "github.com/vicanso/elton/middleware"
  6. )
  7. func main() {
  8. e := elton.New()
  9. target, _ := url.Parse("https://www.baidu.com")
  10. e.GET("/*", middleware.NewProxy(middleware.ProxyConfig{
  11. // proxy done will call this function
  12. Done: func(c *elton.Context) {
  13. },
  14. // http request url rewrite
  15. Rewrites: []string{
  16. "/api/*:/$1",
  17. },
  18. Target: target,
  19. // change the request host
  20. Host: "www.baidu.com",
  21. }))
  22. err := e.ListenAndServe(":3000")
  23. if err != nil {
  24. panic(err)
  25. }
  26. }