From a749fc0b461a5556d2e3c25e509fb7e6c1b1ad00 Mon Sep 17 00:00:00 2001 From: Anders Pitman Date: Mon, 12 Oct 2020 18:39:35 -0600 Subject: [PATCH] Share HTML head section amongst pages --- database.go | 4 ++-- webui/confirm.tmpl | 14 +------------- webui/index.tmpl | 12 +----------- webui/login.tmpl | 16 +--------------- webui_handler.go | 35 +++++++++++++++++++++++++++++------ 5 files changed, 34 insertions(+), 47 deletions(-) diff --git a/database.go b/database.go index 73cd1c6..4b94a69 100644 --- a/database.go +++ b/database.go @@ -18,8 +18,8 @@ type TokenData struct { } type User struct { - Name string `json:"name"` - IsAdmin bool `json:"is_admin"` + Name string `json:"name"` + IsAdmin bool `json:"is_admin"` } type Tunnel struct { diff --git a/webui/confirm.tmpl b/webui/confirm.tmpl index 049208e..c1b8bd9 100644 --- a/webui/confirm.tmpl +++ b/webui/confirm.tmpl @@ -1,19 +1,7 @@ - - - - boringproxy - - - - - + {{.Head}} diff --git a/webui/index.tmpl b/webui/index.tmpl index 9014185..4b9e87c 100644 --- a/webui/index.tmpl +++ b/webui/index.tmpl @@ -1,17 +1,7 @@ - - - - boringproxy - - - - - + {{.Head}} diff --git a/webui/login.tmpl b/webui/login.tmpl index fc9f00e..ddce232 100644 --- a/webui/login.tmpl +++ b/webui/login.tmpl @@ -1,21 +1,7 @@ - - - - boringproxy - - - - - + {{.Head}} diff --git a/webui_handler.go b/webui_handler.go index 1e718ee..a3d052f 100644 --- a/webui_handler.go +++ b/webui_handler.go @@ -8,6 +8,7 @@ import ( "log" "net/http" "strconv" + "strings" ) type WebUiHandler struct { @@ -18,18 +19,22 @@ type WebUiHandler struct { } type IndexData struct { - Styles template.CSS + Head template.HTML Tunnels map[string]Tunnel } type ConfirmData struct { - Styles template.CSS + Head template.HTML Message string ConfirmUrl string CancelUrl string } type LoginData struct { + Head template.HTML +} + +type HeadData struct { Styles template.CSS } @@ -58,6 +63,24 @@ func (h *WebUiHandler) handleWebUiRequest(w http.ResponseWriter, r *http.Request 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 { case "/login": h.handleLogin(w, r) @@ -83,7 +106,7 @@ func (h *WebUiHandler) handleWebUiRequest(w http.ResponseWriter, r *http.Request } loginData := LoginData{ - Styles: template.CSS(stylesText), + Head: template.HTML(headBuilder.String()), } w.WriteHeader(401) @@ -104,7 +127,7 @@ func (h *WebUiHandler) handleWebUiRequest(w http.ResponseWriter, r *http.Request return } - tmpl, err := template.New("test").Parse(indexTemplate) + tmpl, err := template.New("index").Parse(indexTemplate) if err != nil { w.WriteHeader(500) log.Println(err) @@ -113,7 +136,7 @@ func (h *WebUiHandler) handleWebUiRequest(w http.ResponseWriter, r *http.Request } indexData := IndexData{ - Styles: template.CSS(stylesText), + Head: template.HTML(headBuilder.String()), Tunnels: h.db.GetTunnels(), } @@ -171,7 +194,7 @@ func (h *WebUiHandler) handleWebUiRequest(w http.ResponseWriter, r *http.Request domain := r.Form["domain"][0] data := &ConfirmData{ - Styles: template.CSS(stylesText), + Head: template.HTML(headBuilder.String()), Message: fmt.Sprintf("Are you sure you want to delete %s?", domain), ConfirmUrl: fmt.Sprintf("/delete-tunnel?domain=%s", domain), CancelUrl: "/",