mirror of
https://github.com/pomerium/pomerium.git
synced 2025-07-02 17:38:27 +02:00
core/ui: fix page title
This commit is contained in:
parent
f9808a73ba
commit
e7ea364ecb
9 changed files with 39 additions and 31 deletions
47
ui/embed.go
47
ui/embed.go
|
@ -3,7 +3,7 @@ package ui
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"html/template"
|
||||
"io"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
@ -26,35 +26,44 @@ func ServeFile(w http.ResponseWriter, r *http.Request, filePath string) error {
|
|||
}
|
||||
|
||||
// ServePage serves the index.html page.
|
||||
func ServePage(w http.ResponseWriter, r *http.Request, page string, data map[string]interface{}) error {
|
||||
func ServePage(w http.ResponseWriter, r *http.Request, page, title string, data map[string]interface{}) error {
|
||||
if data == nil {
|
||||
data = make(map[string]interface{})
|
||||
data = make(map[string]any)
|
||||
}
|
||||
data["csrfToken"] = csrf.Token(r)
|
||||
data["page"] = page
|
||||
|
||||
jsonData, err := json.Marshal(data)
|
||||
bs, err := renderTemplate("dist/index.gohtml", map[string]any{
|
||||
"Title": title,
|
||||
"Data": 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()
|
||||
|
||||
func renderTemplate(name string, data any) ([]byte, error) {
|
||||
f, _, err := openFile(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bs, err := io.ReadAll(f)
|
||||
_ = f.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tpl, err := template.New("").Parse(string(bs))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
err = tpl.Execute(&buf, data)
|
||||
return buf.Bytes(), err
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue