cli: Fix error when setting basepath to /

Closes https://github.com/tus/tusd/issues/325
This commit is contained in:
Marius 2019-11-10 20:18:24 +01:00
parent e138fc3e9e
commit 14aebe2985
1 changed files with 11 additions and 9 deletions

View File

@ -58,18 +58,20 @@ func Serve() {
stdout.Printf("Supported tus extensions: %s\n", handler.SupportedExtensions()) stdout.Printf("Supported tus extensions: %s\n", handler.SupportedExtensions())
// Do not display the greeting if the tusd handler will be mounted at the root if basepath == "/" {
// path. Else this would cause a "multiple registrations for /" panic. // If the basepath is set to the root path, only install the tusd handler
if basepath != "/" { // and do not show a greeting.
http.Handle("/", handler)
} else {
// If a custom basepath is defined, we show a greeting at the root path...
http.HandleFunc("/", DisplayGreeting) http.HandleFunc("/", DisplayGreeting)
}
http.Handle(basepath, http.StripPrefix(basepath, handler)) // ... and register a route with and without the trailing slash, so we can
// handle uploads for /files/ and /files, for example.
// Also register a route without the trailing slash, so we can handle uploads
// for /files/ and /files, for example.
if strings.HasSuffix(basepath, "/") {
basepathWithoutSlash := strings.TrimSuffix(basepath, "/") basepathWithoutSlash := strings.TrimSuffix(basepath, "/")
basepathWithSlash := basepathWithoutSlash + "/"
http.Handle(basepathWithSlash, http.StripPrefix(basepathWithSlash, handler))
http.Handle(basepathWithoutSlash, http.StripPrefix(basepathWithoutSlash, handler)) http.Handle(basepathWithoutSlash, http.StripPrefix(basepathWithoutSlash, handler))
} }