Skip to content

Instantly share code, notes, and snippets.

@gsw945
Forked from simon-cj/beego http reverse proxy
Created October 9, 2021 10:18
Show Gist options
  • Save gsw945/8284338069e78e02be01b4be66b6532d to your computer and use it in GitHub Desktop.
Save gsw945/8284338069e78e02be01b4be66b6532d to your computer and use it in GitHub Desktop.
package main
import (
"encoding/base64"
"net/http"
"net/http/httputil"
"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
)
func reverseProxy(c *context.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.ResponseWriter, c.Request)
}
func startServer() {
beego.Any("/api/v2.0/projects", reverseProxy)
beego.Run(":8888")
}
func main() {
startServer()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment