From 9ea45f0ad3d318ca50c364706791dd87cbfa6fee Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Sun, 9 Jun 2019 00:15:06 -0400 Subject: [PATCH] web: add a menu and redo index We can add arbitrary links now with the `bot.links` config --- bot/bot.go | 51 ++++--------------------- bot/interfaces.go | 1 + bot/mock.go | 1 + bot/web.go | 73 ++++++++++++++++++++++++++++++++++++ plugins/admin/admin.go | 5 ++- plugins/admin/index.go | 12 ++++-- plugins/cli/cli.go | 5 ++- plugins/cli/index.go | 12 ++++-- plugins/counter/counter.go | 7 ++-- plugins/counter/html.go | 20 ++++++---- plugins/fact/factoid.go | 4 +- plugins/fact/webTemplates.go | 10 ++++- 12 files changed, 136 insertions(+), 65 deletions(-) create mode 100644 bot/web.go diff --git a/bot/bot.go b/bot/bot.go index 8674a34..c313457 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -3,7 +3,6 @@ package bot import ( - "html/template" "net/http" "reflect" "strings" @@ -38,7 +37,7 @@ type bot struct { version string // The entries to the bot's HTTP interface - httpEndPoints map[string]string + httpEndPoints []EndPoint // filters registered by plugins filters map[string]func(string) string @@ -46,6 +45,10 @@ type bot struct { callbacks CallbackMap } +type EndPoint struct { + Name, URL string +} + // Variable represents a $var replacement type Variable struct { Variable, Value string @@ -73,7 +76,7 @@ func New(config *config.Config, connector Connector) Bot { me: users[0], logIn: logIn, logOut: logOut, - httpEndPoints: make(map[string]string), + httpEndPoints: make([]EndPoint, 0), filters: make(map[string]func(string) string), callbacks: make(CallbackMap), } @@ -133,46 +136,6 @@ func (b *bot) Who(channel string) []user.User { return users } -var rootIndex = ` - - - - Factoids - - - - {{if .EndPoints}} -
- - - - - - - - - {{range $key, $value := .EndPoints}} - - - - {{end}} - -
Plugin
{{$key}}
-
- {{end}} - -` - -func (b *bot) serveRoot(w http.ResponseWriter, r *http.Request) { - context := make(map[string]interface{}) - context["EndPoints"] = b.httpEndPoints - t, err := template.New("rootIndex").Parse(rootIndex) - if err != nil { - log.Error().Err(err) - } - t.Execute(w, context) -} - // IsCmd checks if message is a command and returns its curtailed version func IsCmd(c *config.Config, message string) (bool, string) { cmdcs := c.GetArray("CommandChar", []string{"!"}) @@ -266,5 +229,5 @@ func (b *bot) Register(p Plugin, kind Kind, cb Callback) { } func (b *bot) RegisterWeb(root, name string) { - b.httpEndPoints[name] = root + b.httpEndPoints = append(b.httpEndPoints, EndPoint{name, root}) } diff --git a/bot/interfaces.go b/bot/interfaces.go index fba9396..d9d10d3 100644 --- a/bot/interfaces.go +++ b/bot/interfaces.go @@ -66,6 +66,7 @@ type Bot interface { RegisterFilter(string, func(string) string) RegisterWeb(string, string) DefaultConnector() Connector + GetWebNavigation() []EndPoint } // Connector represents a server connection to a chat service diff --git a/bot/mock.go b/bot/mock.go index 6f67c62..3511cf1 100644 --- a/bot/mock.go +++ b/bot/mock.go @@ -52,6 +52,7 @@ func (mb *MockBot) Send(c Connector, kind Kind, args ...interface{}) (string, er func (mb *MockBot) AddPlugin(f Plugin) {} func (mb *MockBot) Register(p Plugin, kind Kind, cb Callback) {} func (mb *MockBot) RegisterWeb(_, _ string) {} +func (mb *MockBot) GetWebNavigation() []EndPoint { return nil } func (mb *MockBot) Receive(c Connector, kind Kind, msg msg.Message, args ...interface{}) bool { return false } diff --git a/bot/web.go b/bot/web.go new file mode 100644 index 0000000..bbe62ec --- /dev/null +++ b/bot/web.go @@ -0,0 +1,73 @@ +package bot + +import ( + "html/template" + "net/http" + "strings" +) + +func (b *bot) serveRoot(w http.ResponseWriter, r *http.Request) { + context := make(map[string]interface{}) + context["Nav"] = b.GetWebNavigation() + t := template.Must(template.New("rootIndex").Parse(rootIndex)) + t.Execute(w, context) +} + +// GetWebNavigation returns a list of bootstrap-vue links +// The parent