0
Fork 0
mirror of https://github.com/project-zot/zot.git synced 2025-01-20 22:52:51 -05:00

build: fix mgmt and userprefs when building them separately (#1601)

move queryHasParams in common package
fixes building mgmt and userprefs separately

Signed-off-by: Petu Eusebiu <peusebiu@cisco.com>
This commit is contained in:
peusebiu 2023-07-10 19:40:14 +03:00 committed by GitHub
parent 94ba538e69
commit aaf03c75fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 18 deletions

View file

@ -2,6 +2,7 @@ package common
import ( import (
"net/http" "net/http"
"net/url"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -82,6 +83,16 @@ func WriteData(w http.ResponseWriter, status int, mediaType string, data []byte)
_, _ = w.Write(data) _, _ = w.Write(data)
} }
func QueryHasParams(values url.Values, params []string) bool {
for _, param := range params {
if !values.Has(param) {
return false
}
}
return true
}
/* /*
GetAuthUserFromRequestSession returns identity GetAuthUserFromRequestSession returns identity
and auth status if on the request's cookie session is a logged in user. and auth status if on the request's cookie session is a logged in user.

View file

@ -99,7 +99,7 @@ func (mgmt *mgmt) handler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var resource string var resource string
if queryHasParams(r.URL.Query(), []string{"resource"}) { if zcommon.QueryHasParams(r.URL.Query(), []string{"resource"}) {
resource = r.URL.Query().Get("resource") resource = r.URL.Query().Get("resource")
} else { } else {
resource = ConfigResource // default value of "resource" query param resource = ConfigResource // default value of "resource" query param
@ -181,7 +181,7 @@ func (mgmt *mgmt) HandleGetConfig(w http.ResponseWriter, r *http.Request) {
// @Failure 400 {string} string "bad request". // @Failure 400 {string} string "bad request".
// @Failure 500 {string} string "internal server error". // @Failure 500 {string} string "internal server error".
func HandleCertificatesAndPublicKeysUploads(response http.ResponseWriter, request *http.Request) { func HandleCertificatesAndPublicKeysUploads(response http.ResponseWriter, request *http.Request) {
if !queryHasParams(request.URL.Query(), []string{"tool"}) { if !zcommon.QueryHasParams(request.URL.Query(), []string{"tool"}) {
response.WriteHeader(http.StatusBadRequest) response.WriteHeader(http.StatusBadRequest)
return return
@ -207,13 +207,13 @@ func HandleCertificatesAndPublicKeysUploads(response http.ResponseWriter, reques
case signatures.NotationSignature: case signatures.NotationSignature:
var truststoreType string var truststoreType string
if !queryHasParams(request.URL.Query(), []string{"truststoreName"}) { if !zcommon.QueryHasParams(request.URL.Query(), []string{"truststoreName"}) {
response.WriteHeader(http.StatusBadRequest) response.WriteHeader(http.StatusBadRequest)
return return
} }
if queryHasParams(request.URL.Query(), []string{"truststoreType"}) { if zcommon.QueryHasParams(request.URL.Query(), []string{"truststoreType"}) {
truststoreType = request.URL.Query().Get("truststoreType") truststoreType = request.URL.Query().Get("truststoreType")
} else { } else {
truststoreType = "ca" // default value of "truststoreType" query param truststoreType = "ca" // default value of "truststoreType" query param

View file

@ -6,7 +6,6 @@ package extensions
import ( import (
"errors" "errors"
"net/http" "net/http"
"net/url"
"github.com/gorilla/mux" "github.com/gorilla/mux"
@ -59,7 +58,7 @@ func SetupUserPreferencesRoutes(config *config.Config, router *mux.Router, store
// @Failure 400 {string} string "bad request". // @Failure 400 {string} string "bad request".
func HandleUserPrefs(repoDB repodb.RepoDB, log log.Logger) func(w http.ResponseWriter, r *http.Request) { func HandleUserPrefs(repoDB repodb.RepoDB, log log.Logger) func(w http.ResponseWriter, r *http.Request) {
return func(rsp http.ResponseWriter, req *http.Request) { return func(rsp http.ResponseWriter, req *http.Request) {
if !queryHasParams(req.URL.Query(), []string{"action"}) { if !zcommon.QueryHasParams(req.URL.Query(), []string{"action"}) {
rsp.WriteHeader(http.StatusBadRequest) rsp.WriteHeader(http.StatusBadRequest)
return return
@ -85,7 +84,7 @@ func HandleUserPrefs(repoDB repodb.RepoDB, log log.Logger) func(w http.ResponseW
} }
func PutStar(rsp http.ResponseWriter, req *http.Request, repoDB repodb.RepoDB, log log.Logger) { func PutStar(rsp http.ResponseWriter, req *http.Request, repoDB repodb.RepoDB, log log.Logger) {
if !queryHasParams(req.URL.Query(), []string{"repo"}) { if !zcommon.QueryHasParams(req.URL.Query(), []string{"repo"}) {
rsp.WriteHeader(http.StatusBadRequest) rsp.WriteHeader(http.StatusBadRequest)
return return
@ -120,7 +119,7 @@ func PutStar(rsp http.ResponseWriter, req *http.Request, repoDB repodb.RepoDB, l
} }
func PutBookmark(rsp http.ResponseWriter, req *http.Request, repoDB repodb.RepoDB, log log.Logger) { func PutBookmark(rsp http.ResponseWriter, req *http.Request, repoDB repodb.RepoDB, log log.Logger) {
if !queryHasParams(req.URL.Query(), []string{"repo"}) { if !zcommon.QueryHasParams(req.URL.Query(), []string{"repo"}) {
rsp.WriteHeader(http.StatusBadRequest) rsp.WriteHeader(http.StatusBadRequest)
return return
@ -153,13 +152,3 @@ func PutBookmark(rsp http.ResponseWriter, req *http.Request, repoDB repodb.RepoD
rsp.WriteHeader(http.StatusOK) rsp.WriteHeader(http.StatusOK)
} }
func queryHasParams(values url.Values, params []string) bool {
for _, param := range params {
if !values.Has(param) {
return false
}
}
return true
}