package middleware import ( "context" "fmt" "github.com/gofrs/uuid" "github.com/labstack/echo/v4" "net/http" ) func InstanceId() echo.MiddlewareFunc { return func(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { s := c.Request().Header.Get("X-Instance-ID") if s == "" { return echo.NewHTTPError(http.StatusForbidden, "missing or malformed instance id") } id, err := uuid.FromString(s) if err != nil { return echo.NewHTTPError(http.StatusForbidden, "missing or malformed instance id") } value := context.WithValue(c.Request().Context(), keyInstanceId, id) c.SetRequest(c.Request().WithContext(value)) return next(c) } } } func GetInstanceId(ctx context.Context) (uuid.UUID, error) { id, ok := ctx.Value(keyInstanceId).(uuid.UUID) if !ok { return uuid.Nil, fmt.Errorf("missing or malformed instance id") } return id, nil }