From 2fc9fd1c84fd9978ab67342c7d667d8b68b17df1 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Sat, 1 Jun 2013 13:39:17 -0400 Subject: [PATCH] Making index page a bit better --- bot/bot.go | 45 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index d49ad8b..773ef18 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -1,10 +1,11 @@ package bot import ( - "fmt" "github.com/chrissexton/alepale/config" irc "github.com/fluffle/goirc/client" + "html/template" "labix.org/v2/mgo" + "log" "net/http" "strings" "time" @@ -39,7 +40,7 @@ type Bot struct { Version string // The entries to the bot's HTTP interface - httpEndPoints []string + httpEndPoints map[string]string } // Log provides a slice of messages in order @@ -144,6 +145,7 @@ func NewBot(config *config.Config, c *irc.Conn) *Bot { logIn: logIn, logOut: logOut, Version: config.Version, + httpEndPoints: make(map[string]string), } http.HandleFunc("/", bot.serveRoot) @@ -160,7 +162,7 @@ func (b *Bot) AddHandler(name string, h Handler) { b.Plugins[strings.ToLower(name)] = h b.PluginOrdering = append(b.PluginOrdering, name) if entry := h.RegisterWeb(); entry != nil { - b.httpEndPoints = append(b.httpEndPoints, *entry) + b.httpEndPoints[name] = *entry } } @@ -202,8 +204,41 @@ RET: return } +var rootIndex string = ` + + + + Factoids + + + {{if .EndPoints}} +
+ + + + + + + + + {{range $key, $value := .EndPoints}} + + + + {{end}} + +
Plugin
{{$key}}
+
+ {{end}} + +` + func (b *Bot) serveRoot(w http.ResponseWriter, r *http.Request) { - for _, entry := range b.httpEndPoints { - fmt.Fprintf(w, "%s", entry, entry) + context := make(map[string]interface{}) + context["EndPoints"] = b.httpEndPoints + t, err := template.New("rootIndex").Parse(rootIndex) + if err != nil { + log.Println(err) } + t.Execute(w, context) }