Make Application name configurable
This commit is contained in:
+15
-8
@@ -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()})
|
||||
}
|
||||
|
||||
+15
-1
@@ -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
|
||||
}
|
||||
|
||||
+9
-3
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user