change parameter prefix of type string to prefixes of type slice for command enums (#2662)

This commit is contained in:
John Guo
2023-05-24 14:12:49 +08:00
committed by GitHub
parent 032d085619
commit f6dbe1a40c
3 changed files with 32 additions and 20 deletions

View File

@ -18,7 +18,7 @@ const (
// StartServer1 starts Server1: A simple http server for demo.
func StartServer1() {
s := g.Server(1)
s.BindHandler("/", func(r *ghttp.Request) {
s.BindHandler("/*", func(r *ghttp.Request) {
r.Response.Write("response from server 1")
})
s.BindHandler("/user/1", func(r *ghttp.Request) {
@ -33,11 +33,17 @@ func StartServer1() {
func StartServer2() {
s := g.Server(2)
u, _ := url.Parse(UpStream)
s.BindHandler("/*", func(r *ghttp.Request) {
proxy := httputil.NewSingleHostReverseProxy(u)
proxy.ErrorHandler = func(writer http.ResponseWriter, request *http.Request, e error) {
writer.WriteHeader(http.StatusBadGateway)
}
proxy := httputil.NewSingleHostReverseProxy(u)
proxy.ErrorHandler = func(writer http.ResponseWriter, request *http.Request, e error) {
writer.WriteHeader(http.StatusBadGateway)
}
s.BindHandler("/proxy/*url", func(r *ghttp.Request) {
var (
originalPath = r.Request.URL.Path
proxyToPath = "/" + r.Get("url").String()
)
r.Request.URL.Path = proxyToPath
g.Log().Infof(r.Context(), `server2:"%s" -> server1:"%s"`, originalPath, proxyToPath)
proxy.ServeHTTP(r.Response.Writer.RawWriter(), r.Request)
})
s.SetPort(PortOfServer2)