Share HTML head section amongst pages

This commit is contained in:
Anders Pitman 2020-10-12 18:39:35 -06:00
parent 96611f4804
commit a749fc0b46
5 changed files with 34 additions and 47 deletions

View File

@ -18,8 +18,8 @@ type TokenData struct {
} }
type User struct { type User struct {
Name string `json:"name"` Name string `json:"name"`
IsAdmin bool `json:"is_admin"` IsAdmin bool `json:"is_admin"`
} }
type Tunnel struct { type Tunnel struct {

View File

@ -1,19 +1,7 @@
<!doctype html> <!doctype html>
<html> <html>
<head> <head>
<meta charset="utf-8"> {{.Head}}
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>boringproxy</title>
<link rel="icon" href="data:image/gif;base64,R0lGODlhEAAQAAAAACwAAAAAAQABAAACASgAOw==">
<style>
{{.Styles}}
</style>
</head> </head>
<body> <body>

View File

@ -1,17 +1,7 @@
<!doctype html> <!doctype html>
<html> <html>
<head> <head>
<meta charset="utf-8"> {{.Head}}
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>boringproxy</title>
<link rel="icon" href="data:image/gif;base64,R0lGODlhEAAQAAAAACwAAAAAAQABAAACASgAOw==">
<style>
{{.Styles}}
</style>
</head> </head>
<body> <body>

View File

@ -1,21 +1,7 @@
<!doctype html> <!doctype html>
<html> <html>
<head> <head>
<meta charset="utf-8"> {{.Head}}
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>boringproxy</title>
<link rel="icon" href="data:image/gif;base64,R0lGODlhEAAQAAAAACwAAAAAAQABAAACASgAOw==">
<style>
{{.Styles}}
button {
display: block;
}
</style>
</head> </head>
<body> <body>

View File

@ -8,6 +8,7 @@ import (
"log" "log"
"net/http" "net/http"
"strconv" "strconv"
"strings"
) )
type WebUiHandler struct { type WebUiHandler struct {
@ -18,18 +19,22 @@ type WebUiHandler struct {
} }
type IndexData struct { type IndexData struct {
Styles template.CSS Head template.HTML
Tunnels map[string]Tunnel Tunnels map[string]Tunnel
} }
type ConfirmData struct { type ConfirmData struct {
Styles template.CSS Head template.HTML
Message string Message string
ConfirmUrl string ConfirmUrl string
CancelUrl string CancelUrl string
} }
type LoginData struct { type LoginData struct {
Head template.HTML
}
type HeadData struct {
Styles template.CSS Styles template.CSS
} }
@ -58,6 +63,24 @@ func (h *WebUiHandler) handleWebUiRequest(w http.ResponseWriter, r *http.Request
return return
} }
headTmplStr, err := box.String("head.tmpl")
if err != nil {
w.WriteHeader(500)
io.WriteString(w, "Error reading head.tmpl")
return
}
headTmpl, err := template.New("head").Parse(headTmplStr)
if err != nil {
w.WriteHeader(500)
log.Println(err)
io.WriteString(w, "Error compiling head.tmpl")
return
}
var headBuilder strings.Builder
headTmpl.Execute(&headBuilder, HeadData{Styles: template.CSS(stylesText)})
switch r.URL.Path { switch r.URL.Path {
case "/login": case "/login":
h.handleLogin(w, r) h.handleLogin(w, r)
@ -83,7 +106,7 @@ func (h *WebUiHandler) handleWebUiRequest(w http.ResponseWriter, r *http.Request
} }
loginData := LoginData{ loginData := LoginData{
Styles: template.CSS(stylesText), Head: template.HTML(headBuilder.String()),
} }
w.WriteHeader(401) w.WriteHeader(401)
@ -104,7 +127,7 @@ func (h *WebUiHandler) handleWebUiRequest(w http.ResponseWriter, r *http.Request
return return
} }
tmpl, err := template.New("test").Parse(indexTemplate) tmpl, err := template.New("index").Parse(indexTemplate)
if err != nil { if err != nil {
w.WriteHeader(500) w.WriteHeader(500)
log.Println(err) log.Println(err)
@ -113,7 +136,7 @@ func (h *WebUiHandler) handleWebUiRequest(w http.ResponseWriter, r *http.Request
} }
indexData := IndexData{ indexData := IndexData{
Styles: template.CSS(stylesText), Head: template.HTML(headBuilder.String()),
Tunnels: h.db.GetTunnels(), Tunnels: h.db.GetTunnels(),
} }
@ -171,7 +194,7 @@ func (h *WebUiHandler) handleWebUiRequest(w http.ResponseWriter, r *http.Request
domain := r.Form["domain"][0] domain := r.Form["domain"][0]
data := &ConfirmData{ data := &ConfirmData{
Styles: template.CSS(stylesText), Head: template.HTML(headBuilder.String()),
Message: fmt.Sprintf("Are you sure you want to delete %s?", domain), Message: fmt.Sprintf("Are you sure you want to delete %s?", domain),
ConfirmUrl: fmt.Sprintf("/delete-tunnel?domain=%s", domain), ConfirmUrl: fmt.Sprintf("/delete-tunnel?domain=%s", domain),
CancelUrl: "/", CancelUrl: "/",