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

24
http.go
Datei anzeigen

@ -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
}

Datei anzeigen

@ -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)

Datei anzeigen

@ -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)
}

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.
// 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