added a menu modul.
this module make it easier to use templating.
Dieser Commit ist enthalten in:
Ursprung
cca8fcc237
Commit
a1b11a6adb
8
http.go
8
http.go
|
@ -9,6 +9,7 @@ import (
|
|||
"github.com/pelletier/go-toml"
|
||||
"github.com/phuslu/log"
|
||||
"go.sebtobie.de/httpserver/auth"
|
||||
"go.sebtobie.de/httpserver/menus"
|
||||
)
|
||||
|
||||
// Config that is used to map the toml config to the settings that are used.
|
||||
|
@ -32,6 +33,7 @@ type Server struct {
|
|||
config *toml.Tree
|
||||
authhf auth.AuthenticationHandler
|
||||
sites []Site
|
||||
menus []menus.Menu
|
||||
}
|
||||
|
||||
// StartServer starts the server as configured and sends the errormessage to the log.
|
||||
|
@ -79,6 +81,7 @@ func CreateServer(config *toml.Tree) *Server {
|
|||
gin.DebugPrintRouteFunc = func(httpMethod, absolutePath, handlerName string, nuHandlers int) {
|
||||
log.Trace().Msgf("%-4s(%02d): %-20s %s", httpMethod, nuHandlers-1, absolutePath, handlerName)
|
||||
}
|
||||
server.menus = []menus.Menu{}
|
||||
return server
|
||||
}
|
||||
|
||||
|
@ -112,5 +115,10 @@ type Site interface {
|
|||
func (s *Server) RegisterSite(path string, site Site) {
|
||||
site.Init(s.router.Group(path))
|
||||
s.sites = append(s.sites, site)
|
||||
if ms, ok := site.(menus.MenuSite); ok {
|
||||
menus := ms.Menu()
|
||||
log.Debug().Msgf("%d menus are added", len(menus))
|
||||
s.menus = append(s.menus, menus...)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
package menus
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"go.sebtobie.de/httpserver/auth"
|
||||
)
|
||||
|
||||
// Menu is an Interface to give the server infos about menuitems
|
||||
type Menu interface {
|
||||
Name() string
|
||||
AltText() string
|
||||
URL() *url.URL
|
||||
Children() []Menu
|
||||
Enabled(auth.Account) bool
|
||||
}
|
||||
|
||||
// MenuSite is an interface that is fullfilled by sites to return an type of menu.
|
||||
type MenuSite interface {
|
||||
Menu() []Menu
|
||||
}
|
||||
|
||||
// MenuItem is an Implementation of the Menu Interface
|
||||
// Enabled returns always true
|
||||
type MenuItem struct {
|
||||
ItemName string
|
||||
ItemAltText string
|
||||
ItemURL *url.URL
|
||||
ItemChildren []Menu
|
||||
}
|
||||
|
||||
// Name returns the name of the Item.
|
||||
func (m *MenuItem) Name() string {
|
||||
return m.ItemName
|
||||
}
|
||||
|
||||
// AltText is for hovertext or Descriptions. Its can return "".
|
||||
// to make templatemaking easier i decided to make an return required.
|
||||
func (m *MenuItem) AltText() string {
|
||||
return m.ItemAltText
|
||||
}
|
||||
|
||||
// URL returns the full URL of the item.
|
||||
func (m *MenuItem) URL() *url.URL {
|
||||
return m.ItemURL
|
||||
}
|
||||
|
||||
// Children returns the ItemChildren or an empty Menu-Arry if ItemChildren is nil
|
||||
func (m *MenuItem) Children() []Menu {
|
||||
if m.ItemChildren == nil {
|
||||
return []Menu{}
|
||||
}
|
||||
return m.ItemChildren
|
||||
}
|
||||
|
||||
// Enabled returns always true
|
||||
func (*MenuItem) Enabled(auth.Account) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
var _ Menu = &MenuItem{}
|
Laden…
In neuem Issue referenzieren