Add --context parameter to allow the definition of an initial url path

This commit is contained in:
mre 2018-03-21 09:08:30 +01:00
parent 0127bd130c
commit f6b205a2fb
2 changed files with 13 additions and 4 deletions

View file

@ -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:<port>/doc/'
-default-user-basic-auth string
Define the user (default "gopher")
-enable-basic-auth

13
main.go
View file

@ -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:<port>/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))
}