mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-22 10:15:35 +01:00
feat: add BeforePrepare for operation of swagger API (#14048)
Signed-off-by: He Weiwei <hweiwei@vmware.com>
This commit is contained in:
parent
19a72cf350
commit
50b40445f4
@ -15,9 +15,11 @@
|
|||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
rmiddleware "github.com/go-openapi/runtime/middleware"
|
||||||
lib_http "github.com/goharbor/harbor/src/lib/http"
|
lib_http "github.com/goharbor/harbor/src/lib/http"
|
||||||
"github.com/goharbor/harbor/src/server/middleware"
|
"github.com/goharbor/harbor/src/server/middleware"
|
||||||
"github.com/goharbor/harbor/src/server/middleware/blob"
|
"github.com/goharbor/harbor/src/server/middleware/blob"
|
||||||
@ -52,11 +54,17 @@ func New() http.Handler {
|
|||||||
api.RegisterMiddleware("DeleteArtifact", quota.RefreshForProjectMiddleware())
|
api.RegisterMiddleware("DeleteArtifact", quota.RefreshForProjectMiddleware())
|
||||||
api.RegisterMiddleware("DeleteRepository", quota.RefreshForProjectMiddleware())
|
api.RegisterMiddleware("DeleteRepository", quota.RefreshForProjectMiddleware())
|
||||||
|
|
||||||
|
api.BeforePrepare = beforePrepare
|
||||||
api.ServeError = serveError
|
api.ServeError = serveError
|
||||||
|
|
||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// function is called before the Prepare of the operation
|
||||||
|
func beforePrepare(ctx context.Context, operation string, params interface{}) rmiddleware.Responder {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Before executing operation handler, go-swagger will bind a parameters object to a request and validate the request,
|
// Before executing operation handler, go-swagger will bind a parameters object to a request and validate the request,
|
||||||
// it will return directly when bind and validate failed.
|
// it will return directly when bind and validate failed.
|
||||||
// The response format of the default ServeError implementation does not match the internal error response format.
|
// The response format of the default ServeError implementation does not match the internal error response format.
|
||||||
|
@ -90,6 +90,9 @@ type {{ pascalize .Name }}API struct {
|
|||||||
// operationMiddlewares middleware for operations
|
// operationMiddlewares middleware for operations
|
||||||
operationMiddlewares map[string]middleware.Builder
|
operationMiddlewares map[string]middleware.Builder
|
||||||
|
|
||||||
|
// BeforePrepare is called before the Prepare of the operation
|
||||||
|
BeforePrepare func(context.Context, string, interface{}) middleware.Responder
|
||||||
|
|
||||||
// BasicAuthenticator generates a runtime.Authenticator from the supplied basic auth function.
|
// BasicAuthenticator generates a runtime.Authenticator from the supplied basic auth function.
|
||||||
// It has a default implementation in the security package, however you can replace it for your particular usage.
|
// It has a default implementation in the security package, however you can replace it for your particular usage.
|
||||||
BasicAuthenticator func(security.UserPassAuthentication) runtime.Authenticator
|
BasicAuthenticator func(security.UserPassAuthentication) runtime.Authenticator
|
||||||
|
@ -151,6 +151,11 @@ func HandlerAPI(c Config) (http.Handler, *{{.Package}}.{{ pascalize .Name }}API,
|
|||||||
{{ if .Authorized -}}
|
{{ if .Authorized -}}
|
||||||
ctx = storeAuth(ctx, principal)
|
ctx = storeAuth(ctx, principal)
|
||||||
{{ end -}}
|
{{ end -}}
|
||||||
|
if api.BeforePrepare != nil {
|
||||||
|
if res := api.BeforePrepare(ctx, "{{pascalize .Name}}", ¶ms); res != nil {
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
}
|
||||||
if res := c.{{pascalize .Package}}API.Prepare(ctx, "{{pascalize .Name}}", ¶ms); res != nil {
|
if res := c.{{pascalize .Package}}API.Prepare(ctx, "{{pascalize .Name}}", ¶ms); res != nil {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user