用golang的gin框架实现简单的代理转发

package main

import (
   "github.com/gin-gonic/gin"
   "net/http/httputil"
   "net/http"
   "fmt"
)


const Host  = "***.***.*.**:****"

var simpleHostProxy = httputil.ReverseProxy{
   Director: func(req *http.Request) {
      req.URL.Scheme = "http"
      req.URL.Host = Host
      req.Host = Host
   },
}

func main() {
   engine := gin.New()
   vi := engine.Group("/api/v1")
   vi.Any("/*action", WithHeader)
   err := engine.Run(":8341")
   if err != nil {
      fmt.Println(err)
   }
}

func WithHeader(ctx *gin.Context) {

   ctx.Request.Header.Add("requester-uid", "id")
   simpleHostProxy.ServeHTTP(ctx.Writer, ctx.Request)
}

你可能感兴趣的:(golang,gin,代理,转发)