-
Notifications
You must be signed in to change notification settings - Fork 30
/
main.go
279 lines (242 loc) · 7.19 KB
/
main.go
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package main
import (
"bytes"
"chrome100/v2/cros_types"
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"os"
"path"
"regexp"
"strconv"
"strings"
"github.com/evanw/esbuild/pkg/api"
"github.com/tdewolff/minify/v2"
"github.com/tdewolff/minify/v2/html"
)
func getTheme(r *http.Request) string {
theme := "light"
c, err := r.Cookie("theme")
if err == nil && c.Value == "dark" {
theme = "dark"
}
return theme
}
func main() {
db := openChromeVersions()
mirrors := loadShimMirrors()
m := minify.New()
m.AddFunc("text/html", html.Minify)
// tmpl := template.Must(template.ParseGlob("views/*.tmpl"))
tmpl := make(map[string]*template.Template)
tmpl["index"] = template.Must(template.ParseFiles("views/index.tmpl", "views/navlayout.tmpl"))
tmpl["board"] = template.Must(template.ParseFiles("views/board.tmpl", "views/navlayout.tmpl"))
tmpl["info"] = template.Must(template.ParseFiles("views/info.tmpl", "views/navlayout.tmpl"))
tmpl["guide"] = template.Must(template.ParseFiles("views/guide.tmpl", "views/navlayout.tmpl"))
tmpl["404"] = template.Must(template.ParseFiles("views/404.tmpl", "views/layout.tmpl"))
render404 := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
var buf bytes.Buffer
err := tmpl["404"].ExecuteTemplate(&buf, "base", struct {
Theme string
}{Theme: getTheme(r)})
if err == nil {
err = m.Minify("text/html", w, &buf)
}
if err != nil {
fmt.Fprintf(os.Stderr, "Rendering 404: %v\n", err)
}
}
static := http.FileServer(http.Dir("static/"))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
// load our own 404 page if we can't find the file
_, err := os.Open(path.Join("static/", path.Clean(r.URL.Path))) // Do not allow path traversals.
if os.IsNotExist(err) {
render404(w, r)
} else {
static.ServeHTTP(w, r)
}
return
}
targets := getTargets(db)
type TB struct {
Board string
Brands string
}
// boards with brands
boards := make([]TB, len(targets))
for i := range boards {
target := &targets[i]
boards[i].Board = target.Board
boards[i].Brands = strings.Join(getBrands(db, target.Board), ", ")
}
var buf bytes.Buffer
err := tmpl["index"].ExecuteTemplate(&buf, "base", struct {
Theme string
Boards []TB
}{Theme: getTheme(r), Boards: boards})
if err == nil {
err = m.Minify("text/html", w, &buf)
}
if err != nil {
fmt.Fprintf(os.Stderr, "Rendering index: %v\n", err)
}
})
http.HandleFunc("/board/{name}", func(w http.ResponseWriter, r *http.Request) {
boardName := r.PathValue("name")
target := getTarget(db, boardName)
type BoardViewData struct {
Board *string
Brands []string
Images []cros_types.CROS_RECO_IMG_DB
Shims []ResolvedShim
Theme string
}
if target == nil {
render404(w, r)
return
}
images := getImages(db, boardName)
shims := getShims(mirrors, boardName)
var buf bytes.Buffer
err := tmpl["board"].ExecuteTemplate(&buf, "base", BoardViewData{
Board: &target.Board,
Brands: getBrands(db, boardName),
Images: images,
Shims: shims,
Theme: getTheme(r),
})
if err == nil {
err = m.Minify("text/html", w, &buf)
}
if err != nil {
fmt.Fprintf(os.Stderr, "Rendering board %s: %v\n", boardName, err)
}
})
http.HandleFunc("/api/board/{name}", func(w http.ResponseWriter, r *http.Request) {
boardName := r.PathValue("name")
target := getTarget(db, boardName)
if target == nil {
w.WriteHeader(404)
return
}
// CROS_RECO_IMG + CROS_RECO_IMG_DB + a url field
type BoardAPIImg struct {
Platform string `json:"platform"`
Channel string `json:"channel"`
Board string `json:"board"`
MP_Key int `json:"mp_key"`
MP_Token string `json:"mp_token"`
LastModified string `json:"last_modified"`
Chrome string `json:"chrome"`
URL string `json:"url"`
}
type BoardAPI struct {
Images []BoardAPIImg `json:"images"`
// API change: used to be an array of objects with the board name, is now just an array of strings
Brands []string `json:"brands"`
Shims []ResolvedShim `json:"shims"`
}
res := BoardAPI{
Brands: getBrands(db, boardName),
Shims: getShims(mirrors, boardName),
}
imgs := getImages(db, boardName)
for i := range imgs {
img := &imgs[i]
res.Images = append(res.Images, BoardAPIImg{
Platform: img.Img.Platform,
Channel: img.Img.Channel,
Board: img.Img.Board,
MP_Key: img.Img.MP_Key,
MP_Token: img.Img.MP_Token,
LastModified: img.LastModified,
Chrome: img.Chrome,
URL: img.Img.URL(),
})
}
w.Header().Set("content-type", "application/json")
err := json.NewEncoder(w).Encode(res)
if err != nil {
fmt.Fprintf(os.Stderr, "Rendering API for board %s: %v\n", boardName, err)
}
})
// fix path dirs
http.HandleFunc("/board/{name}/", func(w http.ResponseWriter, r *http.Request) {
boardName := r.PathValue("name")
if boardName == "" {
} else {
w.Header().Set("location", "/board/"+boardName)
}
w.WriteHeader(308)
})
http.HandleFunc("/guide/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("location", "/guide")
w.WriteHeader(308)
})
http.HandleFunc("/info/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("location", "/info")
w.WriteHeader(308)
})
// static stuff
http.HandleFunc("/info", func(w http.ResponseWriter, r *http.Request) {
var buf bytes.Buffer
err := tmpl["info"].ExecuteTemplate(&buf, "base", struct {
Theme string
}{Theme: getTheme(r)})
if err == nil {
err = m.Minify("text/html", w, &buf)
}
if err != nil {
fmt.Fprintf(os.Stderr, "Rendering info: %v\n", err)
}
})
http.HandleFunc("/guide", func(w http.ResponseWriter, r *http.Request) {
var buf bytes.Buffer
err := tmpl["guide"].ExecuteTemplate(&buf, "base", struct {
Theme string
}{Theme: getTheme(r)})
if err == nil {
err = m.Minify("text/html", w, &buf)
}
if err != nil {
fmt.Fprintf(os.Stderr, "Rendering guide: %v\n", err)
}
})
// 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,
})
// patch the output CSS so we have more control over the color variables
out, err := os.ReadFile("static/index.css")
if err != nil {
fmt.Fprintf(os.Stderr, "Reading output css: %v\n", err)
os.Exit(1)
}
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)
port := 8080
if envPort, err := strconv.Atoi(os.Getenv("PORT")); err == nil {
port = envPort
}
address := "127.0.0.1:" + strconv.Itoa(port)
fmt.Println("Listening on http://" + address)
log.Fatal(http.ListenAndServe(address, nil))
}