46 lines
1.3 KiB
Go
46 lines
1.3 KiB
Go
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"
|
|
)
|
|
|
|
const (
|
|
// SaveAction defines label of button.
|
|
SaveAction = "Save"
|
|
// AddAction defines label of button.
|
|
AddAction = "Add"
|
|
// DeleteAction defines label of button.
|
|
DeleteAction = "Delete"
|
|
)
|
|
|
|
// 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, 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":
|
|
c.JSON(http.StatusOK, data["payload"])
|
|
case "application/xml":
|
|
c.XML(http.StatusOK, data["payload"])
|
|
default:
|
|
c.HTML(http.StatusOK, fullTemplateName, data)
|
|
}
|
|
}
|
|
|
|
// ShowIndexPage render the index page of Kontor web application.
|
|
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": properties.Application, "payload": nil}, "index.html", true)
|
|
}
|