-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathweb_release.go
45 lines (35 loc) · 1.18 KB
/
web_release.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
// When the binary is compiled in release mode, assets are embedded and served from the binary directly. This expects
// a ./dist directory containing the frontend assets. This can be created by running the `make frontend`. This is
// handled automatically if building with goreleaser via `make snapshot`.
//go:build release
package main
import (
"embed"
"errors"
"io/fs"
"log/slog"
"net/http"
)
//go:embed dist
var embedFS embed.FS
var ErrEmbedFS = errors.New("failed to load embed.fs path")
func AddRoutes(mux *http.ServeMux, _ string) error {
subFs, errSubFS := fs.Sub(embedFS, "dist")
if errSubFS != nil {
return errors.Join(errSubFS, ErrEmbedFS)
}
mux.Handle("GET /assets/", http.FileServer(http.FS(subFs)))
mux.HandleFunc("GET /", func(writer http.ResponseWriter, _ *http.Request) {
index, errIndex := embedFS.ReadFile("dist/index.html")
if errIndex != nil {
slog.Error("failed to open index.html", slog.String("error", errIndex.Error()))
http.Error(writer, "", http.StatusInternalServerError)
return
}
_, err := writer.Write(index)
if err != nil {
slog.Error("Failed to write index response", slog.String("error", errIndex.Error()))
}
})
return nil
}