1
0
Fork 0

splitted the site into configsite and teardownsite

Dieser Commit ist enthalten in:
Sebastian Tobie 2022-11-04 23:49:35 +01:00
Ursprung 2589f42da5
Commit 72cb4d16af
4 geänderte Dateien mit 30 neuen und 17 gelöschten Zeilen

12
http.go
Datei anzeigen

@ -196,7 +196,9 @@ 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.Teardown() if ts, ok := s.(TeardownSite); ok {
ts.Teardown()
}
} }
} }
@ -267,7 +269,9 @@ func (s *Server) Setup() {
s.template.AddLoader(templates) s.template.AddLoader(templates)
} }
} }
site.Setup(config) if cs, ok := site.(ConfigSite); ok {
cs.Setup(config)
}
} }
s.setup = true s.setup = true
} }
@ -275,7 +279,8 @@ func (s *Server) Setup() {
// RegisterSite adds an site to the engine as its own grouo // 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 // 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) { func (s *Server) RegisterSite(cfg string, site Site) {
var config = site.Defaults() if cs, ok := site.(ConfigSite); ok {
var config = cs.Defaults()
if _, found := config["domain"]; !found { if _, found := config["domain"]; !found {
config["domain"] = "" config["domain"] = ""
} }
@ -283,6 +288,7 @@ func (s *Server) RegisterSite(cfg string, site Site) {
config["path"] = "" config["path"] = ""
} }
s.Conf.Sites[cfg] = config s.Conf.Sites[cfg] = config
}
s.sites[cfg] = site s.sites[cfg] = site
} }

Datei anzeigen

@ -34,6 +34,7 @@ var (
Cookiename: "ILOVECOOKIES", Cookiename: "ILOVECOOKIES",
} }
_ httpserver.Site = defaultsaml _ httpserver.Site = defaultsaml
_ httpserver.ConfigSite = defaultsaml
) )
type metadata struct{} type metadata struct{}
@ -154,9 +155,6 @@ func (s *SAML) Init(router *gin.RouterGroup) {
router.POST("acs", s.acsHF) router.POST("acs", s.acsHF)
} }
// Teardown is to satisfy the httpserver.Site interface.
func (s *SAML) Teardown() {}
func (s *SAML) metadataHF(c *gin.Context) { func (s *SAML) metadataHF(c *gin.Context) {
if s.sp == nil { if s.sp == nil {
c.AbortWithStatus(500) c.AbortWithStatus(500)

Datei anzeigen

@ -13,10 +13,10 @@ func TestSamlMethods(t *testing.T) {
var samlo = &saml.SAML{} var samlo = &saml.SAML{}
var samlsite httpserver.Site = samlo var samlsite httpserver.Site = samlo
var _ auth.AuthenticationHandler = samlo var _ auth.AuthenticationHandler = samlo
defaults := samlsite.Defaults() defaults := samlsite.(httpserver.ConfigSite).Defaults()
if len(defaults) == 0 { if len(defaults) == 0 {
t.Log("There is an empty Default Object") t.Log("There is an empty Default Object")
t.Fail() t.Fail()
} }
samlsite.Setup(defaults) samlsite.(httpserver.ConfigSite).Setup(defaults)
} }

13
site.go
Datei anzeigen

@ -5,10 +5,19 @@ import "github.com/gin-gonic/gin"
// Site is an Interface to abstract the modularized group of pages. // Site is an Interface to abstract the modularized group of pages.
// The Middleware must be able to detect multiple calls by itself. Deduplication is not performed. // The Middleware must be able to detect multiple calls by itself. Deduplication is not performed.
type Site interface { type Site interface {
Setup(SiteConfig) error
Init(*gin.RouterGroup) Init(*gin.RouterGroup)
Teardown() }
// ConfigSite is for sites that have to be configured
type ConfigSite interface {
Site
Defaults() SiteConfig Defaults() SiteConfig
Setup(SiteConfig) error
}
// TeardownSite is for sites that require to do steps before shutdown
type TeardownSite interface {
Teardown()
} }
// SiteConfig is an interface for configitems of the site. The methods return the required items for the server // SiteConfig is an interface for configitems of the site. The methods return the required items for the server