From f6b205a2fb2ee46d8b551f8e145adc5310f945c8 Mon Sep 17 00:00:00 2001 From: mre Date: Wed, 21 Mar 2018 09:08:30 +0100 Subject: [PATCH] Add --context parameter to allow the definition of an initial url path --- README.md | 4 +++- main.go | 13 ++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index da3624f..82d0dbf 100644 --- a/README.md +++ b/README.md @@ -41,9 +41,11 @@ docker run -d -p 80:8043 -v path/to/website:/srv/http --name goStatic pierrezemb ``` ./goStatic --help -Usage of ./goStatic: +Usage of /goStatic: -append-header HeaderName:Value HTTP response header, specified as HeaderName:Value that should be added to all responses. + -context string + The 'context' path on which files are served, e.g. 'doc' will serve the files at 'http://localhost:/doc/' -default-user-basic-auth string Define the user (default "gopher") -enable-basic-auth diff --git a/main.go b/main.go index f3322bc..90cfcc8 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ import ( var ( // Def of flags portPtr = flag.Int("port", 8043, "The listening port") + context = flag.String("context", "", "The 'context' path on which files are served, e.g. 'doc' will serve the files at 'http://localhost:/doc/'") path = flag.String("path", "/srv/http", "The path for the static files") headerFlag = flag.String("append-header", "", "HTTP response header, specified as `HeaderName:Value` that should be added to all responses.") basicAuth = flag.Bool("enable-basic-auth", false, "Enable basic auth. By default, password are randomly generated. Use --set-basic-auth to set it.") @@ -46,8 +47,14 @@ func main() { } port := ":" + strconv.FormatInt(int64(*portPtr), 10) - + handler := http.FileServer(http.Dir(*path)) + + pathPrefix := "/"; + if len(*context) > 0 { + pathPrefix = "/"+*context+"/" + handler = http.StripPrefix(pathPrefix, handler) + } if *basicAuth { log.Println("Enabling Basic Auth") @@ -73,8 +80,8 @@ func main() { } } - http.Handle("/", handler) + http.Handle(pathPrefix, handler) - log.Printf("Listening at 0.0.0.0%v...", port) + log.Printf("Listening at 0.0.0.0%v %v...", port, pathPrefix) log.Fatalln(http.ListenAndServe(port, nil)) }