X-Git-Url: https://gerrit.akraino.org/r/gitweb?a=blobdiff_plain;f=src%2Fchart-repo-handler%2Fpkg%2Fapi%2Fhandlers.go;fp=src%2Fchart-repo-handler%2Fpkg%2Fapi%2Fhandlers.go;h=1791bbbfe600b55017a7cf33558362e1ab0a6d42;hb=cf9e279cdad75e3bacaa71fb0323f1284288b73a;hp=0000000000000000000000000000000000000000;hpb=fd08a43597cef9022d3c935dffcd81afceb04267;p=ta%2Fcaas-helm.git diff --git a/src/chart-repo-handler/pkg/api/handlers.go b/src/chart-repo-handler/pkg/api/handlers.go new file mode 100644 index 0000000..1791bbb --- /dev/null +++ b/src/chart-repo-handler/pkg/api/handlers.go @@ -0,0 +1,122 @@ +// Copyright 2019 Nokia +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package api + +import ( + "fmt" + "io" + "log" + "net/http" + "strings" + + "pkg/config" + "pkg/repo" + + "github.com/gorilla/mux" + "github.com/ncw/swift" +) + +func Index(c *swift.Connection, config config.EnvConfig) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, "") + fmt.Fprintln(w, "Hello World!
") + objects, err := c.ObjectNames(config.Container, nil) + if err != nil { + log.Println("Index error:", err) + fmt.Fprintln(w, "Index error:", err) + } else { + for _, v := range objects { + fmt.Fprintf(w, "%s
", v, v) + } + } + fmt.Fprintln(w, "") + } +} + +func ObjectGet(c *swift.Connection, config config.EnvConfig) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + objectName := vars["id"] + + object, _, err := c.ObjectOpen(config.Container, objectName, false, nil) + + if err != nil { + switch err.Error() { + case "Object Not Found": + w.WriteHeader(http.StatusNotFound) + default: + w.WriteHeader(http.StatusInternalServerError) + } + log.Println("ObjectGet error:", err, ", requested object:", objectName) + fmt.Fprintln(w, "") + fmt.Fprintln(w, "ObjectGet error:", err) + fmt.Fprintln(w, "") + return + } + io.Copy(w, object) + object.Close() + } +} + +func ObjectPut(c *swift.Connection, config config.EnvConfig) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + objectName := vars["id"] + + _, err := c.ObjectPut(config.Container, objectName, r.Body, false, "", "", nil) + if err != nil { + log.Println("ObjectPut error:", err.Error()) + w.WriteHeader(http.StatusInternalServerError) + fmt.Fprintln(w, "") + fmt.Fprintln(w, "ObjectPut error:", err) + fmt.Fprintln(w, "") + return + } + if strings.HasPrefix(objectName, config.IndexPath) && strings.HasSuffix(objectName, "tgz") { + log.Println("Regenerating index for:", config.IndexPath) + if err := repo.Index(c, config.Container, config.RepoUrl+":"+config.ListenOnPort, config.IndexPath, "/index.yaml"); err != nil { + log.Println("ObjectPut error:", err.Error()) + fmt.Fprintln(w, "") + fmt.Fprintln(w, "Index file generation error:", err) + fmt.Fprintln(w, "") + } + } + } +} + +func ObjectDelete(c *swift.Connection, config config.EnvConfig) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + objectName := vars["id"] + + err := c.ObjectDelete(config.Container, objectName) + if err != nil { + log.Println("ObjectDelete error:", err.Error()) + fmt.Fprintln(w, "") + fmt.Fprintln(w, "ObjectDelete error:", err) + fmt.Fprintln(w, "") + return + } + if strings.HasPrefix(objectName, config.IndexPath) && strings.HasSuffix(objectName, "tgz") { + log.Println("Regenerating index for:", config.IndexPath) + if err := repo.Index(c, config.Container, config.RepoUrl+":"+config.ListenOnPort, config.IndexPath, "/index.yaml"); err != nil { + log.Println("ObjectPut error:", err.Error()) + fmt.Fprintln(w, "") + fmt.Fprintln(w, "Index file generation error:", err) + fmt.Fprintln(w, "") + } + } + } +}