Make Application name configurable

This commit is contained in:
Thomas Peetz
2019-09-03 15:42:48 +02:00
parent b7cbe08d7b
commit 276e65b153
15 changed files with 359 additions and 12 deletions
+2
View File
@@ -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)
+15 -8
View File
@@ -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
View File
@@ -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
View File
@@ -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)
}
+31
View File
@@ -0,0 +1,31 @@
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">Kalorienmanager</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
{{ if .is_logged_in }}
<li><a href="/data">Data</a></li>
<li><a href="/menu">Menu</a></li>
{{end}}
{{ if not .is_logged_in }}
<li><a href="/user/login"><span class="glyphicon glyphicon-off" aria-hidden="true"></span></a></li>
{{end}}
{{ if .is_logged_in }}
<li class="active"><a href="/admin/">Admin<span class="sr-only">(current)</a></li>
<li><a href="/user/logout"><span class="glyphicon glyphicon-off" aria-hidden="true"></span></a></li>
{{end}}
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
+11
View File
@@ -0,0 +1,11 @@
<!--admin.html-->
{{ define "kalorienmanager/admin.html" }}
{{ template "header.html" .}}
{{ template "admin-menu.html" .}}
<ul class="nav nav-tabs">
<li role="presentation" class="active"><a href="/admin/user">User</a></li>
<li role="presentation"><a href="/admin/data">Data</a></li>
</ul>
{{ template "footer.html" .}}
{{ end }}
@@ -0,0 +1,40 @@
{{ define "kalorienmanager/create-user.html" }}
{{ template "header.html" .}}
{{ template "admin-menu.html" .}}
<ul class="nav nav-tabs">
<li role="presentation" class="active"><a href="/admin/user">User</a></li>
<li role="presentation"><a href="/admin/data">Data</a></li>
</ul>
<div class="panel panel-default col-sm-6">
<div class="panel-body">
<!--If there's an error, display the error-->
{{ if .ErrorTitle}}
<p class="bg-danger">
{{.ErrorTitle}}: {{.ErrorMessage}}
</p>
{{end}}
<!--Create a form that POSTs to the `/u/register` route-->
<form class="form" action="/admin/user/create" method="POST">
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" name="username" placeholder="Username">
</div>
<div class="form-group">
<label for="firstname">First Name</label>
<input type="text" class="form-control" id="firstname" name="firstname" placeholder="First Name">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" name="password" class="form-control" id="password" placeholder="Password">
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="admin">
<label class="form-check-label" for="admin">Administrator</label>
</div>
<button type="submit" class="btn btn-primary">Add</button>
</form>
</div>
</div>
{{ template "footer.html" .}}
{{end}}
+26
View File
@@ -0,0 +1,26 @@
<!--footer.html-->
<nav class="navbar navbar-default navbar-fixed-bottom">
<div class="container">
<div class="row">
<p class="col-lg-12">
<ul class="list-inline">
<li><a href="/">Kalorienmanager</a></li>
<li class="footer-menu-divider"></li>
{{ if not .is_logged_in }}
<li><a href="/user/login">Login</a></li>
{{end}}
{{ if .is_logged_in }}
<li><a href="/user/logout">Logout</a></li>
{{end}}
</ul>
<p class="copyright text-muted small">Version {{ .version }} Copyright © 2018. All Rights Reserved</p>
</div>
</div>
</div>
</nav>
</body>
</html>
+18
View File
@@ -0,0 +1,18 @@
<!--header.html-->
<!doctype html>
<html>
<head>
<!--Use the `title` variable to set the title of the page-->
<title>{{ .title }}</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="UTF-8">
<!--Use bootstrap to make the application look decent-->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script async src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
</head>
<body class="container">
+17
View File
@@ -0,0 +1,17 @@
<!--index.html-->
{{ define "kalorienmanager/index.html" }}
<!--Embed the header.html template at this location-->
{{ template "header.html" .}}
<!--Embed the menu.html template at this location-->
{{ template "menu.html" . }}
<div>
{{ if .InfoMessage}}
<p class="bg-info">{{.InfoMessage}}</p>
{{end}}
</div>
<!--Embed the footer.html template at this location-->
{{ template "footer.html" .}}
{{ end }}
@@ -0,0 +1,12 @@
<!--login-successful.html-->
<!--Embed the header.html template at this location-->
{{ template "header.html" .}}
{{ template "menu.html" . }}
<div>
You have successfully logged in.
</div>
<!--Embed the footer.html template at this location-->
{{ template "footer.html" .}}
+35
View File
@@ -0,0 +1,35 @@
<!--login.html-->
<!--Embed the header.html template at this location-->
{{ template "header.html" .}}
{{ template "menu.html" . }}
<h1>Login</h1>
<div class="panel panel-default col-sm-6">
<div class="panel-body">
<!--If there's an error, display the error-->
{{ if .ErrorTitle}}
<p class="bg-danger">
{{.ErrorTitle}}: {{.ErrorMessage}}
</p>
{{end}}
<!--Create a form that POSTs to the `/u/login` route-->
<form class="form" action="/user/login" method="POST">
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" name="username" placeholder="Username">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" name="password" placeholder="Password">
</div>
<button type="submit" class="btn btn-primary">Login</button>
</form>
</div>
</div>
<!--Embed the footer.html template at this location-->
{{ template "footer.html" .}}
+33
View File
@@ -0,0 +1,33 @@
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">Kalorienmanager</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
{{ if .is_logged_in }}
<li><a href="/data">Data</a></li>
<li><a href="/menu">Menu</a></li>
{{end}}
{{ if not .is_logged_in }}
<li><a href="/user/login"><span class="glyphicon glyphicon-off" aria-hidden="true"></span></a></li>
{{end}}
{{ if .is_admin }}
<li><a href="/admin/">Admin</a></li>
{{end}}
{{ if .is_logged_in }}
<li><a href="/user/logout"><span class="glyphicon glyphicon-off" aria-hidden="true"></span></a></li>
{{end}}
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
@@ -0,0 +1,63 @@
{{ define "kalorienmanager/user-detail.html" }}
{{ template "header.html" .}}
{{ template "admin-menu.html" .}}
<ul class="nav nav-tabs">
<li role="presentation" class="active"><a href="/admin/user">User</a></li>
<li role="presentation"><a href="/admin/data">Data</a></li>
</ul>
<!--<div class="panel panel-default col-sm-6">-->
<div class="panel-body">
<!--If there's an error, display the error-->
{{ if .ErrorTitle}}
<p class="bg-danger">
{{.ErrorTitle}}: {{.ErrorMessage}}
</p>
{{end}}
<!--Create a form that POSTs to the `/u/register` route-->
<!--<form class="form" action="/admin/user/view" method="POST">-->
<form action="/admin/user/view" method="POST">
<div class="form-group">
<label for="username">Username</label>
{{ if .payload.Username }}
<input type="text" class="form-control" id="username" name="username" value={{.payload.Username}}>
{{ else }}
<input type="text" class="form-control" id="username" name="username" placeholder="Username">
{{ end }}
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="firstname">First Name</label>
{{ if .payload.Firstname }}
<input type="text" class="form-control" id="firstname" name="firstname" value={{.payload.Firstname}}>
{{ else }}
<input type="text" class="form-control" id="firstname" name="firstname" placeholder="First Name">
{{ end }}
</div>
<div class="form-group col-md-6">
<label for="lastname">Last Name</label>
{{ if .payload.Lastname }}
<input type="text" class="form-control" id="lastname" name="lastname" value={{.payload.Lastname}}>
{{ else }}
<input type="text" class="form-control" id="lastname" name="lastname" placeholder="Last Name">
{{ end }}
</div>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" name="password" class="form-control" id="password" placeholder="Password">
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" value="true" name="admin" id="admin" {{ if .payload.IsAdmin }}checked{{end}}>
<label class="form-check-label" for="admin">Administrator</label>
</div>
<input type="hidden" name="userid" id="userid" value={{.payload.ID.Hex}}>
<button type="submit" class="btn btn-primary" name="action" value={{.action}}>{{.action}}</button>
{{ if eq .action "Save" }}
<button type="submit" class="btn btn-primary" name="action" value="Delete">Delete</button>
{{ end }}
</form>
</div>
<!--</div>-->
{{ template "footer.html" .}}
{{end}}
+32
View File
@@ -0,0 +1,32 @@
{{ define "kalorienmanager/users.html" }}
{{ template "header.html" .}}
{{ template "admin-menu.html" .}}
<ul class="nav nav-tabs">
<li role="presentation" class="active"><a href="/admin/user">User</a></li>
<li role="presentation"><a href="/admin/data">Data</a></li>
</ul>
<div class="panel panel-default container">
<table class="table">
<tr><th>Username</th><th>First Name</th><th>Last Name</th><th>Administrator</th></tr>
{{range .payload }}
<tr>
<td><a href="/admin/user/view/{{.ID.Hex}}">{{.Username}}</a></td>
<td>{{.Firstname}}</td>
<td>{{.Lastname}}</td>
{{ if .IsAdmin}}
<td><span class="glyphicon glyphicon-ok" aria-hidden="true"></span></td>
{{end}}
{{ if not .IsAdmin }}
<td><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></td>
{{end}}
</tr>
{{end}}
</table>
<div class="panel-body">
<a href="/admin/user/create">Add entry</a>
</div>
</div>
{{ template "footer.html" .}}
{{end}}