Archived
Template
1
0
This repository has been archived on 2023-12-20. You can view files and clone it, but cannot push or open issues or pull requests.
Files

60 lines
1.6 KiB
Go
Raw Permalink Normal View History

2021-12-12 14:56:13 +01:00
// Package routes defines all the handling functions for all the routes
package routes
import (
"github.com/gin-gonic/gin"
2022-01-29 10:21:05 +01:00
"github.com/nicksnyder/go-i18n/v2/i18n"
2021-12-12 14:56:13 +01:00
"github.com/uberswe/golang-base-project/config"
2022-01-29 10:21:05 +01:00
"github.com/uberswe/golang-base-project/lang"
2021-12-12 14:56:13 +01:00
"github.com/uberswe/golang-base-project/middleware"
"gorm.io/gorm"
)
// Controller holds all the variables needed for routes to perform their logic
2021-12-12 14:56:13 +01:00
type Controller struct {
db *gorm.DB
config config.Config
2022-01-29 10:21:05 +01:00
bundle *i18n.Bundle
2021-12-12 14:56:13 +01:00
}
// New creates a new instance of the routes.Controller
2022-01-29 10:21:05 +01:00
func New(db *gorm.DB, c config.Config, bundle *i18n.Bundle) Controller {
2021-12-12 14:56:13 +01:00
return Controller{
db: db,
config: c,
2022-01-29 10:21:05 +01:00
bundle: bundle,
2021-12-12 14:56:13 +01:00
}
}
// PageData holds the default data needed for HTML pages to render
2021-12-12 14:56:13 +01:00
type PageData struct {
Title string
Messages []Message
IsAuthenticated bool
CacheParameter string
2022-01-29 10:21:05 +01:00
Trans func(s string) string
2021-12-12 14:56:13 +01:00
}
// Message holds a message which can be rendered as responses on HTML pages
2021-12-12 14:56:13 +01:00
type Message struct {
Type string // success, warning, error, etc.
Content string
}
// isAuthenticated checks if the current user is authenticated or not
2021-12-12 14:56:13 +01:00
func isAuthenticated(c *gin.Context) bool {
_, exists := c.Get(middleware.UserIDKey)
return exists
}
2022-01-29 10:21:05 +01:00
func (controller Controller) DefaultPageData(c *gin.Context) PageData {
langService := lang.New(c, controller.bundle)
return PageData{
Title: "Home",
Messages: nil,
IsAuthenticated: isAuthenticated(c),
CacheParameter: controller.config.CacheParameter,
Trans: langService.Trans,
}
}