1
0
Fork 0

added a menu modul.

this module make it easier to use templating.
Dieser Commit ist enthalten in:
Sebastian Tobie 2021-01-19 23:44:35 +01:00
Ursprung cca8fcc237
Commit a1b11a6adb
2 geänderte Dateien mit 69 neuen und 0 gelöschten Zeilen

Datei anzeigen

@ -9,6 +9,7 @@ import (
"github.com/pelletier/go-toml" "github.com/pelletier/go-toml"
"github.com/phuslu/log" "github.com/phuslu/log"
"go.sebtobie.de/httpserver/auth" "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. // 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 config *toml.Tree
authhf auth.AuthenticationHandler authhf auth.AuthenticationHandler
sites []Site sites []Site
menus []menus.Menu
} }
// 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.
@ -79,6 +81,7 @@ func CreateServer(config *toml.Tree) *Server {
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-1, absolutePath, handlerName) log.Trace().Msgf("%-4s(%02d): %-20s %s", httpMethod, nuHandlers-1, absolutePath, handlerName)
} }
server.menus = []menus.Menu{}
return server return server
} }
@ -112,5 +115,10 @@ type Site interface {
func (s *Server) RegisterSite(path string, site Site) { func (s *Server) RegisterSite(path string, site Site) {
site.Init(s.router.Group(path)) site.Init(s.router.Group(path))
s.sites = append(s.sites, site) 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 return
} }

61
menus/menus.go Normale Datei
Datei anzeigen

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