aboutsummaryrefslogtreecommitdiff
path: root/web/web.go
blob: 0f82148d0812e154735c4f787b4bea6305fabe92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Package web contains the files for the Northstar website.
package web

import (
	"embed"
	"net/http"
	"net/url"
	"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
	}

	// rewrite /favicon.ico -> /assets/favicon.ico
	if r.URL.Path == "/favicon.ico" {
		r2 := new(http.Request)
		*r2 = *r
		r2.URL = new(url.URL)
		*r2.URL = *r.URL
		r2.URL.Path = "/assets/favicon.ico"
		r2.URL.RawPath = r2.URL.Path
		r = r2
	}

	// this handles range requests, etags, time, etc
	http.FileServer(http.FS(Assets)).ServeHTTP(w, r)
}