diff --git a/cmd/root.go b/cmd/root.go index 502b52b..b7518c6 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -67,6 +67,8 @@ var rootCmd = &cobra.Command{ func Execute(version string) { rootCmd.Version = version properties.SetVersion(version) + properties.SetApplication("Kalorienmanager") + properties.SetTemplatePrefix("kalorienmanager") if err := rootCmd.Execute(); err != nil { fmt.Println(err) os.Exit(1) diff --git a/pkg/admin/views.go b/pkg/admin/views.go index 187de39..27222dd 100644 --- a/pkg/admin/views.go +++ b/pkg/admin/views.go @@ -3,17 +3,24 @@ package admin import ( "gitlab.ingenieurbuero-peetz.de/tpeetz/kalorienmanager.git/pkg/auth" "gitlab.ingenieurbuero-peetz.de/tpeetz/kalorienmanager.git/pkg/dao" + "gitlab.ingenieurbuero-peetz.de/tpeetz/kalorienmanager.git/pkg/properties" "gitlab.ingenieurbuero-peetz.de/tpeetz/kalorienmanager.git/pkg/util" "net/http" "strconv" + "fmt" "github.com/gin-gonic/gin" ) +var ( + // PageTitleUserAdministration defines the page title for user administration + PageTitleUserAdministration = fmt.Sprintf("%s User Administration", properties.Application) +) + // ShowLoginPage renders login page. func ShowLoginPage(c *gin.Context) { // Call the render function with the name of the template to render - util.Render(c, gin.H{"title": "Login"}, "login.html") + util.Render(c, gin.H{"title": "Login"}, "login.html", false) } // PerformLogin reads data from login form and validates input. @@ -34,7 +41,7 @@ func PerformLogin(c *gin.Context) { session.Username = username session.IsAdmin = user.IsAdmin sessionDao.Update(session) - util.Render(c, gin.H{"title": "Successful Login", "InfoMessage": "Login successfull"}, "kontor/index.html") + util.Render(c, gin.H{"title": "Successful Login", "InfoMessage": "Login successfull"}, "index.html", true) } else { // If the username/password combination is invalid, // show the error message on the login page @@ -56,15 +63,15 @@ func Logout(c *gin.Context) { func showAdminIndex(c *gin.Context) { // Call the render function with the name of the template to render - util.Render(c, gin.H{"title": "Kontor", "payload": nil}, "kontor/admin.html") + util.Render(c, gin.H{"title": properties.Application, "payload": nil}, "admin.html", true) } func showUserIndex(c *gin.Context) { var dao = UserDAO{Db: dao.KalorienmanagerDb} if users, err := dao.FindAll(); err == nil && users != nil { - util.Render(c, gin.H{"title": "Kontor User Administration", "payload": users}, "kontor/users.html") + util.Render(c, gin.H{"title": PageTitleUserAdministration, "payload": users}, "users.html", true) } else { - util.Render(c, gin.H{"title": "Kontor User Administration", "payload": users, "ErrorMessage": err}, "kontor/users.html") + util.Render(c, gin.H{"title": PageTitleUserAdministration, "payload": users, "ErrorMessage": err}, "users.html", true) } } @@ -72,7 +79,7 @@ func showUserDetails(c *gin.Context) { userID := c.Param("userid") var userDao = UserDAO{Db: dao.KalorienmanagerDb} if user, err := userDao.FindByID(userID); err == nil && &user != nil { - util.Render(c, gin.H{"title": "Kontor User Administration", "payload": user, "action": util.SaveAction}, "kontor/user-detail.html") + util.Render(c, gin.H{"title": PageTitleUserAdministration, "payload": user, "action": util.SaveAction}, "user-detail.html", true) } else { c.AbortWithError(http.StatusNotFound, err) } @@ -80,7 +87,7 @@ func showUserDetails(c *gin.Context) { func showUserCreation(c *gin.Context) { var user = User{} - util.Render(c, gin.H{"title": "Kontor User Administration", "payload": user, "action": util.AddAction}, "kontor/user-detail.html") + util.Render(c, gin.H{"title": PageTitleUserAdministration, "payload": user, "action": util.AddAction}, "user-detail.html", true) } func validateUserCreation(c *gin.Context) { @@ -121,7 +128,7 @@ func validateUserCreation(c *gin.Context) { if err == nil { c.Redirect(http.StatusTemporaryRedirect, "/admin/user") } else { - c.HTML(http.StatusBadRequest, "kontor/create-user.html", gin.H{ + c.HTML(http.StatusBadRequest, "kalorienmanager/create-user.html", gin.H{ "ErrorTitle": "User Creation Failed", "ErrorMessage": err.Error()}) } diff --git a/pkg/properties/root.go b/pkg/properties/root.go index c80e136..925db1b 100644 --- a/pkg/properties/root.go +++ b/pkg/properties/root.go @@ -1,12 +1,16 @@ package properties var ( - // Version defines the version of the web application kontor. + // Version defines the version of the web application kalorienmanager. Version = "undefined" // Debug defines the property debug to be used for more verbose output. Debug = false // Port defines port number under the web application is reachable. Port = 8500 + // Application defines the name of the web application + Application = "" + // TemplatePrefix defines the prefix for general templates + TemplatePrefix = "" ) // SetVersion sets Version with given value. @@ -23,3 +27,13 @@ func SetDebug(value bool) { func SetPort(value int) { Port = value } + +// SetApplication sets Applivcation with given value. +func SetApplication(value string) { + Application = value +} + +// SetTemplatePrefix sets TemplatePrefix with given value. +func SetTemplatePrefix(value string) { + TemplatePrefix = value +} diff --git a/pkg/util/render.go b/pkg/util/render.go index b83f11b..bfcb7ef 100644 --- a/pkg/util/render.go +++ b/pkg/util/render.go @@ -2,8 +2,10 @@ package util import ( "gitlab.ingenieurbuero-peetz.de/tpeetz/kalorienmanager.git/pkg/auth" + "gitlab.ingenieurbuero-peetz.de/tpeetz/kalorienmanager.git/pkg/properties" "net/http" + "fmt" "github.com/gin-gonic/gin" ) @@ -19,7 +21,11 @@ const ( // Render one of HTML, JSON or CSV based on the 'Accept' header of the request // If the header doesn't specify this, HTML is rendered, provided that // the template name is present -func Render(c *gin.Context, data gin.H, templateName string) { +func Render(c *gin.Context, data gin.H, templateName string, prefix bool) { + var fullTemplateName = templateName + if prefix { + fullTemplateName = fmt.Sprintf("%s/%s", properties.TemplatePrefix, templateName) + } auth.SetSessionData(c, data) switch c.Request.Header.Get("Accept") { case "application/json": @@ -27,7 +33,7 @@ func Render(c *gin.Context, data gin.H, templateName string) { case "application/xml": c.XML(http.StatusOK, data["payload"]) default: - c.HTML(http.StatusOK, templateName, data) + c.HTML(http.StatusOK, fullTemplateName, data) } } @@ -35,5 +41,5 @@ func Render(c *gin.Context, data gin.H, templateName string) { func ShowIndexPage(c *gin.Context) { // Call the render function with the name of the template to render //log.Printf("Context: %v", c) - Render(c, gin.H{"title": "Kalorienmanager", "payload": nil}, "index.html") + Render(c, gin.H{"title": properties.Application, "payload": nil}, "index.html", true) } diff --git a/templates/kalorienmanager/admin-menu.html b/templates/kalorienmanager/admin-menu.html new file mode 100644 index 0000000..4cd9b2c --- /dev/null +++ b/templates/kalorienmanager/admin-menu.html @@ -0,0 +1,31 @@ + diff --git a/templates/kalorienmanager/admin.html b/templates/kalorienmanager/admin.html new file mode 100644 index 0000000..7651df6 --- /dev/null +++ b/templates/kalorienmanager/admin.html @@ -0,0 +1,11 @@ + +{{ define "kalorienmanager/admin.html" }} +{{ template "header.html" .}} +{{ template "admin-menu.html" .}} + + +{{ template "footer.html" .}} +{{ end }} diff --git a/templates/kalorienmanager/create-user.html b/templates/kalorienmanager/create-user.html new file mode 100644 index 0000000..e04c0ed --- /dev/null +++ b/templates/kalorienmanager/create-user.html @@ -0,0 +1,40 @@ +{{ define "kalorienmanager/create-user.html" }} +{{ template "header.html" .}} +{{ template "admin-menu.html" .}} + + +
+
+ + {{ if .ErrorTitle}} +

+ {{.ErrorTitle}}: {{.ErrorMessage}} +

+ {{end}} + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
+
+
+{{ template "footer.html" .}} +{{end}} diff --git a/templates/kalorienmanager/footer.html b/templates/kalorienmanager/footer.html new file mode 100644 index 0000000..a099e97 --- /dev/null +++ b/templates/kalorienmanager/footer.html @@ -0,0 +1,26 @@ + + + + + + + diff --git a/templates/kalorienmanager/header.html b/templates/kalorienmanager/header.html new file mode 100644 index 0000000..eeb552d --- /dev/null +++ b/templates/kalorienmanager/header.html @@ -0,0 +1,18 @@ + + + + + + + + {{ .title }} + + + + + + + + + + diff --git a/templates/kalorienmanager/index.html b/templates/kalorienmanager/index.html new file mode 100644 index 0000000..5098af8 --- /dev/null +++ b/templates/kalorienmanager/index.html @@ -0,0 +1,17 @@ + +{{ define "kalorienmanager/index.html" }} + +{{ template "header.html" .}} + +{{ template "menu.html" . }} + +
+ {{ if .InfoMessage}} +

{{.InfoMessage}}

+ {{end}} +
+ + +{{ template "footer.html" .}} + +{{ end }} diff --git a/templates/kalorienmanager/login-successful.html b/templates/kalorienmanager/login-successful.html new file mode 100644 index 0000000..46e07fa --- /dev/null +++ b/templates/kalorienmanager/login-successful.html @@ -0,0 +1,12 @@ + + + +{{ template "header.html" .}} +{{ template "menu.html" . }} + +
+ You have successfully logged in. +
+ + +{{ template "footer.html" .}} diff --git a/templates/kalorienmanager/login.html b/templates/kalorienmanager/login.html new file mode 100644 index 0000000..83fce69 --- /dev/null +++ b/templates/kalorienmanager/login.html @@ -0,0 +1,35 @@ + + + +{{ template "header.html" .}} +{{ template "menu.html" . }} + +

Login

+ + +
+
+ + {{ if .ErrorTitle}} +

+ {{.ErrorTitle}}: {{.ErrorMessage}} +

+ {{end}} + +
+
+ + +
+
+ + +
+ +
+
+
+ + + +{{ template "footer.html" .}} diff --git a/templates/kalorienmanager/menu.html b/templates/kalorienmanager/menu.html new file mode 100644 index 0000000..c45e94b --- /dev/null +++ b/templates/kalorienmanager/menu.html @@ -0,0 +1,33 @@ + diff --git a/templates/kalorienmanager/user-detail.html b/templates/kalorienmanager/user-detail.html new file mode 100644 index 0000000..2f33c59 --- /dev/null +++ b/templates/kalorienmanager/user-detail.html @@ -0,0 +1,63 @@ +{{ define "kalorienmanager/user-detail.html" }} +{{ template "header.html" .}} +{{ template "admin-menu.html" .}} + + + +
+ + {{ if .ErrorTitle}} +

+ {{.ErrorTitle}}: {{.ErrorMessage}} +

+ {{end}} + + +
+
+ + {{ if .payload.Username }} + + {{ else }} + + {{ end }} +
+
+
+ + {{ if .payload.Firstname }} + + {{ else }} + + {{ end }} +
+
+ + {{ if .payload.Lastname }} + + {{ else }} + + {{ end }} +
+
+
+ + +
+
+ + +
+ + + {{ if eq .action "Save" }} + + {{ end }} +
+
+ +{{ template "footer.html" .}} +{{end}} diff --git a/templates/kalorienmanager/users.html b/templates/kalorienmanager/users.html new file mode 100644 index 0000000..3197562 --- /dev/null +++ b/templates/kalorienmanager/users.html @@ -0,0 +1,32 @@ +{{ define "kalorienmanager/users.html" }} +{{ template "header.html" .}} +{{ template "admin-menu.html" .}} + + +
+ + + {{range .payload }} + + + + + {{ if .IsAdmin}} + + {{end}} + {{ if not .IsAdmin }} + + {{end}} + + {{end}} +
UsernameFirst NameLast NameAdministrator
{{.Username}}{{.Firstname}}{{.Lastname}}
+
+ Add entry +
+
+ +{{ template "footer.html" .}} +{{end}}