2026-04-23 18:13:14: merge [main:68f79a6bfeb51395820e04cbe827b53c156c0c31];
Create and publish a Docker image / build-and-push-image (push) Canceled after 0s
Create and publish a Docker image / build-and-push-image (push) Canceled after 0s
This commit is contained in:
1 parent
d4091f8805
commit
a42aeacad0
10 files changed
+145
-138
No files matched your search
@@ -57,12 +57,15 @@ func NewGrpc(cli *client.Client) Grpc {
|
||||
|
||||
func (p Grpc) PrepareEndpoint(endpoint string, proxyApiPrefix bool) string {
|
||||
endpoint = strings.TrimPrefix(endpoint, "/")
|
||||
if proxyApiPrefix {
|
||||
if !proxyApiPrefix {
|
||||
return endpoint
|
||||
}
|
||||
|
||||
// api/endpoint -> endpoint
|
||||
return strings.TrimPrefix(endpoint, domain.GrpcApiPrefix)
|
||||
if strings.HasPrefix(endpoint, "api/") {
|
||||
return endpoint
|
||||
}
|
||||
|
||||
return "api/" + endpoint
|
||||
}
|
||||
|
||||
func (p Grpc) Handle(ctx *domain.RequestContext) error {
|
||||
|
||||
+25
-20
@@ -2,6 +2,7 @@ package proxy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
@@ -34,37 +35,26 @@ var (
|
||||
|
||||
type Http struct {
|
||||
hostManager *lb.RoundRobin
|
||||
proxies map[string]*httputil.ReverseProxy
|
||||
}
|
||||
|
||||
func NewHttp(addresses []string) Http {
|
||||
p := Http{
|
||||
hostManager: lb.NewRoundRobin(addresses),
|
||||
proxies: make(map[string]*httputil.ReverseProxy),
|
||||
}
|
||||
|
||||
for _, addr := range addresses {
|
||||
target, _ := url.Parse("http://" + addr)
|
||||
proxy := httputil.NewSingleHostReverseProxy(target)
|
||||
proxy.Transport = httpTransport
|
||||
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
|
||||
_ = domain.NewHttpError(
|
||||
http.StatusServiceUnavailable,
|
||||
"upstream is not available",
|
||||
errors.WithMessagef(err, "http proxy to %s", addr),
|
||||
).WriteError(w)
|
||||
}
|
||||
p.proxies[addr] = proxy
|
||||
}
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
func (p Http) PrepareEndpoint(endpoint string, proxyApiPrefix bool) string {
|
||||
if proxyApiPrefix {
|
||||
if !proxyApiPrefix {
|
||||
return endpoint
|
||||
}
|
||||
return strings.TrimPrefix(endpoint, domain.ApiPrefix)
|
||||
|
||||
if strings.HasPrefix(endpoint, domain.ApiPrefix) {
|
||||
return endpoint
|
||||
}
|
||||
|
||||
return domain.ApiPrefix + endpoint
|
||||
}
|
||||
|
||||
func (p Http) Handle(ctx *domain.RequestContext) error {
|
||||
@@ -73,17 +63,32 @@ func (p Http) Handle(ctx *domain.RequestContext) error {
|
||||
return errors.WithMessage(err, "http: next host")
|
||||
}
|
||||
|
||||
proxy := p.proxies[host]
|
||||
rawUrl := fmt.Sprintf("http://%s", host) // secure HTTP links are reset connection
|
||||
target, err := url.Parse(rawUrl)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "http: parse url")
|
||||
}
|
||||
|
||||
proxy := httputil.NewSingleHostReverseProxy(target)
|
||||
proxy.Transport = httpTransport
|
||||
|
||||
req := ctx.Request()
|
||||
req.URL.Scheme = "http"
|
||||
req.URL.Host = host
|
||||
req.Host = host
|
||||
req.URL.Path = ctx.Endpoint()
|
||||
var resultError error
|
||||
proxy.ErrorHandler = func(writer http.ResponseWriter, request *http.Request, err error) {
|
||||
resultError = domain.NewHttpError(
|
||||
http.StatusServiceUnavailable,
|
||||
"upstream is not available",
|
||||
errors.WithMessagef(err, "http proxy to %s", host),
|
||||
)
|
||||
}
|
||||
|
||||
proxy.ServeHTTP(ctx.ResponseWriter(), req)
|
||||
|
||||
return nil
|
||||
return resultError // nolint:wrapcheck
|
||||
}
|
||||
|
||||
func (p Http) Close() error {
|
||||
|
||||
Reference in new issue
Block a user