From d1a6e2efb7ca033bd808cc2a61eb8cdc3e0bd7c0 Mon Sep 17 00:00:00 2001 From: Lars Karlitski Date: Sun, 17 Nov 2019 10:28:23 +0000 Subject: [PATCH] weldr: clean up source/info route Split the error case (no sources specified) into its own function, so that we can use `source/info/:sources` (note the colon) to get the list of sources without the leading `/`. This gets rid of two special cases which made the previous implementation hard to parse. --- internal/weldr/api.go | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/internal/weldr/api.go b/internal/weldr/api.go index f56a308d4..6e64986cb 100644 --- a/internal/weldr/api.go +++ b/internal/weldr/api.go @@ -49,7 +49,8 @@ func New(rpmmd rpmmd.RPMMD, repo rpmmd.RepoConfig, logger *log.Logger, store *st api.router.GET("/api/status", api.statusHandler) api.router.GET("/api/v0/projects/source/list", api.sourceListHandler) - api.router.GET("/api/v0/projects/source/info/*sources", api.sourceInfoHandler) + api.router.GET("/api/v0/projects/source/info/", api.sourceEmptyInfoHandler) + api.router.GET("/api/v0/projects/source/info/:sources", api.sourceInfoHandler) api.router.POST("/api/v0/projects/source/new", api.sourceNewHandler) api.router.DELETE("/api/v0/projects/source/delete/*source", api.sourceDeleteHandler) @@ -177,6 +178,15 @@ func (api *API) sourceListHandler(writer http.ResponseWriter, request *http.Requ }) } +func (api *API) sourceEmptyInfoHandler(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { + errors := responseError{ + Code: http.StatusNotFound, + ID: "HTTPError", + Msg: "Not Found", + } + statusResponseError(writer, http.StatusNotFound, errors) +} + func (api *API) sourceInfoHandler(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { // weldr uses a slightly different format than dnf to store repository // configuration @@ -187,23 +197,9 @@ func (api *API) sourceInfoHandler(writer http.ResponseWriter, request *http.Requ names := strings.Split(params.ByName("sources"), ",") - if names[0] == "/" { - errors := responseError{ - Code: http.StatusNotFound, - ID: "HTTPError", - Msg: "Not Found", - } - statusResponseError(writer, http.StatusNotFound, errors) - return - } - sources := map[string]store.SourceConfig{} errors := []responseError{} - for i, name := range names { - // remove leading / from first name - if i == 0 { - name = name[1:] - } + for _, name := range names { // if name is "*" we want all sources from the store and the base repo if name == "*" { sources = api.store.GetAllSources()