-
Notifications
You must be signed in to change notification settings - Fork 280
/
embed.go
60 lines (50 loc) · 1.22 KB
/
embed.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
// Package ui contains the user info dashboard ui.
package ui
import (
"bytes"
"encoding/json"
"io"
"net/http"
"path/filepath"
"time"
"github.com/pomerium/csrf"
)
// ServeFile serves a file.
func ServeFile(w http.ResponseWriter, r *http.Request, filePath string) error {
f, etag, err := openFile(filepath.Join("dist", filePath))
if err != nil {
return err
}
defer f.Close()
w.Header().Set("ETag", `"`+etag+`"`)
http.ServeContent(w, r, filepath.Base(filePath), time.Time{}, f.(io.ReadSeeker))
return nil
}
// ServePage serves the index.html page.
func ServePage(w http.ResponseWriter, r *http.Request, page string, data map[string]interface{}) error {
if data == nil {
data = make(map[string]interface{})
}
data["csrfToken"] = csrf.Token(r)
data["page"] = page
jsonData, err := json.Marshal(data)
if err != nil {
return err
}
f, _, err := openFile("dist/index.html")
if err != nil {
return err
}
bs, err := io.ReadAll(f)
_ = f.Close()
if err != nil {
return err
}
bs = bytes.Replace(bs,
[]byte("window.POMERIUM_DATA = {}"),
append([]byte("window.POMERIUM_DATA = "), jsonData...),
1)
http.ServeContent(w, r, "index.html", time.Now(), bytes.NewReader(bs))
return nil
}
var startTime = time.Now()