Commit 19ea4701 authored by Bruce Flynn's avatar Bruce Flynn
Browse files

use gorilla

parent 192c5a45
Pipeline #4100 passed with stage
in 1 minute and 20 seconds
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
name = "github.com/gorilla/context"
packages = ["."]
revision = "08b5f424b9271eedf6f9f0ce86cb9396ed337a42"
version = "v1.1.1"
[[projects]]
name = "github.com/gorilla/handlers"
packages = ["."]
revision = "7e0847f9db758cdebd26c149d0ae9d5d0b9c98ce"
version = "v1.4.0"
[[projects]]
name = "github.com/gorilla/mux"
packages = ["."]
revision = "e3702bed27f0d39777b0b37b664b6280e8ef8fbf"
version = "v1.6.2"
[[projects]]
name = "github.com/pkg/errors"
packages = ["."]
......@@ -16,6 +34,6 @@
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "6a5cb0d32267ee88743a8972ec6d5c53e95e1e90a4711815b803c569c22340b6"
inputs-digest = "f6087c0b85729f6281d53f35c3d177780a29d49c74ea6dfde0a7ad376af7e256"
solver-name = "gps-cdcl"
solver-version = 1
......@@ -36,3 +36,7 @@
[prune]
go-tests = true
unused-packages = true
[[constraint]]
name = "github.com/gorilla/mux"
version = "1.6.2"
......@@ -9,15 +9,12 @@ import (
"os/exec"
"path"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/spf13/pflag"
)
func httpError(w http.ResponseWriter, r *http.Request, msg string, code int) {
log.Printf("HttpError[%d %s]: %s", code, msg, r.URL)
http.Error(w, msg, code)
}
func httpFail(w http.ResponseWriter, r *http.Request, msg string, err error) {
log.Printf("InternalServerError: %s: %s: %s", msg, err, r.URL)
http.Error(w, msg, http.StatusInternalServerError)
......@@ -38,12 +35,12 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
st, err := os.Stat(p)
// got an error and the file exists... something interesting perhaps?
if err != nil && !os.IsNotExist(err) {
httpError(w, r, "InternalServerError", http.StatusInternalServerError)
http.Error(w, "InternalServerError", http.StatusInternalServerError)
return
}
// got a stat result, file must exist
if st != nil {
httpError(w, r, "File exists", http.StatusForbidden)
http.Error(w, "File exists", http.StatusForbidden)
return
}
......@@ -80,6 +77,20 @@ helm repo add <name> %s
</pre>
</code>
</p>
<h3>API</h3>
<dl>
<dt>GET /index.yaml</dt>
<dd>Get chart index file used by Helm</dd>
<dt>GET /<version>-<version>.tgz</dt>
<dd>Get a Heml chart package</dd>
<dt>GET /status</dt>
<dd>Health check that simply returns <code>OK!</code></dd>
</dl>
</div>
`, url)))
}
......@@ -88,7 +99,7 @@ func handleGetFile(w http.ResponseWriter, r *http.Request) {
p := path.Join(dir, r.URL.Path)
st, err := os.Stat(p)
if err != nil && os.IsNotExist(err) || st != nil && st.IsDir() {
httpError(w, r, "Not Found", http.StatusNotFound)
http.Error(w, "Not Found", http.StatusNotFound)
return
}
f, err := os.Open(p)
......@@ -102,21 +113,6 @@ func handleGetFile(w http.ResponseWriter, r *http.Request) {
}
}
func handle(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "PUT":
handleUpload(w, r)
case "GET":
if r.URL.Path == "/" {
handleGetHome(w, r)
} else {
handleGetFile(w, r)
}
default:
httpError(w, r, "Bad Request", http.StatusBadRequest)
}
}
func handleStatus(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK!\n"))
......@@ -173,9 +169,15 @@ Options can also be specified as environment variables $CHART_SVR_<name>
dir = path.Join(p, dir)
}
http.HandleFunc("/status", handleStatus)
http.HandleFunc("/", handle)
r := mux.NewRouter()
r.HandleFunc("/status", handleStatus).Methods("GET")
// Limit to only index.yaml and chart (.tgz) files
r.HandleFunc(`/{filename:(?:index.yaml|.*-.*.tgz)}`, handleGetFile).Methods("GET")
r.HandleFunc(`/{chart:.*-.*\.tgz}`, handleUpload).Methods("PUT")
r.HandleFunc("/", handleGetHome)
h := handlers.CombinedLoggingHandler(os.Stdout, handlers.CORS()(r))
log.Printf("serving charts from %s on %s", dir, host)
log.Fatal(http.ListenAndServe(host, nil))
log.Fatal(http.ListenAndServe(host, h))
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment