|
|
@ -39,6 +39,12 @@ ENVIRONMENT VARIABLES |
|
|
|
to a client without regard for the hostname. |
|
|
|
to a client without regard for the hostname. |
|
|
|
PORT |
|
|
|
PORT |
|
|
|
The port used for binding. If not supplied, defaults to port '8080'. |
|
|
|
The port used for binding. If not supplied, defaults to port '8080'. |
|
|
|
|
|
|
|
SHOW_LISTING |
|
|
|
|
|
|
|
Automatically serve the index file for the directory if requested. For |
|
|
|
|
|
|
|
example, if the client requests 'http://127.0.0.1/' the 'index.html'
|
|
|
|
|
|
|
|
file in the root of the directory being served is returned. If the value |
|
|
|
|
|
|
|
is set to 'false', the same request will return a 'NOT FOUND'. Default |
|
|
|
|
|
|
|
value is 'true'. |
|
|
|
TLS_CERT |
|
|
|
TLS_CERT |
|
|
|
Path to the TLS certificate file to serve files using HTTPS. If supplied |
|
|
|
Path to the TLS certificate file to serve files using HTTPS. If supplied |
|
|
|
then TLS_KEY must also be supplied. If not supplied, contents will be |
|
|
|
then TLS_KEY must also be supplied. If not supplied, contents will be |
|
|
@ -55,6 +61,7 @@ ENVIRONMENT VARIABLES |
|
|
|
USAGE |
|
|
|
USAGE |
|
|
|
FILE LAYOUT |
|
|
|
FILE LAYOUT |
|
|
|
/var/www/sub/my.file |
|
|
|
/var/www/sub/my.file |
|
|
|
|
|
|
|
/var/www/index.html |
|
|
|
|
|
|
|
|
|
|
|
COMMAND |
|
|
|
COMMAND |
|
|
|
export FOLDER=/var/www/sub |
|
|
|
export FOLDER=/var/www/sub |
|
|
@ -87,6 +94,18 @@ USAGE |
|
|
|
export TLS_KEY=/etc/server/my.machine.key |
|
|
|
export TLS_KEY=/etc/server/my.machine.key |
|
|
|
static-file-server |
|
|
|
static-file-server |
|
|
|
Retrieve with: wget https://my.machine/my.file
|
|
|
|
Retrieve with: wget https://my.machine/my.file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export FOLDER=/var/www |
|
|
|
|
|
|
|
export PORT=80 |
|
|
|
|
|
|
|
export SHOW_LISTING=true # Default behavior |
|
|
|
|
|
|
|
static-file-server |
|
|
|
|
|
|
|
Retrieve 'index.html' with: wget http://my.machine/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export FOLDER=/var/www |
|
|
|
|
|
|
|
export PORT=80 |
|
|
|
|
|
|
|
export SHOW_LISTING=false |
|
|
|
|
|
|
|
static-file-server |
|
|
|
|
|
|
|
Returns 'NOT FOUND': wget http://my.machine/
|
|
|
|
` |
|
|
|
` |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
@ -110,6 +129,7 @@ func main() { |
|
|
|
folder := env("FOLDER", "/web") + "/" |
|
|
|
folder := env("FOLDER", "/web") + "/" |
|
|
|
host := env("HOST", "") |
|
|
|
host := env("HOST", "") |
|
|
|
port := env("PORT", "8080") |
|
|
|
port := env("PORT", "8080") |
|
|
|
|
|
|
|
showListing := envAsBool("SHOW_LISTING", true) |
|
|
|
tlsCert := env("TLS_CERT", "") |
|
|
|
tlsCert := env("TLS_CERT", "") |
|
|
|
tlsKey := env("TLS_KEY", "") |
|
|
|
tlsKey := env("TLS_KEY", "") |
|
|
|
urlPrefix := env("URL_PREFIX", "") |
|
|
|
urlPrefix := env("URL_PREFIX", "") |
|
|
@ -137,9 +157,9 @@ func main() { |
|
|
|
// Choose and set the appropriate, optimized static file serving function.
|
|
|
|
// Choose and set the appropriate, optimized static file serving function.
|
|
|
|
var handler http.HandlerFunc |
|
|
|
var handler http.HandlerFunc |
|
|
|
if 0 == len(urlPrefix) { |
|
|
|
if 0 == len(urlPrefix) { |
|
|
|
handler = basicHandler(folder) |
|
|
|
handler = handleListing(showListing, basicHandler(folder)) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
handler = prefixHandler(folder, urlPrefix) |
|
|
|
handler = handleListing(showListing, prefixHandler(folder, urlPrefix)) |
|
|
|
} |
|
|
|
} |
|
|
|
http.HandleFunc("/", handler) |
|
|
|
http.HandleFunc("/", handler) |
|
|
|
|
|
|
|
|
|
|
@ -151,6 +171,19 @@ func main() { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// handleListing wraps an HTTP request. In the event of a folder root request,
|
|
|
|
|
|
|
|
// setting 'show' to false will automatically return 'NOT FOUND' whereas true
|
|
|
|
|
|
|
|
// will attempt to retrieve the index file of that directory.
|
|
|
|
|
|
|
|
func handleListing(show bool, serve http.HandlerFunc) http.HandlerFunc { |
|
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
|
|
if show || strings.HasSuffix(r.URL.Path, "/") { |
|
|
|
|
|
|
|
http.NotFound(w, r) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
serve(w, r) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// basicHandler serves files from the folder passed.
|
|
|
|
// basicHandler serves files from the folder passed.
|
|
|
|
func basicHandler(folder string) http.HandlerFunc { |
|
|
|
func basicHandler(folder string) http.HandlerFunc { |
|
|
|
return func(w http.ResponseWriter, r *http.Request) { |
|
|
|
return func(w http.ResponseWriter, r *http.Request) { |
|
|
@ -178,3 +211,35 @@ func env(key, fallback string) string { |
|
|
|
} |
|
|
|
} |
|
|
|
return fallback |
|
|
|
return fallback |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// strAsBool converts the intent of the passed value into a boolean
|
|
|
|
|
|
|
|
// representation.
|
|
|
|
|
|
|
|
func strAsBool(value string) (result bool, err error) { |
|
|
|
|
|
|
|
lvalue := strings.ToLower(value) |
|
|
|
|
|
|
|
switch lvalue { |
|
|
|
|
|
|
|
case "0", "false", "f", "no", "n": |
|
|
|
|
|
|
|
result = false |
|
|
|
|
|
|
|
case "1", "true", "t", "yes", "y": |
|
|
|
|
|
|
|
result = true |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
result = false |
|
|
|
|
|
|
|
msg := "Unknown conversion from string to bool for value '%s'" |
|
|
|
|
|
|
|
err = fmt.Errorf(msg, value) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// envAsBool returns the value for an environment variable or, if not set, a
|
|
|
|
|
|
|
|
// fallback value as a boolean.
|
|
|
|
|
|
|
|
func envAsBool(key string, fallback bool) bool { |
|
|
|
|
|
|
|
value := env(key, fmt.Sprintf("%t", fallback)) |
|
|
|
|
|
|
|
result, err := strAsBool(value) |
|
|
|
|
|
|
|
if nil != err { |
|
|
|
|
|
|
|
log.Printf( |
|
|
|
|
|
|
|
"Invalid value for '%s': %v\nUsing fallback: %t", |
|
|
|
|
|
|
|
key, err, fallback, |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
return fallback |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return result |
|
|
|
|
|
|
|
} |
|
|
|