aboutsummaryrefslogtreecommitdiff
path: root/web
diff options
context:
space:
mode:
authorpg9182 <96569817+pg9182@users.noreply.github.com>2022-10-13 03:03:12 -0400
committerpg9182 <96569817+pg9182@users.noreply.github.com>2022-10-13 03:03:12 -0400
commit195769230c0809cc186637951855319d1bd13f8e (patch)
treecd7110939df7e800029daa7328360d4953d3185b /web
parent6f72a024fa0cb49022bd91c0a6a08d7b9f78767d (diff)
downloadAtlas-195769230c0809cc186637951855319d1bd13f8e.tar.gz
Atlas-195769230c0809cc186637951855319d1bd13f8e.zip
pkg/api/api0, web: Implement static site handler, move redirects
Diffstat (limited to 'web')
-rw-r--r--web/assets.go7
-rw-r--r--web/web.go42
2 files changed, 42 insertions, 7 deletions
diff --git a/web/assets.go b/web/assets.go
deleted file mode 100644
index af600f4..0000000
--- a/web/assets.go
+++ /dev/null
@@ -1,7 +0,0 @@
-// Package web contains the files for the Northstar website.
-package web
-
-import "embed"
-
-//go:embed index.html assets/* script/* style/*
-var Assets embed.FS
diff --git a/web/web.go b/web/web.go
new file mode 100644
index 0000000..f76db8e
--- /dev/null
+++ b/web/web.go
@@ -0,0 +1,42 @@
+// Package web contains the files for the Northstar website.
+package web
+
+import (
+ "embed"
+ "net/http"
+ "strings"
+ "time"
+)
+
+//go:embed index.html assets/* script/* style/*
+var Assets embed.FS
+
+// TODO: compress assets
+// TODO: probably better to put website in a separate repo
+
+var Redirects = map[string]string{
+ "/github": "https://github.com/R2Northstar",
+ "/discord": "https://discord.gg/northstar",
+ "/wiki": "https://r2northstar.gitbook.io/",
+ "/thunderstore": "https://northstar.thunderstore.io",
+}
+
+// TODO: probably better to make redirects configurable via a JSON file or something
+
+func ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ // - cache publicly, allow reusing responses for multiple users
+ // - allow reusing responses if server is down
+ // - cache for up to 1800s
+ // - check for updates after 900s
+ w.Header().Set("Cache-Control", "public, max-age=1800, stale-while-revalidate=900")
+ w.Header().Set("Expires", time.Now().UTC().Add(time.Second*1800).Format(http.TimeFormat))
+
+ // check redirects first
+ if u, ok := Redirects[strings.TrimSuffix(r.URL.Path, "/")]; ok {
+ http.Redirect(w, r, u, http.StatusTemporaryRedirect)
+ return
+ }
+
+ // this handles range requests, etags, time, etc
+ http.FileServer(http.FS(Assets)).ServeHTTP(w, r)
+}