Skip to content

Instantly share code, notes, and snippets.

@simon-cj
Created July 1, 2020 03:20
Show Gist options
  • Save simon-cj/ae08e70fb7d58e8e97797bc9739b354a to your computer and use it in GitHub Desktop.
Save simon-cj/ae08e70fb7d58e8e97797bc9739b354a to your computer and use it in GitHub Desktop.
package main
import (
"encoding/base64"
"net/http"
"net/http/httputil"
"github.com/gin-gonic/gin"
)
func reverseProxy(c *gin.Context) {
proxy := httputil.NewSingleHostReverseProxy(c.Request.URL)
proxy.Director = func(req *http.Request) {
req.Header = c.Request.Header
req.Header["Authorization"] = []string{"basic " + base64.StdEncoding.EncodeToString([]byte("admin:Harbor12345"))}
req.Host = "harbor.caicloud.com"
req.URL.Scheme = "http"
req.URL.Host = "harbor.caicloud.com"
req.URL.Path = c.Request.URL.Path
}
proxy.ServeHTTP(c.Writer, c.Request)
}
func startServer() {
r := gin.Default()
r.Any("/api/v2.0/projects", reverseProxy)
r.Run(":8888")
}
func main() {
startServer()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment