1
0
Fork 0

restructured the server startup

Dieser Commit ist enthalten in:
Sebastian Tobie 2021-11-06 20:59:56 +01:00
Ursprung 8594622cae
Commit aa0592bca9
1 geänderte Dateien mit 57 neuen und 53 gelöschten Zeilen

110
http.go
Datei anzeigen

@ -44,17 +44,16 @@ var _ log.ObjectMarshaler = &Config{}
// Server is an wrapper for the *http.Server and *gin.Engine
type Server struct {
http *http.Server
Conf *Config
mrouter map[string]*gin.Engine
sites []struct {
config SiteConfig
site Site
}
http *http.Server
Conf *Config
mrouter map[string]*gin.Engine
sites map[string]Site
menu []menus.Menu
template template.Template
NotFoundHandler http.Handler
routines sync.WaitGroup
setup bool
authh auth.AuthenticationHandler
}
// CreateServer creates an server that can be run in a coroutine.
@ -64,15 +63,16 @@ func CreateServer() *Server {
gin.DefaultWriter = log.DefaultLogger.Std(log.DebugLevel, log.Context{}, "GIN", 0).Writer()
log.Info().Msg("Creating HTTP-Server")
var server = &Server{
Conf: &Config{},
mrouter: map[string]*gin.Engine{},
Conf: &Config{},
mrouter: map[string]*gin.Engine{},
authh: &auth.AnonAccountHandler{},
menu: []menus.Menu{},
NotFoundHandler: http.NotFoundHandler(),
}
server.http = &http.Server{
ErrorLog: log.DefaultLogger.Std(log.ErrorLevel, log.Context{}, "", 0),
Handler: http.HandlerFunc(server.DomainRouter),
}
server.NotFoundHandler = http.NotFoundHandler()
server.menu = []menus.Menu{}
return server
}
@ -99,9 +99,18 @@ func (s *Server) runPort(address string, tls bool) {
s.routines.Done()
}
// SetAuthentication sets the handler that is responsible for authentication
func (s *Server) SetAuthentication(a auth.AuthenticationHandler) {
s.authh = a
}
// StartServer starts the server as configured and sends the errormessage to the log.
// it blocks until all ports are closed.
func (s *Server) StartServer() {
if !s.setup {
log.Error().Msg("Server not set up")
return
}
log.Info().Msg("Starting server")
s.http.TLSConfig = s.Conf.TLSconfig
var err error
@ -161,7 +170,7 @@ func (s *Server) Use(m ...gin.HandlerFunc) {
func (s *Server) Stop(ctx context.Context) {
log.Info().Err(s.http.Shutdown(ctx)).Msg("Server Shut down.")
for _, s := range s.sites {
s.site.Teardown()
s.Teardown()
}
}
@ -169,50 +178,45 @@ func (s *Server) menus() []menus.Menu {
return s.menu
}
// RegisterSite adds an site to the engine as its own grouo
func (s *Server) RegisterSite(cfg string, site Site) {
// Setup ets the server up. It loads the sites and prepare the server for startup.
// The sites get their config in this step.
func (s *Server) Setup() {
var router *gin.Engine
var found bool
var config = s.Conf.Sites[cfg]
if err := site.Setup(config); err != nil {
log.Error().Err(err).Msg("Site failed to load the config")
return
}
if router, found = s.mrouter[config.Domain()]; !found {
var authhf auth.AuthenticationHandler
router = gin.New()
mw := []gin.HandlerFunc{
func(c *gin.Context) {
c.Set(Menus, s.menus)
c.Set(Domain, config.Domain())
},
for cfg, site := range s.sites {
config := s.Conf.Sites[cfg]
if router, found = s.mrouter[config.Domain()]; !found {
router = gin.New()
mw := []gin.HandlerFunc{
func(c *gin.Context) {
c.Set(Menus, s.menus)
c.Set(Domain, config.Domain())
},
}
mw = append(mw, func(c *gin.Context) { s.authh.Account(c) })
router.Use(mw...)
s.mrouter[config.Domain()] = router
}
if authhf, found = site.(auth.AuthenticationHandler); !found {
authhf = &auth.AnonAccountHandler{}
site.Init(router.Group(config.Path()))
if ms, ok := site.(menus.MenuSite); ok {
menus := ms.Menu(config.Domain())
log.Debug().Msgf("%d menus are added", len(menus))
s.menu = append(s.menu, menus...)
}
if ts, ok := site.(templates.TemplateSite); ok {
templates := ts.Templates()
log.Debug().Msgf("Templates for %s%s are added", config.Domain(), config.Path())
s.template.AddParseTree(templates.Name(), templates.Tree)
s.template.Funcs(ts.Funcs())
}
mw = append(mw, func(c *gin.Context) { authhf.Account(c) })
router.Use(mw...)
s.mrouter[config.Domain()] = router
}
site.Init(router.Group(config.Path()))
s.sites = append(s.sites, struct {
config SiteConfig
site Site
}{
config: config,
site: site,
})
if ms, ok := site.(menus.MenuSite); ok {
menus := ms.Menu(config.Domain())
log.Debug().Msgf("%d menus are added", len(menus))
s.menu = append(s.menu, menus...)
}
if ts, ok := site.(templates.TemplateSite); ok {
templates := ts.Templates()
log.Debug().Msgf("Templates for %s%s are added", config.Domain(), config.Path())
s.template.AddParseTree(templates.Name(), templates.Tree)
s.template.Funcs(ts.Funcs())
}
return
s.setup = true
}
// RegisterSite adds an site to the engine as its own grouo
// it registers the defaults so that the application can load/dump it from/into an configfile or commandline options
func (s *Server) RegisterSite(cfg string, site Site) {
var config = site.Defaults()
s.Conf.Sites[cfg] = config
s.sites[cfg] = site
}