-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathstatic.go
More file actions
169 lines (141 loc) · 4.16 KB
/
Copy pathstatic.go
File metadata and controls
169 lines (141 loc) · 4.16 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"crypto/sha256"
"embed"
"encoding/hex"
"fmt"
"io"
"mime"
"net/http"
"os"
"path"
"regexp"
"time"
"github.com/evanw/esbuild/pkg/api"
)
//go:embed static/*
var staticFiles embed.FS
func patchCSS() {
// patch the output CSS so we have more control over the color variables
index, err := os.Open("static/index.css")
if err != nil {
fmt.Fprintf(os.Stderr, "Reading output css: %v\n", err)
os.Exit(1)
}
out, _ := io.ReadAll(index)
reg := regexp.MustCompile(`@media \(prefers-color-scheme: (\w+)\)\{\.markdown-body,\[data-theme=\w+\]\{(.*?)\}\}`)
// captures theme (light, dark) and raw rules
out = reg.ReplaceAll(out, []byte("[data-theme=$1]{$2}"))
//os.WriteFile("static/index.css", out, 0644)
hasher := sha256.New()
hasher.Write(out)
etag := `W/"` + hex.EncodeToString(hasher.Sum(nil)) + `"`
stat, _ := index.Stat()
indexModTime := stat.ModTime()
http.HandleFunc("/index.css", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/css")
w.Header().Set("Last-Modified", indexModTime.UTC().Format(http.TimeFormat))
w.Header().Set("ETag", etag)
w.Write(out)
})
}
func serveStatic() {
entries, err := staticFiles.ReadDir("static")
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to list static files: %v\n", err)
os.Exit(1)
}
for _, entry := range entries {
if entry.IsDir() {
continue
}
name := entry.Name()
route := "/" + name
assetPath := "static/" + name
ext := path.Ext(name)
if ext == ".js" || ext == ".css" {
continue
}
content, err := staticFiles.ReadFile(assetPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to embed %s: %v\n", name, err)
continue
}
contentType := mime.TypeByExtension(ext)
if contentType == "" {
contentType = "application/octet-stream"
}
info, _ := entry.Info()
modTime := info.ModTime()
hasher := sha256.New()
hasher.Write(content)
hasher.Write([]byte(modTime.UTC().Format(http.TimeFormat)))
etag := `W/"` + hex.EncodeToString(hasher.Sum(nil)) + `"`
func(route string, content []byte, etag string) {
http.HandleFunc(route, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", contentType)
w.Header().Set("Last-Modified", modTime.UTC().Format(http.TimeFormat))
w.Header().Set("ETag", etag)
if match := r.Header.Get("If-None-Match"); match != "" {
if match == etag {
w.WriteHeader(http.StatusNotModified)
return
}
}
if t, err := http.ParseTime(r.Header.Get("If-Modified-Since")); err == nil && modTime.Before(t.Add(1*time.Second)) {
w.WriteHeader(http.StatusNotModified)
return
}
w.Write(content)
})
}(route, content, etag)
}
}
func serveScripts() {
// build board js when script is ran
api.Build(api.BuildOptions{
EntryPoints: []string{"views/theme.ts", "views/nav.ts", "views/board.ts", "views/index.css"},
Outdir: "static/",
Bundle: true,
Write: true,
MinifyWhitespace: true,
MinifyIdentifiers: true,
MinifySyntax: true,
Banner: map[string]string{
"js": "'use strict';",
},
Format: api.FormatCommonJS,
Platform: api.PlatformBrowser,
LogLevel: api.LogLevelInfo,
})
compileTime := time.Now()
jsFiles := []string{"nav.js", "board.js", "theme.js"}
for _, name := range jsFiles {
route := "/" + name
path := "static/" + name
content, err := os.ReadFile(path)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to embed %s: %v\n", name, err)
continue
}
hasher := sha256.New()
hasher.Write(content)
etag := `W/"` + hex.EncodeToString(hasher.Sum(nil)) + `"`
http.HandleFunc(route, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/javascript")
w.Header().Set("Last-Modified", compileTime.UTC().Format(http.TimeFormat))
w.Header().Set("ETag", etag)
if match := r.Header.Get("If-None-Match"); match != "" {
if match == etag {
w.WriteHeader(http.StatusNotModified)
return
}
}
if t, err := http.ParseTime(r.Header.Get("If-Modified-Since")); err == nil && compileTime.Before(t.Add(1*time.Second)) {
w.WriteHeader(http.StatusNotModified)
return
}
w.Write(content)
})
}
}