1
0
Fork 0

contants have been moved to an own package

Dieser Commit ist enthalten in:
Sebastian Tobie 2021-11-11 23:05:07 +01:00
Ursprung 198881d347
Commit 1c151cdd04
3 geänderte Dateien mit 30 neuen und 10 gelöschten Zeilen

Datei anzeigen

@ -1,4 +1,4 @@
package httpserver package constants
// These are constants to have an single point to look up static items // These are constants to have an single point to look up static items
const ( const (

34
http.go
Datei anzeigen

@ -11,6 +11,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/phuslu/log" "github.com/phuslu/log"
"go.sebtobie.de/httpserver/auth" "go.sebtobie.de/httpserver/auth"
"go.sebtobie.de/httpserver/constants"
"go.sebtobie.de/httpserver/funcs" "go.sebtobie.de/httpserver/funcs"
"go.sebtobie.de/httpserver/menus" "go.sebtobie.de/httpserver/menus"
"go.sebtobie.de/httpserver/templates" "go.sebtobie.de/httpserver/templates"
@ -200,21 +201,40 @@ func (s *Server) menus() []menus.Menu {
return s.menu return s.menu
} }
func maptoarray(m map[string]Site) (a []interface{}) {
for _, i := range m {
a = append(a, i)
}
return
}
// Setup sets the server up. It loads the sites and prepare the server for startup. // Setup sets the server up. It loads the sites and prepare the server for startup.
// The sites get their config in this step. // The Midleware and the site are setup in this Order:
// 1. Middleware.Setup
// 2. Site.Init
// 3. Middleware.Sites
// 4. Site.Setup
func (s *Server) Setup() { func (s *Server) Setup() {
log.Info().Msg("Perparing server for start") log.Info().Msg("Perparing server for start")
var router *gin.Engine var (
var found bool router *gin.Engine
for cfg, site := range s.sites { found bool
site Site
cfg string
)
for cfg, middleware := range s.advmiddleware {
middleware.Setup(s.Conf.Middleware[cfg])
}
for cfg, site = range s.sites {
config := s.Conf.Sites[cfg] config := s.Conf.Sites[cfg]
if router, found = s.mrouter[config["domain"].(string)]; !found { if router, found = s.mrouter[config["domain"].(string)]; !found {
log.Info().Msgf("Setting up router for %s", config["domain"].(string)) log.Info().Msgf("Setting up router for %s", config["domain"].(string))
router = gin.New() router = gin.New()
router.Use(func(c *gin.Context) { router.Use(func(c *gin.Context) {
c.Set(Domain, config["domain"]) c.Set(constants.Domain, config["domain"])
c.Set(Menus, s.menus) c.Set(constants.Menus, s.menus)
c.Set(Accounts, s.authh.Account(c)) c.Set(constants.Accounts, s.authh.Account(c))
}) })
router.Use(s.middleware...) router.Use(s.middleware...)
router.HTMLRender = templates.NewPongo2Renderer(s.template) router.HTMLRender = templates.NewPongo2Renderer(s.template)

Datei anzeigen

@ -6,8 +6,8 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/phuslu/log" "github.com/phuslu/log"
"go.sebtobie.de/httpserver"
"go.sebtobie.de/httpserver/auth" "go.sebtobie.de/httpserver/auth"
"go.sebtobie.de/httpserver/constants"
) )
// LogMiddleware is an middleware to log requests to phuslu/log and catches panics. // LogMiddleware is an middleware to log requests to phuslu/log and catches panics.
@ -46,7 +46,7 @@ func LogMiddleware(c *gin.Context) {
// RequireUser is an middleware that looks if the user is an Anonymous user and redircts it to the login if so. // RequireUser is an middleware that looks if the user is an Anonymous user and redircts it to the login if so.
func RequireUser(c *gin.Context) { func RequireUser(c *gin.Context) {
var acc = c.MustGet(httpserver.Accounts).(auth.Account) var acc = c.MustGet(constants.Accounts).(auth.Account)
if acc.Anonymous() { if acc.Anonymous() {
acc.Redirect(c) acc.Redirect(c)
c.Abort() c.Abort()