From a1b11a6adb85b2a77eee6538f9fbf9e0172bf00d Mon Sep 17 00:00:00 2001 From: Sebastian Tobie Date: Tue, 19 Jan 2021 23:44:35 +0100 Subject: [PATCH] added a menu modul. this module make it easier to use templating. --- http.go | 8 +++++++ menus/menus.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 menus/menus.go diff --git a/http.go b/http.go index 48cc766..a497475 100644 --- a/http.go +++ b/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 } diff --git a/menus/menus.go b/menus/menus.go new file mode 100644 index 0000000..dbcd930 --- /dev/null +++ b/menus/menus.go @@ -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{}