From 72cb4d16afb2869b419f26499d6960810ef9e9a1 Mon Sep 17 00:00:00 2001 From: Sebastian Tobie Date: Fri, 4 Nov 2022 23:49:35 +0100 Subject: [PATCH] splitted the site into configsite and teardownsite --- http.go | 24 +++++++++++++++--------- modules/saml/saml.go | 6 ++---- modules/saml/saml_test.go | 4 ++-- site.go | 13 +++++++++++-- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/http.go b/http.go index ad25208..86e85d8 100644 --- a/http.go +++ b/http.go @@ -196,7 +196,9 @@ func (s *Server) Use(m ...gin.HandlerFunc) { func (s *Server) Stop(ctx context.Context) { log.Info().Err(s.http.Shutdown(ctx)).Msg("Server Shut down.") 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) } } - site.Setup(config) + if cs, ok := site.(ConfigSite); ok { + cs.Setup(config) + } } s.setup = true } @@ -275,14 +279,16 @@ func (s *Server) Setup() { // 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() - if _, found := config["domain"]; !found { - config["domain"] = "" + if cs, ok := site.(ConfigSite); ok { + var config = cs.Defaults() + if _, found := config["domain"]; !found { + config["domain"] = "" + } + if _, found := config["path"]; !found { + config["path"] = "" + } + s.Conf.Sites[cfg] = config } - if _, found := config["path"]; !found { - config["path"] = "" - } - s.Conf.Sites[cfg] = config s.sites[cfg] = site } diff --git a/modules/saml/saml.go b/modules/saml/saml.go index 1c3701b..8d75527 100644 --- a/modules/saml/saml.go +++ b/modules/saml/saml.go @@ -33,7 +33,8 @@ var ( Domain: "example.com", Cookiename: "ILOVECOOKIES", } - _ httpserver.Site = defaultsaml + _ httpserver.Site = defaultsaml + _ httpserver.ConfigSite = defaultsaml ) type metadata struct{} @@ -154,9 +155,6 @@ func (s *SAML) Init(router *gin.RouterGroup) { router.POST("acs", s.acsHF) } -// Teardown is to satisfy the httpserver.Site interface. -func (s *SAML) Teardown() {} - func (s *SAML) metadataHF(c *gin.Context) { if s.sp == nil { c.AbortWithStatus(500) diff --git a/modules/saml/saml_test.go b/modules/saml/saml_test.go index 9bce37a..108354d 100644 --- a/modules/saml/saml_test.go +++ b/modules/saml/saml_test.go @@ -13,10 +13,10 @@ func TestSamlMethods(t *testing.T) { var samlo = &saml.SAML{} var samlsite httpserver.Site = samlo var _ auth.AuthenticationHandler = samlo - defaults := samlsite.Defaults() + defaults := samlsite.(httpserver.ConfigSite).Defaults() if len(defaults) == 0 { t.Log("There is an empty Default Object") t.Fail() } - samlsite.Setup(defaults) + samlsite.(httpserver.ConfigSite).Setup(defaults) } diff --git a/site.go b/site.go index 551ea1e..1069780 100644 --- a/site.go +++ b/site.go @@ -5,10 +5,19 @@ import "github.com/gin-gonic/gin" // 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. type Site interface { - Setup(SiteConfig) error Init(*gin.RouterGroup) - Teardown() +} + +// ConfigSite is for sites that have to be configured +type ConfigSite interface { + Site 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