1
0
Fork 0

splitted Middleware.Sites into PreSetupMiddleware.PreSetup and PostSetupMiddleware.PostSetup

this fixes the problem where dependency problems are fixed
Dieser Commit ist enthalten in:
Sebastian Tobie 2022-11-07 18:45:04 +01:00
Ursprung e47609087f
Commit 3ead80fe16
4 geänderte Dateien mit 34 neuen und 12 gelöschten Zeilen

23
http.go
Datei anzeigen

@ -229,9 +229,8 @@ func (s *Server) Setup() {
site Site site Site
cfg string cfg string
) )
for cfg, m := range s.advmiddleware {
for cfg, middleware := range s.advmiddleware { m.Setup(s.Conf.Middleware[cfg])
middleware.Setup(s.Conf.Middleware[cfg])
} }
for cfg, site = range s.sites { for cfg, site = range s.sites {
config := s.Conf.Sites[cfg] config := s.Conf.Sites[cfg]
@ -250,10 +249,12 @@ func (s *Server) Setup() {
group := router.Group(config["path"].(string)) group := router.Group(config["path"].(string))
site.Init(group) site.Init(group)
} }
for _, middleware := range s.advmiddleware { for _, m := range s.advmiddleware {
if err := middleware.Sites(maptoarray(s.sites)); err != nil { if psm, ok := m.(middleware.PreSetupMiddleware); ok {
log.Error().Err(err).Msg("Failed to setup midddleware. Stopping with the setup") if err := psm.PreSetup(maptoarray(s.sites)); err != nil {
return log.Error().Err(err).Msg("Failed to setup midddleware (pre-site-setup). Stopping with the setup")
return
}
} }
} }
for cfg, site = range s.sites { for cfg, site = range s.sites {
@ -275,6 +276,14 @@ func (s *Server) Setup() {
cs.Setup(config) cs.Setup(config)
} }
} }
for _, m := range s.advmiddleware {
if psm, ok := m.(middleware.PostSetupMiddleware); ok {
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")
return
}
}
}
s.setup = true s.setup = true
} }

Datei anzeigen

@ -23,6 +23,7 @@ type ConnGet func(string) *pgxpool.Conn
var _ ConnGet = NewMiddleware().GetConn var _ ConnGet = NewMiddleware().GetConn
var _ middleware.Middleware = &Middleware{} var _ middleware.Middleware = &Middleware{}
var _ middleware.PostSetupMiddleware = &Middleware{}
// Middleware return a handler that sets the db into the context of every request. // Middleware return a handler that sets the db into the context of every request.
// uri is an url in the form dbtype:connectargs // uri is an url in the form dbtype:connectargs
@ -121,8 +122,8 @@ func (m *Middleware) Teardown() {
} }
} }
// Sites is an function for getting the migrations of the site // PostSetup is an function for getting the migrations of the site
func (m *Middleware) Sites(sites []any) (err error) { func (m *Middleware) PostSetup(sites []any) (err error) {
m.lock.Lock() m.lock.Lock()
defer m.lock.Unlock() defer m.lock.Unlock()
var ( var (

Datei anzeigen

@ -64,5 +64,16 @@ type Middleware interface {
Defaults() Config Defaults() Config
Setup(Config) Setup(Config)
Teardown() Teardown()
Sites([]any) error }
// PreSetupMiddleware is for middleware that only needs the sites after their initialization
type PreSetupMiddleware interface {
Middleware
PreSetup([]any) error
}
// PostSetupMiddleware is for middleware that requires sites to be already configured
type PostSetupMiddleware interface {
Middleware
PostSetup([]any) error
} }

Datei anzeigen

@ -11,6 +11,7 @@ import (
) )
var _ middleware.Middleware = &Middleware{} var _ middleware.Middleware = &Middleware{}
var _ middleware.PreSetupMiddleware = &Middleware{}
var _ httpserver.Site = &Middleware{} var _ httpserver.Site = &Middleware{}
type ( type (
@ -56,8 +57,8 @@ func (*Middleware) Setup(middleware.Config) {}
// Teardown does nothing // Teardown does nothing
func (*Middleware) Teardown() {} func (*Middleware) Teardown() {}
// Sites goes through all sites and collects all registrations for wellknown uris // PreSetup goes through all sites and collects all registrations for wellknown uris
func (m *Middleware) Sites(s []any) error { func (m *Middleware) PreSetup(s []any) error {
for _, site := range s { for _, site := range s {
if wfs, ok := site.(FingerSite); ok { if wfs, ok := site.(FingerSite); ok {
for _, finger := range wfs.RegisterFingers() { for _, finger := range wfs.RegisterFingers() {