Merge pull request #13 from jessebluemr/master

Add --context parameter to allow the definition of an initial url path
This commit is contained in:
Pierre Zemb 2018-04-28 15:02:21 +02:00 committed by GitHub
commit c835df9fee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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 ./goStatic --help
Usage of ./goStatic: Usage of /goStatic:
-append-header HeaderName:Value -append-header HeaderName:Value
HTTP response header, specified as HeaderName:Value that should be added to all responses. 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 -default-user-basic-auth string
Define the user (default "gopher") Define the user (default "gopher")
-enable-basic-auth -enable-basic-auth

11
main.go
View file

@ -14,6 +14,7 @@ import (
var ( var (
// Def of flags // Def of flags
portPtr = flag.Int("port", 8043, "The listening port") 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") 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.") 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.") basicAuth = flag.Bool("enable-basic-auth", false, "Enable basic auth. By default, password are randomly generated. Use --set-basic-auth to set it.")
@ -49,6 +50,12 @@ func main() {
handler := http.FileServer(http.Dir(*path)) handler := http.FileServer(http.Dir(*path))
pathPrefix := "/";
if len(*context) > 0 {
pathPrefix = "/"+*context+"/"
handler = http.StripPrefix(pathPrefix, handler)
}
if *basicAuth { if *basicAuth {
log.Println("Enabling Basic Auth") log.Println("Enabling Basic Auth")
if len(*setBasicAuth) != 0 { if len(*setBasicAuth) != 0 {
@ -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)) log.Fatalln(http.ListenAndServe(port, nil))
} }