mirror of
https://github.com/halverneus/static-file-server.git
synced 2026-06-05 17:00:04 -04:00
hide directory listing
This commit is contained in:
parent
dc99a50883
commit
6ecc5555b0
27
serve.go
27
serve.go
@ -51,6 +51,8 @@ ENVIRONMENT VARIABLES
|
|||||||
The prefix to use in the URL path. If supplied, then the prefix must
|
The prefix to use in the URL path. If supplied, then the prefix must
|
||||||
start with a forward-slash and NOT end with a forward-slash. If not
|
start with a forward-slash and NOT end with a forward-slash. If not
|
||||||
supplied then no prefix is used.
|
supplied then no prefix is used.
|
||||||
|
SHOW_LISTING
|
||||||
|
This will allow you to hide the file inside the directory
|
||||||
|
|
||||||
USAGE
|
USAGE
|
||||||
FILE LAYOUT
|
FILE LAYOUT
|
||||||
@ -72,6 +74,7 @@ USAGE
|
|||||||
export HOST=my.machine
|
export HOST=my.machine
|
||||||
export PORT=80
|
export PORT=80
|
||||||
export URL_PREFIX=/my/stuff
|
export URL_PREFIX=/my/stuff
|
||||||
|
export SHOW_LISTING=true
|
||||||
static-file-server
|
static-file-server
|
||||||
Retrieve with: wget http://my.machine/my/stuff/my.file
|
Retrieve with: wget http://my.machine/my/stuff/my.file
|
||||||
|
|
||||||
@ -113,6 +116,7 @@ func main() {
|
|||||||
tlsCert := env("TLS_CERT", "")
|
tlsCert := env("TLS_CERT", "")
|
||||||
tlsKey := env("TLS_KEY", "")
|
tlsKey := env("TLS_KEY", "")
|
||||||
urlPrefix := env("URL_PREFIX", "")
|
urlPrefix := env("URL_PREFIX", "")
|
||||||
|
showListing := env("SHOW_LISTING", "true")
|
||||||
|
|
||||||
// If HTTPS is to be used, verify both TLS_* environment variables are set.
|
// If HTTPS is to be used, verify both TLS_* environment variables are set.
|
||||||
if 0 < len(tlsCert) || 0 < len(tlsKey) {
|
if 0 < len(tlsCert) || 0 < len(tlsKey) {
|
||||||
@ -138,9 +142,13 @@ func main() {
|
|||||||
var handler http.HandlerFunc
|
var handler http.HandlerFunc
|
||||||
if 0 == len(urlPrefix) {
|
if 0 == len(urlPrefix) {
|
||||||
handler = basicHandler(folder)
|
handler = basicHandler(folder)
|
||||||
|
} else {
|
||||||
|
if showListing == "false" {
|
||||||
|
handler = prefixHandlerWithoutListingFilesInDirectory(folder, urlPrefix)
|
||||||
} else {
|
} else {
|
||||||
handler = prefixHandler(folder, urlPrefix)
|
handler = prefixHandler(folder, urlPrefix)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
http.HandleFunc("/", handler)
|
http.HandleFunc("/", handler)
|
||||||
|
|
||||||
// Serve files over HTTP or HTTPS based on paths to TLS files being provided.
|
// Serve files over HTTP or HTTPS based on paths to TLS files being provided.
|
||||||
@ -158,6 +166,24 @@ func basicHandler(folder string) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// prefixHandler removes the URL path prefix before serving files from the
|
||||||
|
// folder passed.
|
||||||
|
func prefixHandlerWithoutListingFilesInDirectory(folder, urlPrefix string) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !strings.HasPrefix(r.URL.Path, urlPrefix) {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// dont list file in directory
|
||||||
|
if strings.HasSuffix(r.URL.Path, "/") {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
http.ServeFile(w, r, folder+strings.TrimPrefix(r.URL.Path, urlPrefix))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// prefixHandler removes the URL path prefix before serving files from the
|
// prefixHandler removes the URL path prefix before serving files from the
|
||||||
// folder passed.
|
// folder passed.
|
||||||
func prefixHandler(folder, urlPrefix string) http.HandlerFunc {
|
func prefixHandler(folder, urlPrefix string) http.HandlerFunc {
|
||||||
@ -166,6 +192,7 @@ func prefixHandler(folder, urlPrefix string) http.HandlerFunc {
|
|||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
http.ServeFile(w, r, folder+strings.TrimPrefix(r.URL.Path, urlPrefix))
|
http.ServeFile(w, r, folder+strings.TrimPrefix(r.URL.Path, urlPrefix))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user