1
0
Fork 0

fixed logging, sites configuration and other fixes

Dieser Commit ist enthalten in:
Sebastian Tobie 2022-11-14 09:08:33 +01:00
Ursprung bf60ad409c
Commit 1b64551582
1 geänderte Dateien mit 62 neuen und 21 gelöschten Zeilen

79
http.go
Datei anzeigen

@ -12,6 +12,7 @@ import (
"github.com/flosch/pongo2/v6" "github.com/flosch/pongo2/v6"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"go.sebtobie.de/generic"
"go.sebtobie.de/httpserver/auth" "go.sebtobie.de/httpserver/auth"
"go.sebtobie.de/httpserver/constants" "go.sebtobie.de/httpserver/constants"
"go.sebtobie.de/httpserver/funcs" "go.sebtobie.de/httpserver/funcs"
@ -52,11 +53,12 @@ type Server struct {
authh auth.AuthenticationHandler authh auth.AuthenticationHandler
middleware gin.HandlersChain middleware gin.HandlersChain
advmiddleware map[string]middleware.Middleware advmiddleware map[string]middleware.Middleware
alldomainsites []Site
} }
// CreateServer creates an server that can be run in a coroutine. // CreateServer creates an server that can be run in a coroutine.
func CreateServer() *Server { func CreateServer() *Server {
log.Info().Msg("Redirect logging output to phuslu/log") log.Info().Msg("Redirect logging output to zerolog")
gin.DefaultErrorWriter = log.Logger.With().Str("source", "GIN").Logger() gin.DefaultErrorWriter = log.Logger.With().Str("source", "GIN").Logger()
gin.DefaultWriter = log.Logger.With().Str("source", "GIN").Logger() gin.DefaultWriter = log.Logger.With().Str("source", "GIN").Logger()
log.Info().Msg("Creating HTTP-Server") log.Info().Msg("Creating HTTP-Server")
@ -74,6 +76,7 @@ func CreateServer() *Server {
middleware: gin.HandlersChain{}, middleware: gin.HandlersChain{},
template: pongo2.NewSet("templates", &templates.EmptyLoader{}), template: pongo2.NewSet("templates", &templates.EmptyLoader{}),
advmiddleware: map[string]middleware.Middleware{}, advmiddleware: map[string]middleware.Middleware{},
alldomainsites: []Site{},
} }
server.http = &http.Server{ server.http = &http.Server{
ErrorLog: intlog.New(log.Logger, "", 0), ErrorLog: intlog.New(log.Logger, "", 0),
@ -131,13 +134,13 @@ func (s *Server) StartServer() {
log.Info().Msg("Starting server") log.Info().Msg("Starting server")
s.http.TLSConfig = s.Conf.TLSconfig s.http.TLSConfig = s.Conf.TLSconfig
if s.Conf.Certfile != "" && s.Conf.Keyfile != "" { if s.Conf.Certfile != "" && s.Conf.Keyfile != "" {
s.routines.Add(len(s.Conf.TLSAddr))
for _, addr := range s.Conf.TLSAddr { for _, addr := range s.Conf.TLSAddr {
s.routines.Add(1)
go s.runPort(addr, true) go s.runPort(addr, true)
} }
} }
s.routines.Add(len(s.Conf.Addr))
for _, addr := range s.Conf.Addr { for _, addr := range s.Conf.Addr {
s.routines.Add(1)
go s.runPort(addr, false) go s.runPort(addr, false)
} }
s.routines.Wait() s.routines.Wait()
@ -155,9 +158,11 @@ func (s *Server) DomainRouter(w http.ResponseWriter, r *http.Request) {
} }
r.Host = domain r.Host = domain
r.URL.Host = domain r.URL.Host = domain
trch := log.Trace()
for header, value := range map[string][]string(r.Header) { for header, value := range map[string][]string(r.Header) {
log.Trace().Strs(header, value).Msg("Headers") trch.Strs(header, value)
} }
trch.Msg("Headers")
if router, found := s.mrouter[domain]; found { if router, found := s.mrouter[domain]; found {
router.NoMethod(gin.WrapH(s.NotFoundHandler)) router.NoMethod(gin.WrapH(s.NotFoundHandler))
router.NoRoute(gin.WrapH(s.NotFoundHandler)) router.NoRoute(gin.WrapH(s.NotFoundHandler))
@ -203,6 +208,29 @@ func maptoarray(m map[string]Site) (a []any) {
return return
} }
func (s *Server) newrouter(domain string) *gin.Engine {
log.Info().Msgf("Setting up router for %s", domain)
router := gin.New()
router.Use(func(c *gin.Context) {
c.Set(constants.Domain, 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)
s.mrouter[domain] = router
for _, site := range s.alldomainsites {
site.Init(router.Group("/"))
}
return router
}
func unequal[T comparable](i T) func(t T) bool {
return func(t T) bool {
return t != i
}
}
// 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 Midleware and the site are setup in this Order: // The Midleware and the site are setup in this Order:
// 1. Middleware.Setup // 1. Middleware.Setup
@ -220,26 +248,37 @@ func (s *Server) Setup() {
for cfg, m := range s.advmiddleware { for cfg, m := range s.advmiddleware {
m.Setup(s.Conf.Middleware[cfg]) m.Setup(s.Conf.Middleware[cfg])
} }
var config SiteConfig
var err error
for cfg, site = range s.sites { for cfg, site = range s.sites {
config := s.Conf.Sites[cfg] config, found = s.Conf.Sites[cfg]
if router, found = s.mrouter[config["domain"].(string)]; !found { var domain = "*"
log.Info().Msgf("Setting up router for %s", config["domain"].(string)) var path = "/"
router = gin.New() if found {
router.Use(func(c *gin.Context) { if generic.Valid(config["domain"], unequal("")) {
c.Set(constants.Domain, config["domain"]) domain = config["domain"].(string)
c.Set(constants.Menus, s.menus) }
c.Set(constants.Accounts, s.authh.Account(c)) if generic.Valid(config["path"], unequal("")) {
}) path = config["path"].(string)
router.Use(s.middleware...) }
router.HTMLRender = templates.NewPongo2Renderer(s.template) }
s.mrouter[config["domain"].(string)] = router log.Debug().Str("domain", domain).Str("path", path).Msgf("setting up site %s", cfg)
if domain != "*" {
if router, found = s.mrouter[domain]; !found {
router = s.newrouter(domain)
} }
group := router.Group(config["path"].(string)) group := router.Group(config["path"].(string))
site.Init(group) site.Init(group)
} else {
s.alldomainsites = append(s.alldomainsites, site)
for _, router = range s.mrouter {
site.Init(router.Group(path))
}
}
} }
for _, m := range s.advmiddleware { for _, m := range s.advmiddleware {
if psm, ok := m.(middleware.PreSetupMiddleware); ok { if psm, ok := m.(middleware.PreSetupMiddleware); ok {
if err := psm.PreSetup(maptoarray(s.sites)); err != nil { if err = psm.PreSetup(maptoarray(s.sites)); err != nil {
log.Error().Err(err).Msg("Failed to setup midddleware (pre-site-setup). Stopping with the setup") log.Error().Err(err).Msg("Failed to setup midddleware (pre-site-setup). Stopping with the setup")
return return
} }
@ -261,12 +300,14 @@ func (s *Server) Setup() {
} }
} }
if cs, ok := site.(ConfigSite); ok { if cs, ok := site.(ConfigSite); ok {
cs.Setup(config) if err = cs.Setup(config); err != nil {
log.Error().Err(err).Msgf("Failed to setup site with config %s", cfg)
}
} }
} }
for _, m := range s.advmiddleware { for _, m := range s.advmiddleware {
if psm, ok := m.(middleware.PostSetupMiddleware); ok { if psm, ok := m.(middleware.PostSetupMiddleware); ok {
if err := psm.PostSetup(maptoarray(s.sites)); err != nil { if err = psm.PostSetup(maptoarray(s.sites)); err != nil {
log.Error().Err(err).Msg("Failed to setup midddleware (post-site-setup). Stopping with the setup") log.Error().Err(err).Msg("Failed to setup midddleware (post-site-setup). Stopping with the setup")
return return
} }