diff --git a/main.go b/main.go index 4c7c26f..0022951 100644 --- a/main.go +++ b/main.go @@ -25,6 +25,10 @@ func main() { fmt.Println("Build Time\t:", buildTime) fmt.Println("Git Repo\t:", "https://github.com/ngoduykhanh/wireguard-ui") + // set app extra data + extraData := make(map[string]string) + extraData["appVersion"] = appVersion + // initialize DB err := util.InitDB() if err != nil { @@ -38,7 +42,7 @@ func main() { assetHandler := http.FileServer(rice.MustFindBox("assets").HTTPBox()) // register routes - app := router.New(tmplBox) + app := router.New(tmplBox, extraData) app.GET("/", handler.WireGuardClients()) app.GET("/login", handler.LoginPage()) diff --git a/router/router.go b/router/router.go index 595c1cd..2bd634e 100644 --- a/router/router.go +++ b/router/router.go @@ -3,6 +3,7 @@ package router import ( "errors" "io" + "reflect" "text/template" "github.com/GeertJohan/go.rice" @@ -16,6 +17,7 @@ import ( // TemplateRegistry is a custom html/template renderer for Echo framework type TemplateRegistry struct { templates map[string]*template.Template + extraData map[string]string } // Render e.Renderer interface @@ -25,15 +27,24 @@ func (t *TemplateRegistry) Render(w io.Writer, name string, data interface{}, c err := errors.New("Template not found -> " + name) return err } + + // inject more app data information. E.g. appVersion + if reflect.TypeOf(data).Kind() == reflect.Map { + for k, v := range t.extraData { + data.(map[string]interface{})[k] = v + } + } + // login page does not need the base layout if name == "login.html" { return tmpl.Execute(w, data) } + return tmpl.ExecuteTemplate(w, "base.html", data) } // New function -func New(tmplBox *rice.Box) *echo.Echo { +func New(tmplBox *rice.Box, extraData map[string]string) *echo.Echo { e := echo.New() e.Use(session.Middleware(sessions.NewCookieStore([]byte("secret")))) @@ -82,6 +93,7 @@ func New(tmplBox *rice.Box) *echo.Echo { e.Validator = NewValidator() e.Renderer = &TemplateRegistry{ templates: templates, + extraData: extraData, } return e diff --git a/templates/base.html b/templates/base.html index f756108..542aca7 100644 --- a/templates/base.html +++ b/templates/base.html @@ -217,7 +217,7 @@