diff --git a/enum.go b/constants/enum.go similarity index 88% rename from enum.go rename to constants/enum.go index d8303ca..bc0eadf 100644 --- a/enum.go +++ b/constants/enum.go @@ -1,4 +1,4 @@ -package httpserver +package constants // These are constants to have an single point to look up static items const ( diff --git a/http.go b/http.go index d27fd31..1631658 100644 --- a/http.go +++ b/http.go @@ -11,6 +11,7 @@ import ( "github.com/gin-gonic/gin" "github.com/phuslu/log" "go.sebtobie.de/httpserver/auth" + "go.sebtobie.de/httpserver/constants" "go.sebtobie.de/httpserver/funcs" "go.sebtobie.de/httpserver/menus" "go.sebtobie.de/httpserver/templates" @@ -200,21 +201,40 @@ func (s *Server) menus() []menus.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. -// 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() { log.Info().Msg("Perparing server for start") - var router *gin.Engine - var found bool - for cfg, site := range s.sites { + var ( + router *gin.Engine + 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] if router, found = s.mrouter[config["domain"].(string)]; !found { log.Info().Msgf("Setting up router for %s", config["domain"].(string)) router = gin.New() router.Use(func(c *gin.Context) { - c.Set(Domain, config["domain"]) - c.Set(Menus, s.menus) - c.Set(Accounts, s.authh.Account(c)) + c.Set(constants.Domain, config["domain"]) + c.Set(constants.Menus, s.menus) + c.Set(constants.Accounts, s.authh.Account(c)) }) router.Use(s.middleware...) router.HTMLRender = templates.NewPongo2Renderer(s.template) diff --git a/middleware/middleware.go b/middleware/middleware.go index a36b2c0..d67592c 100644 --- a/middleware/middleware.go +++ b/middleware/middleware.go @@ -6,8 +6,8 @@ import ( "github.com/gin-gonic/gin" "github.com/phuslu/log" - "go.sebtobie.de/httpserver" "go.sebtobie.de/httpserver/auth" + "go.sebtobie.de/httpserver/constants" ) // 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. func RequireUser(c *gin.Context) { - var acc = c.MustGet(httpserver.Accounts).(auth.Account) + var acc = c.MustGet(constants.Accounts).(auth.Account) if acc.Anonymous() { acc.Redirect(c) c.Abort()