mirror of
https://github.com/portainer/portainer.git
synced 2025-08-02 20:35:25 +02:00
feat(edge/update): remote update structure [EE-4040] (#7553)
This commit is contained in:
parent
dd1662c8b8
commit
6c4c958bf0
61 changed files with 1952 additions and 96 deletions
25
api/http/middlewares/featureflag.go
Normal file
25
api/http/middlewares/featureflag.go
Normal file
|
@ -0,0 +1,25 @@
|
|||
package middlewares
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
httperror "github.com/portainer/libhttp/error"
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
"github.com/portainer/portainer/api/dataservices"
|
||||
)
|
||||
|
||||
func FeatureFlag(settingsService dataservices.SettingsService, feature portainer.Feature) mux.MiddlewareFunc {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(rw http.ResponseWriter, request *http.Request) {
|
||||
enabled := settingsService.IsFeatureFlagEnabled(feature)
|
||||
|
||||
if !enabled {
|
||||
httperror.WriteError(rw, http.StatusForbidden, "This feature is not enabled", nil)
|
||||
return
|
||||
}
|
||||
|
||||
next.ServeHTTP(rw, request)
|
||||
})
|
||||
}
|
||||
}
|
59
api/http/middlewares/withitem.go
Normal file
59
api/http/middlewares/withitem.go
Normal file
|
@ -0,0 +1,59 @@
|
|||
package middlewares
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
httperror "github.com/portainer/libhttp/error"
|
||||
"github.com/portainer/libhttp/request"
|
||||
bolterrors "github.com/portainer/portainer/api/dataservices/errors"
|
||||
)
|
||||
|
||||
type ItemContextKey string
|
||||
|
||||
type ItemGetter[TId ~int, TObject any] func(id TId) (*TObject, error)
|
||||
|
||||
func WithItem[TId ~int, TObject any](getter ItemGetter[TId, TObject], idParam string, contextKey ItemContextKey) mux.MiddlewareFunc {
|
||||
if idParam == "" {
|
||||
idParam = "id"
|
||||
}
|
||||
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||||
itemId, err := request.RetrieveNumericRouteVariableValue(req, idParam)
|
||||
if err != nil {
|
||||
httperror.WriteError(rw, http.StatusBadRequest, "Invalid identifier route variable", err)
|
||||
return
|
||||
}
|
||||
|
||||
item, err := getter(TId(itemId))
|
||||
if err != nil {
|
||||
statusCode := http.StatusInternalServerError
|
||||
if err == bolterrors.ErrObjectNotFound {
|
||||
statusCode = http.StatusNotFound
|
||||
}
|
||||
httperror.WriteError(rw, statusCode, "Unable to find a object with the specified identifier inside the database", err)
|
||||
|
||||
return
|
||||
}
|
||||
ctx := context.WithValue(req.Context(), contextKey, item)
|
||||
next.ServeHTTP(rw, req.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func FetchItem[T any](request *http.Request, contextKey string) (*T, error) {
|
||||
contextData := request.Context().Value(contextKey)
|
||||
if contextData == nil {
|
||||
return nil, errors.New("unable to find item in request context")
|
||||
}
|
||||
|
||||
item, ok := contextData.(*T)
|
||||
if !ok {
|
||||
return nil, errors.New("unable to cast context item")
|
||||
}
|
||||
|
||||
return item, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue