/
pyo
/
updater
Обзор
Документация
Войти
/
pyo
/
updater
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
develop
pkg/middleware/middleware.go
140 строк
3 KB
ilovepitsa
restore from another repo
16 июн 2025, 18:36
16 июн 2025, 18:36
91b9a34
Код
Авторство
О чём код?
package middleware import ( "fmt" "strings" "github.com/labstack/echo/v4" "gitverse.ru/pyo/updater/pkg/server" "gitverse.ru/pyo/updater/pkg/util" ) const ( EchoCtxKeyPrefix = "updater_service_" EchoCtxKeyRouteInfo = EchoCtxKeyPrefix + "route_info" EchoCtxKeyBinds = EchoCtxKeyPrefix + "binds" EchoCtxKeyIdempotentRetry = EchoCtxKeyPrefix + "idempotent_retry" ) func EchoCtxGet[T any](ctx echo.Context, key string) (T, bool) { var out T val := ctx.Get(key) if val == nil { return out, false } out, ok := val.(T) return out, ok } func MiddlewareBinds() echo.MiddlewareFunc { return func(next echo.HandlerFunc) echo.HandlerFunc { return func(ctx echo.Context) error { info, ok := EchoCtxGet[server.EchoRouteInfo](ctx, EchoCtxKeyRouteInfo) if !ok || info.Bind == nil { return next(ctx) } binds, err := info.Bind(ctx, &echo.DefaultBinder{}) if info.OperationID == "PublishComponent" { binds, err = EchoPublishBind(ctx) } else if info.OperationID == "PublishComponentVersion" { binds, err = EchoPublishComponentVersionBind(ctx, &echo.DefaultBinder{}) } if err != nil { fmt.Printf("failed to bind route params with echo default binder %v\n", err) } else { ctx.Set(EchoCtxKeyBinds, binds) } return next(ctx) } } } func EchoPutPublishNameVersionBind(ctx echo.Context, binder server.EchoBinder) (any, error) { var ( ebe server.EchoBindError params server.EchoPutPublishNameVersionParams ) _ = params ebe.Path = binder.BindPathParams(ctx, ¶ms.Path) descRaw := ctx.FormValue("description") if descRaw == "" { return nil, fmt.Errorf("description empty") } fileHeader, err := ctx.FormFile("file") if err != nil { return nil, err } params.Body = server.PutPublishNameVersionMultipartRequestBody{ Description: descRaw, } params.Body.File.InitFromMultipart(fileHeader) return params, nil } func EchoPublishComponentVersionBind(ctx echo.Context, binder server.EchoBinder) (any, error) { var ( ebe server.EchoBindError params server.EchoPublishComponentVersionParams ) ebe.Path = binder.BindPathParams(ctx, ¶ms.Path) descRaw := ctx.FormValue("description") if descRaw == "" { return nil, fmt.Errorf("description empty") } fileHeader, err := ctx.FormFile("file") if err != nil { return nil, err } params.Body = server.PublishComponentVersionMultipartRequestBody{ Description: descRaw, } params.Body.File.InitFromMultipart(fileHeader) return params, nil } func EchoPublishBind(ctx echo.Context) (any, error) { var params server.EchoPublishComponentParams descRaw := ctx.FormValue("description") if descRaw == "" { return nil, fmt.Errorf("description empty") } fileHeader, err := ctx.FormFile("file") if err != nil { return nil, err } params.Body = server.PublishComponentMultipartRequestBody{ Description: descRaw, } params.Body.File.InitFromMultipart(fileHeader) return params, nil } func MiddlewareRouteInfo(next echo.HandlerFunc) echo.HandlerFunc { return func(ctx echo.Context) error { method := strings.ToUpper(ctx.Request().Method) info, ok := server.EchoRoutes[method+";"+ctx.Path()] if ok { ctx.Set(EchoCtxKeyRouteInfo, info) } return next(ctx) } } func EchoRouteName(ctx echo.Context) string { val := ctx.Get(EchoCtxKeyRouteInfo) if val == nil { return "serving_http_request" } info, _ := val.(server.EchoRouteInfo) if len(info.OperationID) == 0 || !util.IsLettersAndDigits(info.OperationID) { return "serving_http_request" } return info.OperationID }