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 support for error documents #60

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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ AWS_ACCESS_KEY_ID | AWS `access key` for API access. |
AWS_SECRET_ACCESS_KEY | AWS `secret key` for API access. | | EC2 Instance Role
AWS_API_ENDPOINT | The endpoint for AWS API for local development. | | -
INDEX_DOCUMENT | Name of your index document. | | index.html
ERROR_DOCUMENT | Name of the error document. | |
DIRECTORY_LISTINGS | List files when a specified URL ends with /. | | false
DIRECTORY_LISTINGS_FORMAT | Configures directory listing to be `html` (spider parsable) | | -
HTTP_CACHE_CONTROL | Overrides S3's HTTP `Cache-Control` header. | | S3 Object metadata
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/go-openapi/swag v0.19.5
github.com/stretchr/testify v1.3.0
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
)
12 changes: 11 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,28 @@ github.com/go-openapi/swag v0.19.5 h1:lTz6Ys4CmqqCQmZPBlbQENR1/GucA2bzYTE12Pw4tF
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63 h1:nTT4s92Dgz2HlrB2NaMgvlfqHH39OgMhA7z3PK7PGD4=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd h1:O7DYs+zxREGLKzKoMQrtrEacpb0ZVXA5rIwylE2Xchk=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
2 changes: 2 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type config struct { // nolint
S3Bucket string // AWS_S3_BUCKET
S3KeyPrefix string // AWS_S3_KEY_PREFIX
IndexDocument string // INDEX_DOCUMENT
ErrorDocument string // ERROR_DOCUMENT
DirectoryListing bool // DIRECTORY_LISTINGS
DirListingFormat string // DIRECTORY_LISTINGS_FORMAT
HTTPCacheControl string // HTTP_CACHE_CONTROL (max-age=86400, no-cache ...)
Expand Down Expand Up @@ -104,6 +105,7 @@ func Setup() {
S3Bucket: os.Getenv("AWS_S3_BUCKET"),
S3KeyPrefix: os.Getenv("AWS_S3_KEY_PREFIX"),
IndexDocument: indexDocument,
ErrorDocument: os.Getenv("ERROR_DOCUMENT"),
DirectoryListing: directoryListings,
DirListingFormat: os.Getenv("DIRECTORY_LISTINGS_FORMAT"),
HTTPCacheControl: os.Getenv("HTTP_CACHE_CONTROL"),
Expand Down
2 changes: 2 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func TestChangeDefaults(t *testing.T) {
os.Setenv("IDLE_CONNECTION_TIMEOUT", "60")
os.Setenv("DISABLE_COMPRESSION", "FALSE")
os.Setenv("INSECURE_TLS", "t")
os.Setenv("ERROR_DOCUMENT", "404.html")

Setup()

Expand All @@ -69,6 +70,7 @@ func TestChangeDefaults(t *testing.T) {
expected.IdleConnTimeout = time.Duration(60) * time.Second
expected.DisableCompression = false
expected.InsecureTLS = true
expected.ErrorDocument = "404.html"

assert.Equal(t, expected, Config)
}
3 changes: 3 additions & 0 deletions internal/controllers/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ func AwsS3(w http.ResponseWriter, r *http.Request) {
}
// Get a S3 object
obj, err := client.S3get(c.S3Bucket, c.S3KeyPrefix+path, rangeHeader)
if err != nil && c.ErrorDocument != "" {
obj, err = client.S3get(c.S3Bucket, c.S3KeyPrefix+c.ErrorDocument, rangeHeader)
}
if err != nil {
code, message := toHTTPError(err)
http.Error(w, message, code)
Expand Down