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

60
http.go
Datei anzeigen

@ -47,14 +47,13 @@ type Server struct {
http *http.Server
Conf *Config
mrouter map[string]*gin.Engine
sites []struct {
config SiteConfig
site Site
}
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.
@ -66,13 +65,14 @@ func CreateServer() *Server {
var server = &Server{
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,18 +178,14 @@ 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
}
for cfg, site := range s.sites {
config := s.Conf.Sites[cfg]
if router, found = s.mrouter[config.Domain()]; !found {
var authhf auth.AuthenticationHandler
router = gin.New()
mw := []gin.HandlerFunc{
func(c *gin.Context) {
@ -188,21 +193,11 @@ func (s *Server) RegisterSite(cfg string, site Site) {
c.Set(Domain, config.Domain())
},
}
if authhf, found = site.(auth.AuthenticationHandler); !found {
authhf = &auth.AnonAccountHandler{}
}
mw = append(mw, func(c *gin.Context) { authhf.Account(c) })
mw = append(mw, func(c *gin.Context) { s.authh.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))
@ -214,5 +209,14 @@ func (s *Server) RegisterSite(cfg string, site Site) {
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
}