diff --git a/cmd/megauploader/main.go b/cmd/megauploader/main.go index 4456ed5..aa2e29e 100644 --- a/cmd/megauploader/main.go +++ b/cmd/megauploader/main.go @@ -3,7 +3,6 @@ package main import ( "flag" "fmt" - "net/http" "os" "time" @@ -36,6 +35,6 @@ func main() { } mu := megauploader.NewMegaUploader(cfg) - mu.SetupRoutes() - graceful.Run(*addr, 15*time.Second, http.DefaultServeMux) + mux := mu.SetupRoutes() + graceful.Run(*addr, 15*time.Second, mux) } diff --git a/http.go b/http.go index 19beccd..6d7adc0 100644 --- a/http.go +++ b/http.go @@ -16,6 +16,8 @@ import ( rice "github.com/GeertJohan/go.rice" "github.com/c2h5oh/datasize" + "github.com/gorilla/handlers" + "github.com/gorilla/mux" "github.com/unrolled/secure" ) @@ -58,27 +60,31 @@ func (mu *MegaUploader) confAcquire(inner http.Handler) http.Handler { return http.HandlerFunc(f) } -func (mu *MegaUploader) privateMiddleware(inner func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) { - return mu.secureMW.Handler(mu.confAcquire(requireUserMiddleware(inner))).ServeHTTP +func (mu *MegaUploader) privateMiddleware(inner func(w http.ResponseWriter, r *http.Request)) http.Handler { + return mu.secureMW.Handler(mu.confAcquire(requireUserMiddleware(inner))) } -func (mu *MegaUploader) publicMiddleware(inner func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) { - return mu.secureMW.Handler(requireUserMiddleware(inner)).ServeHTTP +func (mu *MegaUploader) publicMiddleware(inner func(w http.ResponseWriter, r *http.Request)) http.Handler { + return mu.secureMW.Handler(requireUserMiddleware(inner)) } // SetupRoutes adds API routes -func (mu *MegaUploader) SetupRoutes() { - +func (mu *MegaUploader) SetupRoutes() http.Handler { prefix := strings.TrimRight(mu.Conf.Global.RoutePrefix, "/") + root := mux.NewRouter() + r := root.PathPrefix(prefix).Subrouter() - http.HandleFunc(prefix+"/", mu.privateMiddleware(mu.home)) - http.HandleFunc(prefix+"/upload/", mu.privateMiddleware(mu.uploadUI)) + r.Handle("/", mu.privateMiddleware(mu.home)) + r.Handle("/upload/{share}", mu.privateMiddleware(mu.uploadUI)) static := rice.MustFindBox("res/static") - http.HandleFunc(prefix+"/static/", mu.publicMiddleware( + r.PathPrefix("/static/").Handler(mu.publicMiddleware( http.StripPrefix(prefix+"/static/", http.FileServer(static.HTTPBox())).ServeHTTP, )) - http.HandleFunc(prefix+"/api/share", mu.privateMiddleware(mu.listShares)) - http.HandleFunc(prefix+"/api/share/", mu.privateMiddleware(mu.getShare)) - http.HandleFunc(prefix+"/api/upload/", mu.privateMiddleware(mu.upload)) + api := r.PathPrefix("/api/").Subrouter() + api.Handle("/share", mu.privateMiddleware(mu.listShares)) + api.Handle("/share/{share}", mu.privateMiddleware(mu.getShare)) + api.Handle("/upload/{share}", mu.privateMiddleware(mu.upload)).Methods(http.MethodPost) + + return handlers.CombinedLoggingHandler(os.Stdout, r) } func getUser(r *http.Request) (string, error) { @@ -114,7 +120,7 @@ func (mu *MegaUploader) listShares(w http.ResponseWriter, r *http.Request) { } func (mu *MegaUploader) getShare(w http.ResponseWriter, r *http.Request) { - sharename := r.URL.Path[strings.LastIndexByte(r.URL.Path, '/')+1:] + sharename := mux.Vars(r)["share"] share, err := mu.Conf.GetShare(sharename) if err != nil { http.Error(w, fmt.Sprintf("Share '%s' not found: %s", sharename, err), http.StatusNotFound) @@ -137,7 +143,7 @@ func (mu *MegaUploader) getShare(w http.ResponseWriter, r *http.Request) { // on success, redirect to view URL func (mu *MegaUploader) upload(w http.ResponseWriter, r *http.Request) { user, _ := getUser(r) // user is set: checked by middleware - sharename := r.URL.Path[strings.LastIndexByte(r.URL.Path, '/')+1:] + sharename := mux.Vars(r)["share"] share, err := mu.Conf.GetAuthShare(sharename, user) if err != nil { diff --git a/httpui.go b/httpui.go index 3eae770..3f07ee8 100644 --- a/httpui.go +++ b/httpui.go @@ -10,6 +10,7 @@ import ( "strings" rice "github.com/GeertJohan/go.rice" + "github.com/gorilla/mux" ) //go:generate rice embed-go @@ -55,7 +56,7 @@ func (mu *MegaUploader) home(w http.ResponseWriter, r *http.Request) { func (mu *MegaUploader) uploadUI(w http.ResponseWriter, r *http.Request) { user, _ := getUser(r) // user is set: checked by middleware - sharename := r.URL.Path[strings.LastIndexByte(r.URL.Path, '/')+1:] + sharename := mux.Vars(r)["share"] share, err := mu.Conf.GetAuthShare(sharename, user) if err != nil {