Skip to content

Commit

Permalink
add an option for directory listings output format
Browse files Browse the repository at this point in the history
  • Loading branch information
pottava committed Jul 26, 2017
1 parent 5754185 commit 503ae6d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ services:
- AWS_S3_KEY_PREFIX
- INDEX_DOCUMENT
- DIRECTORY_LISTINGS
- DIRECTORY_LISTINGS_FORMAT
- HTTP_CACHE_CONTROL
- HTTP_EXPIRES
- BASIC_AUTH_USER
Expand Down
14 changes: 13 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type config struct {
s3KeyPrefix string // AWS_S3_KEY_PREFIX
indexDocument string // INDEX_DOCUMENT
directoryListing bool // DIRECTORY_LISTINGS
dirListingFormat string // DIRECTORY_LISTINGS_FORMAT
httpCacheControl string // HTTP_CACHE_CONTROL (max-age=86400, no-cache ...)
httpExpires string // HTTP_EXPIRES (Thu, 01 Dec 1994 16:00:00 GMT ...)
basicAuthUser string // BASIC_AUTH_USER
Expand Down Expand Up @@ -63,7 +64,7 @@ func main() {

http.HandleFunc("/--version", func(w http.ResponseWriter, r *http.Request) {
if len(version) > 0 && len(date) > 0 {
fmt.Fprintf(w, "version: %s (built at %s)", version, date)
fmt.Fprintf(w, "version: %s (built at %s)\n", version, date)
} else {
w.WriteHeader(http.StatusOK)
}
Expand Down Expand Up @@ -127,6 +128,7 @@ func configFromEnvironmentVariables() *config {
s3KeyPrefix: os.Getenv("AWS_S3_KEY_PREFIX"),
indexDocument: indexDocument,
directoryListing: directoryListings,
dirListingFormat: os.Getenv("DIRECTORY_LISTINGS_FORMAT"),
httpCacheControl: os.Getenv("HTTP_CACHE_CONTROL"),
httpExpires: os.Getenv("HTTP_EXPIRES"),
basicAuthUser: os.Getenv("BASIC_AUTH_USER"),
Expand Down Expand Up @@ -352,6 +354,16 @@ func s3listFiles(w http.ResponseWriter, r *http.Request, backet, key string) {
}
sort.Sort(objects(files))

if strings.ToLower(c.dirListingFormat) == "html" {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
html := "<!DOCTYPE html><html><body><ul>"
for _, file := range files {
html += "<li><a href=\"" + file + "\">" + file + "</a></li>"
}
html += "</ul></body></html>"
fmt.Fprintln(w, html)
return
}
bytes, merr := json.Marshal(files)
if merr != nil {
http.Error(w, merr.Error(), http.StatusInternalServerError)
Expand Down

0 comments on commit 503ae6d

Please sign in to comment.