Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add verbose logging when enabled #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type config struct { // nolint
DisableCompression bool // DISABLE_COMPRESSION
InsecureTLS bool // Disables TLS validation on request endpoints.
JwtSecretKey string // JWT_SECRET_KEY
VerboseLog bool // VERBOSE_LOG
}

// Setup configurations with environment variables
Expand Down Expand Up @@ -98,6 +99,10 @@ func Setup() {
if b, err := strconv.ParseBool(os.Getenv("INSECURE_TLS")); err == nil {
insecureTLS = b
}
verboseLog := false
if b, err := strconv.ParseBool(os.Getenv("VERBOSE_LOG")); err == nil {
verboseLog = b
}
Config = &config{
AwsRegion: region,
AwsAPIEndpoint: os.Getenv("AWS_API_ENDPOINT"),
Expand Down Expand Up @@ -128,6 +133,7 @@ func Setup() {
DisableCompression: disableCompression,
InsecureTLS: insecureTLS,
JwtSecretKey: os.Getenv("JWT_SECRET_KEY"),
VerboseLog: verboseLog,
}
// Proxy
log.Printf("[config] Proxy to %v", Config.S3Bucket)
Expand Down
14 changes: 12 additions & 2 deletions internal/controllers/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"reflect"
"sort"
Expand Down Expand Up @@ -50,6 +51,9 @@ func AwsS3(w http.ResponseWriter, r *http.Request) {
replaced, err := replacePathWithSymlink(client, c.S3Bucket, c.S3KeyPrefix+path[:idx+12])
if err != nil {
code, message := toHTTPError(err)
if c.VerboseLog {
log.Printf(message)
}
http.Error(w, message, code)
return
}
Expand All @@ -58,7 +62,7 @@ func AwsS3(w http.ResponseWriter, r *http.Request) {
// Ends with / -> listing or index.html
if strings.HasSuffix(path, "/") {
if c.DirectoryListing {
s3listFiles(w, r, client, c.S3Bucket, c.S3KeyPrefix+path)
s3listFiles(w, r, client, c.S3Bucket, c.S3KeyPrefix+path, c.VerboseLog)
return
}
path += c.IndexDocument
Expand All @@ -67,6 +71,9 @@ func AwsS3(w http.ResponseWriter, r *http.Request) {
obj, err := client.S3get(c.S3Bucket, c.S3KeyPrefix+path, rangeHeader)
if err != nil {
code, message := toHTTPError(err)
if c.VerboseLog {
log.Printf(message)
}
http.Error(w, message, code)
return
}
Expand Down Expand Up @@ -141,12 +148,15 @@ func setTimeHeader(w http.ResponseWriter, key string, value *time.Time) {
}
}

func s3listFiles(w http.ResponseWriter, r *http.Request, client service.AWS, bucket, prefix string) {
func s3listFiles(w http.ResponseWriter, r *http.Request, client service.AWS, bucket, prefix string, verboseLog bool) {
prefix = strings.TrimPrefix(prefix, "/")

result, err := client.S3listObjects(bucket, prefix)
if err != nil {
code, message := toHTTPError(err)
if verboseLog {
log.Printf(message)
}
http.Error(w, message, code)
return
}
Expand Down