1 // Copyright 2019 Nokia
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
27 "github.com/gorilla/mux"
28 "github.com/ncw/swift"
31 func Index(c *swift.Connection, config config.EnvConfig) http.HandlerFunc {
32 return func(w http.ResponseWriter, r *http.Request) {
33 fmt.Fprintln(w, "<html><body>")
34 fmt.Fprintln(w, "Hello World!<br />")
35 objects, err := c.ObjectNames(config.Container, nil)
37 log.Println("Index error:", err)
38 fmt.Fprintln(w, "Index error:", err)
40 for _, v := range objects {
41 fmt.Fprintf(w, "<a href=\"%s\">%s</a><br />", v, v)
44 fmt.Fprintln(w, "</body></html>")
48 func ObjectGet(c *swift.Connection, config config.EnvConfig) http.HandlerFunc {
49 return func(w http.ResponseWriter, r *http.Request) {
51 objectName := vars["id"]
53 object, _, err := c.ObjectOpen(config.Container, objectName, false, nil)
57 case "Object Not Found":
58 w.WriteHeader(http.StatusNotFound)
60 w.WriteHeader(http.StatusInternalServerError)
62 log.Println("ObjectGet error:", err, ", requested object:", objectName)
63 fmt.Fprintln(w, "<html><body>")
64 fmt.Fprintln(w, "ObjectGet error:", err)
65 fmt.Fprintln(w, "</body></html>")
73 func ObjectPut(c *swift.Connection, config config.EnvConfig) http.HandlerFunc {
74 return func(w http.ResponseWriter, r *http.Request) {
76 objectName := vars["id"]
78 _, err := c.ObjectPut(config.Container, objectName, r.Body, false, "", "", nil)
80 log.Println("ObjectPut error:", err.Error())
81 w.WriteHeader(http.StatusInternalServerError)
82 fmt.Fprintln(w, "<html><body>")
83 fmt.Fprintln(w, "ObjectPut error:", err)
84 fmt.Fprintln(w, "</body></html>")
87 if strings.HasPrefix(objectName, config.IndexPath) && strings.HasSuffix(objectName, "tgz") {
88 log.Println("Regenerating index for:", config.IndexPath)
89 if err := repo.Index(c, config.Container, config.RepoUrl+":"+config.ListenOnPort, config.IndexPath, "/index.yaml"); err != nil {
90 log.Println("ObjectPut error:", err.Error())
91 fmt.Fprintln(w, "<html><body>")
92 fmt.Fprintln(w, "Index file generation error:", err)
93 fmt.Fprintln(w, "</body></html>")
99 func ObjectDelete(c *swift.Connection, config config.EnvConfig) http.HandlerFunc {
100 return func(w http.ResponseWriter, r *http.Request) {
102 objectName := vars["id"]
104 err := c.ObjectDelete(config.Container, objectName)
106 log.Println("ObjectDelete error:", err.Error())
107 fmt.Fprintln(w, "<html><body>")
108 fmt.Fprintln(w, "ObjectDelete error:", err)
109 fmt.Fprintln(w, "</body></html>")
112 if strings.HasPrefix(objectName, config.IndexPath) && strings.HasSuffix(objectName, "tgz") {
113 log.Println("Regenerating index for:", config.IndexPath)
114 if err := repo.Index(c, config.Container, config.RepoUrl+":"+config.ListenOnPort, config.IndexPath, "/index.yaml"); err != nil {
115 log.Println("ObjectPut error:", err.Error())
116 fmt.Fprintln(w, "<html><body>")
117 fmt.Fprintln(w, "Index file generation error:", err)
118 fmt.Fprintln(w, "</body></html>")