-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
89 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
module gateway | ||
|
||
go 1.19 | ||
go 1.21 | ||
|
||
require ( | ||
github.com/creasty/defaults v1.7.0 | ||
github.com/sirupsen/logrus v1.9.3 | ||
github.com/vibrantbyte/go-antpath v1.1.1 | ||
gopkg.in/yaml.v2 v2.4.0 | ||
) | ||
|
||
require ( | ||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect | ||
github.com/sirupsen/logrus v1.9.3 // indirect | ||
github.com/vibrantbyte/go-antpath v1.1.1 // indirect | ||
golang.org/x/sys v0.10.0 // indirect | ||
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package util | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httputil" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
// ServeReverseProxy Serve a reverse proxy for a given url | ||
func ServeReverseProxy(target string, res http.ResponseWriter, req *http.Request) { | ||
// parse the url | ||
remote, _ := url.Parse(target) | ||
|
||
// create the reverse proxy | ||
proxy := httputil.NewSingleHostReverseProxy(remote) | ||
|
||
proxy.Director = func(request *http.Request) { | ||
targetQuery := remote.RawQuery | ||
request.URL.Scheme = remote.Scheme | ||
request.URL.Host = remote.Host | ||
request.Host = remote.Host // todo 这个是关键 | ||
request.URL.Path, request.URL.RawPath = joinURLPath(remote, request.URL) | ||
|
||
if targetQuery == "" || req.URL.RawQuery == "" { | ||
req.URL.RawQuery = targetQuery + req.URL.RawQuery | ||
} else { | ||
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery | ||
} | ||
|
||
if targetQuery == "" || request.URL.RawQuery == "" { | ||
request.URL.RawQuery = targetQuery + request.URL.RawQuery | ||
} else { | ||
request.URL.RawQuery = targetQuery + "&" + request.URL.RawQuery | ||
} | ||
} | ||
|
||
// 修改响应头 | ||
proxy.ModifyResponse = func(response *http.Response) error { | ||
response.Header.Add("Access-Control-Allow-Origin", "*") | ||
return nil | ||
} | ||
|
||
// Note that ServeHttp is non blocking and uses a go routine under the hood | ||
proxy.ServeHTTP(res, req) | ||
} | ||
|
||
// go sdk 源码 | ||
func singleJoiningSlash(a, b string) string { | ||
aslash := strings.HasSuffix(a, "/") | ||
bslash := strings.HasPrefix(b, "/") | ||
switch { | ||
case aslash && bslash: | ||
return a + b[1:] | ||
case !aslash && !bslash: | ||
return a + "/" + b | ||
} | ||
return a + b | ||
} | ||
|
||
// go sdk 源码 | ||
func joinURLPath(a, b *url.URL) (path, rawpath string) { | ||
if a.RawPath == "" && b.RawPath == "" { | ||
return singleJoiningSlash(a.Path, b.Path), "" | ||
} | ||
// Same as singleJoiningSlash, but uses EscapedPath to determine | ||
// whether a slash should be added | ||
apath := a.EscapedPath() | ||
bpath := b.EscapedPath() | ||
|
||
aslash := strings.HasSuffix(apath, "/") | ||
bslash := strings.HasPrefix(bpath, "/") | ||
|
||
switch { | ||
case aslash && bslash: | ||
return a.Path + b.Path[1:], apath + bpath[1:] | ||
case !aslash && !bslash: | ||
return a.Path + "/" + b.Path, apath + "/" + bpath | ||
} | ||
return a.Path + b.Path, apath + bpath | ||
} |