2026-02-11 16:56:17: merge [main:afeadb1d9a241da30507f3683781558dbccfefc6];
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
aecc42c6d8
commit
d4091f8805
25 files changed
+1664
-92
No files matched your search
@@ -0,0 +1,168 @@
|
||||
// nolint:wrapcheck
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"io"
|
||||
"isp-router-service/domain"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/txix-open/isp-kit/grpc"
|
||||
"github.com/txix-open/isp-kit/grpc/client"
|
||||
"github.com/txix-open/isp-kit/grpc/isp"
|
||||
"github.com/txix-open/isp-kit/json"
|
||||
"github.com/txix-open/isp-kit/requestid"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
// nolint:gochecknoinits
|
||||
func init() {
|
||||
for httpCode, grpcCode := range codeMap {
|
||||
inverseCodeMap[grpcCode] = httpCode
|
||||
}
|
||||
}
|
||||
|
||||
// nolint:gochecknoglobals
|
||||
var (
|
||||
codeMap = map[int]codes.Code{
|
||||
http.StatusOK: codes.OK,
|
||||
http.StatusRequestTimeout: codes.Canceled,
|
||||
http.StatusBadRequest: codes.InvalidArgument,
|
||||
http.StatusGatewayTimeout: codes.DeadlineExceeded,
|
||||
http.StatusNotFound: codes.NotFound,
|
||||
http.StatusConflict: codes.AlreadyExists,
|
||||
http.StatusForbidden: codes.PermissionDenied,
|
||||
http.StatusUnauthorized: codes.Unauthenticated,
|
||||
http.StatusTooManyRequests: codes.ResourceExhausted,
|
||||
http.StatusPreconditionFailed: codes.FailedPrecondition,
|
||||
http.StatusNotImplemented: codes.Unimplemented,
|
||||
http.StatusInternalServerError: codes.Internal,
|
||||
http.StatusServiceUnavailable: codes.Unavailable,
|
||||
}
|
||||
inverseCodeMap = map[codes.Code]int{}
|
||||
)
|
||||
|
||||
type Grpc struct {
|
||||
cli *client.Client
|
||||
}
|
||||
|
||||
func NewGrpc(cli *client.Client) Grpc {
|
||||
return Grpc{
|
||||
cli: cli,
|
||||
}
|
||||
}
|
||||
|
||||
func (p Grpc) PrepareEndpoint(endpoint string, proxyApiPrefix bool) string {
|
||||
endpoint = strings.TrimPrefix(endpoint, "/")
|
||||
if proxyApiPrefix {
|
||||
return endpoint
|
||||
}
|
||||
|
||||
// api/endpoint -> endpoint
|
||||
return strings.TrimPrefix(endpoint, domain.GrpcApiPrefix)
|
||||
}
|
||||
|
||||
func (p Grpc) Handle(ctx *domain.RequestContext) error {
|
||||
body, err := io.ReadAll(ctx.Request().Body)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "grpc: read body")
|
||||
}
|
||||
|
||||
requestId := requestid.FromContext(ctx.Context())
|
||||
|
||||
md := httpHeadersToGrpcMetadata(ctx.Request().Header)
|
||||
md.Set(grpc.ProxyMethodNameHeader, ctx.Endpoint())
|
||||
|
||||
requestContext := metadata.NewOutgoingContext(ctx.Context(), md)
|
||||
|
||||
result, err := p.cli.BackendClient().Request(requestContext, &isp.Message{
|
||||
Body: &isp.Message_BytesBody{BytesBody: body},
|
||||
})
|
||||
if err != nil {
|
||||
return p.writeGrpcError(err, ctx.ResponseWriter(), ctx.Endpoint(), requestId)
|
||||
}
|
||||
|
||||
return p.writeResponse(http.StatusOK, result.GetBytesBody(), requestId, ctx.ResponseWriter())
|
||||
}
|
||||
|
||||
func (p Grpc) Close() error {
|
||||
return p.cli.Close()
|
||||
}
|
||||
|
||||
func (p Grpc) writeGrpcError(err error, w http.ResponseWriter, endpoint string, requestId string) error {
|
||||
status, ok := status.FromError(err)
|
||||
if !ok {
|
||||
return domain.NewHttpError(
|
||||
http.StatusServiceUnavailable,
|
||||
"upstream is not available",
|
||||
errors.WithMessage(err, "grpc proxy"),
|
||||
)
|
||||
}
|
||||
|
||||
statusCode := p.codeToHttpStatus(status.Code())
|
||||
for _, detail := range status.Details() {
|
||||
switch typeOfDetail := detail.(type) {
|
||||
case *isp.Message:
|
||||
switch {
|
||||
case typeOfDetail.GetBytesBody() != nil:
|
||||
return p.writeResponse(statusCode, typeOfDetail.GetBytesBody(), requestId, w)
|
||||
case typeOfDetail.GetListBody() != nil:
|
||||
return p.writeProto(statusCode, typeOfDetail.GetListBody(), requestId, w)
|
||||
case typeOfDetail.GetStructBody() != nil:
|
||||
return p.writeProto(statusCode, typeOfDetail.GetStructBody(), requestId, w)
|
||||
}
|
||||
default:
|
||||
return p.writeProto(statusCode, typeOfDetail, requestId, w)
|
||||
}
|
||||
}
|
||||
|
||||
return domain.NewHttpError(
|
||||
statusCode,
|
||||
status.Message(),
|
||||
errors.WithMessagef(err, "proxy '%s'", endpoint),
|
||||
)
|
||||
}
|
||||
|
||||
func (p Grpc) writeProto(statusCode int, proto any, requestId string, w http.ResponseWriter) error {
|
||||
data, err := json.Marshal(proto)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "marshal grpc details to json")
|
||||
}
|
||||
return p.writeResponse(statusCode, data, requestId, w)
|
||||
}
|
||||
|
||||
func (p Grpc) writeResponse(statusCode int, data []byte, requestId string, w http.ResponseWriter) error {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
if requestId != "" {
|
||||
w.Header().Set(requestid.Header, requestId)
|
||||
}
|
||||
w.WriteHeader(statusCode)
|
||||
_, err := w.Write(data)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "response write")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p Grpc) codeToHttpStatus(code codes.Code) int {
|
||||
s, ok := inverseCodeMap[code]
|
||||
if !ok {
|
||||
return http.StatusInternalServerError
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func httpHeadersToGrpcMetadata(h http.Header) metadata.MD {
|
||||
md := metadata.MD{}
|
||||
for header, values := range h {
|
||||
header = strings.ToLower(header)
|
||||
if strings.HasPrefix(header, "x-") {
|
||||
md.Set(header, values...)
|
||||
}
|
||||
}
|
||||
return md
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"isp-router-service/domain"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/txix-open/isp-kit/lb"
|
||||
)
|
||||
|
||||
// nolint:gochecknoglobals,mnd
|
||||
var (
|
||||
httpTransport = &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DialContext: defaultTransportDialContext(&net.Dialer{
|
||||
Timeout: 3 * time.Second,
|
||||
KeepAlive: 90 * time.Second,
|
||||
}),
|
||||
ForceAttemptHTTP2: true,
|
||||
MaxIdleConns: 1024,
|
||||
MaxIdleConnsPerHost: 256,
|
||||
IdleConnTimeout: 60 * time.Second,
|
||||
TLSHandshakeTimeout: 5 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
}
|
||||
)
|
||||
|
||||
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 {
|
||||
return endpoint
|
||||
}
|
||||
return strings.TrimPrefix(endpoint, domain.ApiPrefix)
|
||||
}
|
||||
|
||||
func (p Http) Handle(ctx *domain.RequestContext) error {
|
||||
host, err := p.hostManager.Next()
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "http: next host")
|
||||
}
|
||||
|
||||
proxy := p.proxies[host]
|
||||
|
||||
req := ctx.Request()
|
||||
req.URL.Scheme = "http"
|
||||
req.URL.Host = host
|
||||
req.Host = host
|
||||
req.URL.Path = ctx.Endpoint()
|
||||
|
||||
proxy.ServeHTTP(ctx.ResponseWriter(), req)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p Http) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func defaultTransportDialContext(dialer *net.Dialer) func(context.Context, string, string) (net.Conn, error) {
|
||||
return dialer.DialContext
|
||||
}
|
||||
Reference in new issue
Block a user