2025-07-01 13:32:55: merge [main:6491e79afd06cd665731c89f8ab2cd373351c1b4];
This commit is contained in:
1 parent
f699aefa58
commit
264b438cdb
12 files changed
+161
-94
No files matched your search
+16
-16
@@ -1,37 +1,37 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/txix-open/isp-kit/log"
|
||||
"isp-gate-service/request"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type EntryPointConfig struct {
|
||||
PathPrefix string
|
||||
WithPrefix bool
|
||||
IsGrpcPath bool
|
||||
}
|
||||
|
||||
func Entrypoint(maxReqBodySize int64, next Handler, logger log.Logger, cfg EntryPointConfig) http.Handler {
|
||||
type EndpointResolver interface {
|
||||
ResolveEndpoint(path string, cfg EntryPointConfig) string
|
||||
}
|
||||
|
||||
func Entrypoint(
|
||||
maxReqBodySize int64,
|
||||
next Handler,
|
||||
cfg EntryPointConfig,
|
||||
entryPointResolver EndpointResolver,
|
||||
logger log.Logger,
|
||||
) http.Handler {
|
||||
return http.HandlerFunc(func(writer http.ResponseWriter, req *http.Request) {
|
||||
req.Body = http.MaxBytesReader(writer, req.Body, maxReqBodySize)
|
||||
ctx := request.NewContext(req, writer, getEndpoint(req, cfg))
|
||||
|
||||
endpoint := entryPointResolver.ResolveEndpoint(req.URL.Path, cfg)
|
||||
ctx := request.NewContext(req, writer, endpoint)
|
||||
|
||||
err := next.Handle(ctx)
|
||||
if err != nil {
|
||||
logger.Error(req.Context(), errors.WithMessage(err, "uncaught error"))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func getEndpoint(req *http.Request, cfg EntryPointConfig) string {
|
||||
if cfg.WithPrefix {
|
||||
return req.URL.Path
|
||||
}
|
||||
if cfg.IsGrpcPath {
|
||||
cfg.PathPrefix += "/"
|
||||
}
|
||||
return strings.TrimPrefix(req.URL.Path, cfg.PathPrefix)
|
||||
}
|
||||
Reference in new issue
Block a user