Show app version on the UI
This commit is contained in:
		
							parent
							
								
									8aa84b2be6
								
							
						
					
					
						commit
						9169e75e88
					
				
							
								
								
									
										6
									
								
								main.go
								
								
								
								
							
							
						
						
									
										6
									
								
								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()) | ||||
|  |  | |||
|  | @ -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 | ||||
|  |  | |||
|  | @ -217,7 +217,7 @@ | |||
| 
 | ||||
|         <footer class="main-footer"> | ||||
|             <div class="float-right d-none d-sm-block"> | ||||
|                 <b>Version</b> 0.1 | ||||
|                 <b>Version</b> {{ .appVersion }} | ||||
|             </div> | ||||
|             <strong>Copyright © 2020 <a href="https://github.com/ngoduykhanh/wireguard-ui">Wireguard UI</a>.</strong> All rights | ||||
|             reserved. | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue