added support for domains
Dieser Commit ist enthalten in:
Ursprung
d827664a69
Commit
9973e186ee
32
http.go
32
http.go
|
@ -30,10 +30,12 @@ type Server struct {
|
||||||
http *http.Server
|
http *http.Server
|
||||||
conf *Config
|
conf *Config
|
||||||
router *gin.Engine
|
router *gin.Engine
|
||||||
|
mrouter map[string]*gin.Engine
|
||||||
config *toml.Tree
|
config *toml.Tree
|
||||||
authhf auth.AuthenticationHandler
|
authhf auth.AuthenticationHandler
|
||||||
sites []Site
|
sites []Site
|
||||||
menus []menus.Menu
|
menus []menus.Menu
|
||||||
|
NotFoundHandler gin.HandlerFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
// StartServer starts the server as configured and sends the errormessage to the log.
|
// StartServer starts the server as configured and sends the errormessage to the log.
|
||||||
|
@ -51,6 +53,19 @@ func (s *Server) StartServer() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DomainRouter redirects the requests to the routers of the domains
|
||||||
|
func (s *Server) DomainRouter(c *gin.Context) {
|
||||||
|
domain := c.Request.URL.Host
|
||||||
|
if router, found := s.mrouter[domain]; found {
|
||||||
|
router.NoMethod(s.NotFoundHandler)
|
||||||
|
router.NoRoute(s.NotFoundHandler)
|
||||||
|
c.Set("domain", domain)
|
||||||
|
router.HandleContext(c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s.NotFoundHandler(c)
|
||||||
|
}
|
||||||
|
|
||||||
// CreateServer creates an server that can be run in a coroutine.
|
// CreateServer creates an server that can be run in a coroutine.
|
||||||
func CreateServer(config *toml.Tree) *Server {
|
func CreateServer(config *toml.Tree) *Server {
|
||||||
log.Info().Msg("Redirect logging output to phuslu/log")
|
log.Info().Msg("Redirect logging output to phuslu/log")
|
||||||
|
@ -62,6 +77,7 @@ func CreateServer(config *toml.Tree) *Server {
|
||||||
Addr: "127.0.0.1:8080",
|
Addr: "127.0.0.1:8080",
|
||||||
},
|
},
|
||||||
router: gin.New(),
|
router: gin.New(),
|
||||||
|
mrouter: map[string]*gin.Engine{},
|
||||||
authhf: &auth.AnonAccountHandler{},
|
authhf: &auth.AnonAccountHandler{},
|
||||||
}
|
}
|
||||||
mw := []gin.HandlerFunc{
|
mw := []gin.HandlerFunc{
|
||||||
|
@ -69,8 +85,10 @@ func CreateServer(config *toml.Tree) *Server {
|
||||||
c.Set(Accounts, server.authhf.Account(c))
|
c.Set(Accounts, server.authhf.Account(c))
|
||||||
c.Set(Menus, server.menus)
|
c.Set(Menus, server.menus)
|
||||||
},
|
},
|
||||||
|
server.DomainRouter,
|
||||||
}
|
}
|
||||||
server.Use(mw...)
|
server.Use(mw...)
|
||||||
|
server.router.RouterGroup.Handlers = mw
|
||||||
if err := config.Unmarshal(server.conf); err != nil {
|
if err := config.Unmarshal(server.conf); err != nil {
|
||||||
log.Error().Msg("Problem mapping config to Configstruct")
|
log.Error().Msg("Problem mapping config to Configstruct")
|
||||||
}
|
}
|
||||||
|
@ -81,7 +99,7 @@ func CreateServer(config *toml.Tree) *Server {
|
||||||
Handler: server.router,
|
Handler: server.router,
|
||||||
TLSConfig: server.conf.TLSconfig,
|
TLSConfig: server.conf.TLSconfig,
|
||||||
}
|
}
|
||||||
server.router.NoRoute(gin.WrapH(http.NotFoundHandler()))
|
server.NotFoundHandler = gin.WrapH(http.NotFoundHandler())
|
||||||
gin.DebugPrintRouteFunc = func(httpMethod, absolutePath, handlerName string, nuHandlers int) {
|
gin.DebugPrintRouteFunc = func(httpMethod, absolutePath, handlerName string, nuHandlers int) {
|
||||||
log.Trace().Msgf("%-4s(%02d): %-20s %s", httpMethod, nuHandlers-len(mw), absolutePath, handlerName)
|
log.Trace().Msgf("%-4s(%02d): %-20s %s", httpMethod, nuHandlers-len(mw), absolutePath, handlerName)
|
||||||
}
|
}
|
||||||
|
@ -116,11 +134,17 @@ type Site interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterSite adds an site to the engine as its own grouo
|
// RegisterSite adds an site to the engine as its own grouo
|
||||||
func (s *Server) RegisterSite(path string, site Site) {
|
func (s *Server) RegisterSite(domain, path string, site Site) {
|
||||||
site.Init(s.router.Group(path))
|
var router *gin.Engine
|
||||||
|
var found bool
|
||||||
|
if router, found = s.mrouter[domain]; !found {
|
||||||
|
router = gin.New()
|
||||||
|
s.mrouter[domain] = router
|
||||||
|
}
|
||||||
|
site.Init(router.Group(path))
|
||||||
s.sites = append(s.sites, site)
|
s.sites = append(s.sites, site)
|
||||||
if ms, ok := site.(menus.MenuSite); ok {
|
if ms, ok := site.(menus.MenuSite); ok {
|
||||||
menus := ms.Menu()
|
menus := ms.Menu(domain)
|
||||||
log.Debug().Msgf("%d menus are added", len(menus))
|
log.Debug().Msgf("%d menus are added", len(menus))
|
||||||
s.menus = append(s.menus, menus...)
|
s.menus = append(s.menus, menus...)
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,7 +37,7 @@ type Menu interface {
|
||||||
|
|
||||||
// MenuSite is an interface that is fullfilled by sites to return an type of menu.
|
// MenuSite is an interface that is fullfilled by sites to return an type of menu.
|
||||||
type MenuSite interface {
|
type MenuSite interface {
|
||||||
Menu() []Menu
|
Menu(string) []Menu
|
||||||
}
|
}
|
||||||
|
|
||||||
// MenuItem is an Implementation of the Menu Interface
|
// MenuItem is an Implementation of the Menu Interface
|
||||||
|
|
Laden…
In neuem Issue referenzieren