restructured the server startup
Dieser Commit ist enthalten in:
Ursprung
8594622cae
Commit
aa0592bca9
110
http.go
110
http.go
|
@ -44,17 +44,16 @@ var _ log.ObjectMarshaler = &Config{}
|
||||||
|
|
||||||
// Server is an wrapper for the *http.Server and *gin.Engine
|
// Server is an wrapper for the *http.Server and *gin.Engine
|
||||||
type Server struct {
|
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.
|
||||||
|
@ -64,15 +63,16 @@ func CreateServer() *Server {
|
||||||
gin.DefaultWriter = log.DefaultLogger.Std(log.DebugLevel, log.Context{}, "GIN", 0).Writer()
|
gin.DefaultWriter = log.DefaultLogger.Std(log.DebugLevel, log.Context{}, "GIN", 0).Writer()
|
||||||
log.Info().Msg("Creating HTTP-Server")
|
log.Info().Msg("Creating HTTP-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,50 +178,45 @@ 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")
|
if router, found = s.mrouter[config.Domain()]; !found {
|
||||||
return
|
router = gin.New()
|
||||||
}
|
mw := []gin.HandlerFunc{
|
||||||
|
func(c *gin.Context) {
|
||||||
if router, found = s.mrouter[config.Domain()]; !found {
|
c.Set(Menus, s.menus)
|
||||||
var authhf auth.AuthenticationHandler
|
c.Set(Domain, config.Domain())
|
||||||
router = gin.New()
|
},
|
||||||
mw := []gin.HandlerFunc{
|
}
|
||||||
func(c *gin.Context) {
|
mw = append(mw, func(c *gin.Context) { s.authh.Account(c) })
|
||||||
c.Set(Menus, s.menus)
|
router.Use(mw...)
|
||||||
c.Set(Domain, config.Domain())
|
s.mrouter[config.Domain()] = router
|
||||||
},
|
|
||||||
}
|
}
|
||||||
if authhf, found = site.(auth.AuthenticationHandler); !found {
|
site.Init(router.Group(config.Path()))
|
||||||
authhf = &auth.AnonAccountHandler{}
|
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.setup = true
|
||||||
s.sites = append(s.sites, struct {
|
}
|
||||||
config SiteConfig
|
|
||||||
site Site
|
// 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
|
||||||
config: config,
|
func (s *Server) RegisterSite(cfg string, site Site) {
|
||||||
site: site,
|
var config = site.Defaults()
|
||||||
})
|
s.Conf.Sites[cfg] = config
|
||||||
if ms, ok := site.(menus.MenuSite); ok {
|
s.sites[cfg] = site
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
Laden…
In neuem Issue referenzieren