Skip to content
New issue

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

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

Already on GitHub? # to your account

Single page application feature #56

Open
wants to merge 2 commits 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 @@ -58,6 +58,7 @@ MAX_IDLE_CONNECTIONS | Allowed number of idle connections to the S3 storage
IDLE_CONNECTION_TIMEOUT | Allowed timeout to the S3 storage. | | 10
DISABLE_COMPRESSION | If true will pass encoded content through as-is. | | true
INSECURE_TLS | If true it will skip cert checks | | false
SPA | Signle Page Application - If true server will return index document content on 404 error (like `try_files $uri $uri/ /index.html;` in nginx) | | false

### 2. Run the application

Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ services:
- CONTENT_ENCODING
- HEALTHCHECK_PATH
- GET_ALL_PAGES_IN_DIR=true
- SPA
container_name: app
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
SPA bool // SPA
}

// 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
}
SPA := false
if b, err := strconv.ParseBool(os.Getenv("SPA")); err == nil {
SPA = 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"),
SPA: SPA,
}
// Proxy
log.Printf("[config] Proxy to %v", Config.S3Bucket)
Expand Down
3 changes: 3 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func defaultConfig() *config {
IdleConnTimeout: time.Duration(10) * time.Second,
DisableCompression: true,
InsecureTLS: false,
SPA: false,
}
}

Expand All @@ -56,6 +57,7 @@ func TestChangeDefaults(t *testing.T) {
os.Setenv("IDLE_CONNECTION_TIMEOUT", "60")
os.Setenv("DISABLE_COMPRESSION", "FALSE")
os.Setenv("INSECURE_TLS", "t")
os.Setenv("SPA", "TRUE")

Setup()

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

assert.Equal(t, expected, Config)
}
2 changes: 2 additions & 0 deletions internal/controllers/s3-error.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ func toHTTPError(err error) (int, string) {
switch aerr.Code() {
case s3.ErrCodeNoSuchBucket, s3.ErrCodeNoSuchKey:
return http.StatusNotFound, aerr.Error()
case "AccessDenied":
return http.StatusForbidden, aerr.Error()
}
return http.StatusInternalServerError, aerr.Error()
}
Expand Down
23 changes: 21 additions & 2 deletions internal/controllers/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,27 @@ 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)
http.Error(w, message, code)
return

if (code == 404 || code == 403) && c.SPA && strings.Index(path, c.IndexDocument) == -1 {

idx := strings.LastIndex(path, "/")

if idx > -1 {
indexPath := c.S3KeyPrefix + path[:idx+1] + c.IndexDocument

var indexError error
obj, indexError = client.S3get(c.S3Bucket, indexPath, rangeHeader)

if indexError != nil {
code, message = toHTTPError(indexError)
http.Error(w, message, code)
return
}
}
} else {
http.Error(w, message, code)
return
}
}
setHeadersFromAwsResponse(w, obj, c.HTTPCacheControl, c.HTTPExpires)

Expand Down