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 http *http.Server
Conf *Config Conf *Config
mrouter map[string]*gin.Engine mrouter map[string]*gin.Engine
sites []struct { sites map[string]Site
config SiteConfig
site Site
}
menu []menus.Menu menu []menus.Menu
template template.Template template template.Template
NotFoundHandler http.Handler NotFoundHandler http.Handler
routines sync.WaitGroup routines sync.WaitGroup
setup bool
authh auth.AuthenticationHandler
} }
// CreateServer creates an server that can be run in a coroutine. // CreateServer creates an server that can be run in a coroutine.
@ -66,13 +65,14 @@ func CreateServer() *Server {
var server = &Server{ var server = &Server{
Conf: &Config{}, Conf: &Config{},
mrouter: map[string]*gin.Engine{}, mrouter: map[string]*gin.Engine{},
authh: &auth.AnonAccountHandler{},
menu: []menus.Menu{},
NotFoundHandler: http.NotFoundHandler(),
} }
server.http = &http.Server{ server.http = &http.Server{
ErrorLog: log.DefaultLogger.Std(log.ErrorLevel, log.Context{}, "", 0), ErrorLog: log.DefaultLogger.Std(log.ErrorLevel, log.Context{}, "", 0),
Handler: http.HandlerFunc(server.DomainRouter), Handler: http.HandlerFunc(server.DomainRouter),
} }
server.NotFoundHandler = http.NotFoundHandler()
server.menu = []menus.Menu{}
return server return server
} }
@ -99,9 +99,18 @@ func (s *Server) runPort(address string, tls bool) {
s.routines.Done() 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. // StartServer starts the server as configured and sends the errormessage to the log.
// it blocks until all ports are closed. // it blocks until all ports are closed.
func (s *Server) StartServer() { func (s *Server) StartServer() {
if !s.setup {
log.Error().Msg("Server not set up")
return
}
log.Info().Msg("Starting server") log.Info().Msg("Starting server")
s.http.TLSConfig = s.Conf.TLSconfig s.http.TLSConfig = s.Conf.TLSconfig
var err error var err error
@ -161,7 +170,7 @@ func (s *Server) Use(m ...gin.HandlerFunc) {
func (s *Server) Stop(ctx context.Context) { func (s *Server) Stop(ctx context.Context) {
log.Info().Err(s.http.Shutdown(ctx)).Msg("Server Shut down.") log.Info().Err(s.http.Shutdown(ctx)).Msg("Server Shut down.")
for _, s := range s.sites { for _, s := range s.sites {
s.site.Teardown() s.Teardown()
} }
} }
@ -169,18 +178,14 @@ func (s *Server) menus() []menus.Menu {
return s.menu return s.menu
} }
// RegisterSite adds an site to the engine as its own grouo // Setup ets the server up. It loads the sites and prepare the server for startup.
func (s *Server) RegisterSite(cfg string, site Site) { // The sites get their config in this step.
func (s *Server) Setup() {
var router *gin.Engine var router *gin.Engine
var found bool var found bool
var config = s.Conf.Sites[cfg] for cfg, site := range s.sites {
if err := site.Setup(config); err != nil { config := s.Conf.Sites[cfg]
log.Error().Err(err).Msg("Site failed to load the config")
return
}
if router, found = s.mrouter[config.Domain()]; !found { if router, found = s.mrouter[config.Domain()]; !found {
var authhf auth.AuthenticationHandler
router = gin.New() router = gin.New()
mw := []gin.HandlerFunc{ mw := []gin.HandlerFunc{
func(c *gin.Context) { func(c *gin.Context) {
@ -188,21 +193,11 @@ func (s *Server) RegisterSite(cfg string, site Site) {
c.Set(Domain, config.Domain()) c.Set(Domain, config.Domain())
}, },
} }
if authhf, found = site.(auth.AuthenticationHandler); !found { mw = append(mw, func(c *gin.Context) { s.authh.Account(c) })
authhf = &auth.AnonAccountHandler{}
}
mw = append(mw, func(c *gin.Context) { authhf.Account(c) })
router.Use(mw...) router.Use(mw...)
s.mrouter[config.Domain()] = router s.mrouter[config.Domain()] = router
} }
site.Init(router.Group(config.Path())) 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 { if ms, ok := site.(menus.MenuSite); ok {
menus := ms.Menu(config.Domain()) menus := ms.Menu(config.Domain())
log.Debug().Msgf("%d menus are added", len(menus)) 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.AddParseTree(templates.Name(), templates.Tree)
s.template.Funcs(ts.Funcs()) 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
} }