From 50b40445f4bc23e2316a8ef5b0f6293e6801c42e Mon Sep 17 00:00:00 2001 From: He Weiwei Date: Fri, 22 Jan 2021 10:44:25 +0800 Subject: [PATCH] feat: add BeforePrepare for operation of swagger API (#14048) Signed-off-by: He Weiwei --- src/server/v2.0/handler/handler.go | 8 ++++++++ tools/swagger/templates/server/builder.gotmpl | 5 ++++- tools/swagger/templates/server/configureapi.gotmpl | 5 +++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/server/v2.0/handler/handler.go b/src/server/v2.0/handler/handler.go index 85b6ee34d..97004825b 100644 --- a/src/server/v2.0/handler/handler.go +++ b/src/server/v2.0/handler/handler.go @@ -15,9 +15,11 @@ package handler import ( + "context" "log" "net/http" + rmiddleware "github.com/go-openapi/runtime/middleware" lib_http "github.com/goharbor/harbor/src/lib/http" "github.com/goharbor/harbor/src/server/middleware" "github.com/goharbor/harbor/src/server/middleware/blob" @@ -52,11 +54,17 @@ func New() http.Handler { api.RegisterMiddleware("DeleteArtifact", quota.RefreshForProjectMiddleware()) api.RegisterMiddleware("DeleteRepository", quota.RefreshForProjectMiddleware()) + api.BeforePrepare = beforePrepare api.ServeError = serveError 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, // 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. diff --git a/tools/swagger/templates/server/builder.gotmpl b/tools/swagger/templates/server/builder.gotmpl index f901b9ff4..da437c5c0 100644 --- a/tools/swagger/templates/server/builder.gotmpl +++ b/tools/swagger/templates/server/builder.gotmpl @@ -90,6 +90,9 @@ type {{ pascalize .Name }}API struct { // operationMiddlewares middleware for operations 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. // It has a default implementation in the security package, however you can replace it for your particular usage. BasicAuthenticator func(security.UserPassAuthentication) runtime.Authenticator @@ -375,4 +378,4 @@ func ({{.ReceiverName}} *{{ pascalize .Name }}API) RegisterProducer(mediaType st // RegisterMiddleware allows you to add (or override) a middleware for operation. func ({{.ReceiverName}} *{{ pascalize .Name }}API) RegisterMiddleware(operation string, builder middleware.Builder) { {{.ReceiverName}}.operationMiddlewares[operation] = builder -} \ No newline at end of file +} diff --git a/tools/swagger/templates/server/configureapi.gotmpl b/tools/swagger/templates/server/configureapi.gotmpl index c5a74cdbb..a5200c184 100644 --- a/tools/swagger/templates/server/configureapi.gotmpl +++ b/tools/swagger/templates/server/configureapi.gotmpl @@ -151,6 +151,11 @@ func HandlerAPI(c Config) (http.Handler, *{{.Package}}.{{ pascalize .Name }}API, {{ if .Authorized -}} ctx = storeAuth(ctx, principal) {{ 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 { return res }