From 684d581b276e92f985890e08830d405963c175c4 Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Fri, 15 Mar 2024 16:59:36 +0100 Subject: [PATCH] Implement HTTP server via gorilla mux. This is mainly so we can keep supporting Go < 1.22 which is still a tiny bit too new for me to assume as default for every Go dev. However, I would love to drop this dependency once we have moved on in the future. --- cmd/storage/http.go | 17 ++++++++++------- go.mod | 1 + go.sum | 2 ++ 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/cmd/storage/http.go b/cmd/storage/http.go index e62f51a..4f3bb45 100644 --- a/cmd/storage/http.go +++ b/cmd/storage/http.go @@ -5,12 +5,15 @@ import ( "io" "net/http" "strconv" + + "github.com/gorilla/mux" ) func newHTTPServiceHandler() http.Handler { - mux := http.NewServeMux() - mux.Handle("/download/{path}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - requestedPath := r.PathValue("path") + r := mux.NewRouter() + r.Get("/download/{path}").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + requestedPath := vars["path"] if requestedPath != demoTrackURL { w.WriteHeader(http.StatusNotFound) return @@ -19,9 +22,9 @@ func newHTTPServiceHandler() http.Handler { w.WriteHeader(http.StatusOK) f := bytes.NewReader(demoTrackBytes) io.Copy(w, f) - })) - mux.Handle("/ping", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + }) + r.Get("/ping").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) - })) - return mux + }) + return r } diff --git a/go.mod b/go.mod index 41228b6..8a28ce9 100644 --- a/go.mod +++ b/go.mod @@ -48,6 +48,7 @@ require ( github.com/google/cel-go v0.20.1 // indirect github.com/google/go-containerregistry v0.19.0 // indirect github.com/google/pprof v0.0.0-20240227163752-401108e1b7e7 // indirect + github.com/gorilla/mux v1.8.1 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/jdx/go-netrc v1.0.0 // indirect github.com/klauspost/compress v1.17.7 // indirect diff --git a/go.sum b/go.sum index 8cae80b..d43c773 100644 --- a/go.sum +++ b/go.sum @@ -1507,6 +1507,8 @@ github.com/googleapis/gax-go/v2 v2.11.0/go.mod h1:DxmR61SGKkGLa2xigwuZIQpkCI2S5i github.com/googleapis/gax-go/v2 v2.12.0/go.mod h1:y+aIqrI5eb1YGMVJfuV3185Ts/D7qKpsEkdD5+I6QGU= github.com/googleapis/go-type-adapters v1.0.0/go.mod h1:zHW75FOG2aur7gAO2B+MLby+cLsWGBF62rFAi7WjWO4= github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g= +github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0/go.mod h1:hgWBS7lorOAVIJEQMi4ZsPv9hVvWI6+ch50m39Pf2Ks=