Skip to content

Commit

Permalink
BASE href, config.json, and cache-control (#5)
Browse files Browse the repository at this point in the history
* added support for base-href, config.json, and cache-control

* added support for base-href, config.json and cache-control

* handling of woff mime types

Co-authored-by: Milan Unger <[email protected]>
  • Loading branch information
milung and Milan Unger authored Aug 23, 2022
1 parent e6e8452 commit 7a9bdba
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.18-alpine as builder
FROM golang:1.18 as builder
LABEL maintainer="[email protected]"
WORKDIR /app

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@ The following options can be configured through environment variables.
| READ_TIMEOUT_SECONDS | 5 |
| WRITE_TIMEOUT_SECONDS | 10 |
| IDLE_TIMEOUT_SECONDS | 120 |
| BASE_HREF | / |
| CONFIG_JSON | {} |

* `BASE_HREF` is used to replace the `href` content in the `index.html`'s string `<base href="/"`, where the original string must match exactly the one mentioned here
* `CONFIG_JSON` must be json object that will be provided as the response for the request path `/config.json`

The `Cache-Control` is a one minute validity for `/index.html` and `/config.json`, and immutable for the rest of the responses.
## Build local

```shell
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module spa-server

go 1.16
go 1.18
39 changes: 36 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"embed"
"fmt"
"io/fs"
Expand All @@ -18,7 +19,8 @@ import (
var embeddedFs embed.FS

const dirPrefix = "public"
const indexFile = "/index.html"
const indexFileName = "/index.html"
const configFileName = "/config.json"

type loadedFile struct {
file []byte
Expand Down Expand Up @@ -61,14 +63,36 @@ func loadFilesFromEmbeddedFs() (map[string]loadedFile, error) {
return err
}
path = strings.TrimLeft(path, dirPrefix)
// apply base href correction
if path == indexFileName {
file = bytes.Replace(
file,
[]byte("<base href=\"/\""),
[]byte(fmt.Sprint("<base href=\"", getenvString("BASE_HREF", "/"), "\"")),
-1)
}
mimeType := "application/unknown"
switch ext := filepath.Ext(path); ext {
case ".woff":
mimeType = "font/woff"
case ".woff2":
mimeType = "font/woff2"
default:
mimeType = mime.TypeByExtension(filepath.Ext(path))
}
files[path] = loadedFile{
file: file,
mime: mime.TypeByExtension(filepath.Ext(path)),
mime: mimeType,
}
log.Printf("Loading file from embeded filessystem. file %s\n", path)
return nil
})

files[configFileName] = loadedFile{
file: []byte(getenvString("CONFIG_JSON", "{}")),
mime: mime.TypeByExtension(filepath.Ext(configFileName)),
}

if err != nil {
return nil, err
}
Expand All @@ -90,7 +114,7 @@ func main() {
log.Fatalf("Could not load files from embedded filesystem. err: %v", err)
}

indexFile, indexFileFound := files[indexFile]
indexFile, indexFileFound := files[indexFileName]
if !indexFileFound {
log.Fatalln("Could not find index.html")
}
Expand All @@ -103,6 +127,15 @@ func main() {
loadedFile = indexFile
}
w.Header().Add("Content-Type", loadedFile.mime)
switch req.URL.Path {
case indexFileName:
fallthrough
case configFileName:
w.Header().Add("Cache-Control", "public, max-age: 60") // refresh every 1 minute to ensure fresh-ness
default:
w.Header().Add("Cache-Control", "public, max-age: 604800, immutable")
}

_, err = w.Write(loadedFile.file)
if err != nil {
log.Printf("Could not send loadedFile to client. file: %s", req.URL.Path)
Expand Down

0 comments on commit 7a9bdba

Please sign in to comment.