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

Datei anzeigen

@ -23,6 +23,7 @@ type ConnGet func(string) *pgxpool.Conn
var _ ConnGet = NewMiddleware().GetConn
var _ middleware.Middleware = &Middleware{}
var _ middleware.PostSetupMiddleware = &Middleware{}
// Middleware return a handler that sets the db into the context of every request.
// 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
func (m *Middleware) Sites(sites []any) (err error) {
// PostSetup is an function for getting the migrations of the site
func (m *Middleware) PostSetup(sites []any) (err error) {
m.lock.Lock()
defer m.lock.Unlock()
var (

Datei anzeigen

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