diff --git a/bot/bot.go b/bot/bot.go index c11e01f..d019692 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -61,7 +61,8 @@ type bot struct { } type EndPoint struct { - Name, URL string + Name string `json:"name"` + URL string `json:"url"` } // Variable represents a $var replacement @@ -104,6 +105,7 @@ func New(config *config.Config, connector Connector) Bot { bot.RefreshPluginWhitelist() http.HandleFunc("/", bot.serveRoot) + http.HandleFunc("/nav", bot.serveNav) connector.RegisterEvent(bot.Receive) diff --git a/bot/web.go b/bot/web.go index b8e52ed..b15310d 100644 --- a/bot/web.go +++ b/bot/web.go @@ -1,16 +1,24 @@ package bot import ( - "html/template" + "encoding/json" + "fmt" "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) + fmt.Fprint(w, rootIndex) +} + +func (b *bot) serveNav(w http.ResponseWriter, r *http.Request) { + enc := json.NewEncoder(w) + err := enc.Encode(b.GetWebNavigation()) + if err != nil { + jsonErr, _ := json.Marshal(err) + w.WriteHeader(500) + w.Write(jsonErr) + } } // GetWebNavigation returns a list of bootstrap-vue links @@ -51,12 +59,12 @@ var rootIndex = `
- - catbase - - {{ "{{ item.Name }}" }} - - + + catbase + + {{ item.name }} + +
diff --git a/plugins/admin/admin.go b/plugins/admin/admin.go index 87db42e..e70a900 100644 --- a/plugins/admin/admin.go +++ b/plugins/admin/admin.go @@ -5,7 +5,6 @@ package admin import ( "encoding/json" "fmt" - "html/template" "net/http" "os" "regexp" @@ -328,10 +327,8 @@ func (p *AdminPlugin) registerWeb() { p.bot.RegisterWeb("/vars", "Variables") } -var tpl = template.Must(template.New("factoidIndex").Parse(varIndex)) - func (p *AdminPlugin) handleWeb(w http.ResponseWriter, r *http.Request) { - tpl.Execute(w, struct{ Nav []bot.EndPoint }{p.bot.GetWebNavigation()}) + fmt.Fprint(w, varIndex) } func (p *AdminPlugin) handleWebAPI(w http.ResponseWriter, r *http.Request) { diff --git a/plugins/admin/index.go b/plugins/admin/index.go index bcf5c68..207d66a 100644 --- a/plugins/admin/index.go +++ b/plugins/admin/index.go @@ -24,7 +24,7 @@ var varIndex = ` Variables - {{ "{{ item.Name }}" }} + {{ item.name }} - {{ "{{ err }}" }} + {{ err }} { + this.nav = resp.data; + }) + .catch(err => console.log(err)) }, methods: { getData: function() { diff --git a/plugins/cli/cli.go b/plugins/cli/cli.go index 8b98f51..53505f2 100644 --- a/plugins/cli/cli.go +++ b/plugins/cli/cli.go @@ -5,7 +5,6 @@ package cli import ( "encoding/json" "fmt" - "html/template" "net/http" "time" @@ -91,10 +90,8 @@ func (p *CliPlugin) handleWebAPI(w http.ResponseWriter, r *http.Request) { w.Write(data) } -var tpl = template.Must(template.New("factoidIndex").Parse(indexHTML)) - func (p *CliPlugin) handleWeb(w http.ResponseWriter, r *http.Request) { - tpl.Execute(w, struct{ Nav []bot.EndPoint }{p.bot.GetWebNavigation()}) + fmt.Fprint(w, indexHTML) } // Completing the Connector interface, but will not actually be a connector diff --git a/plugins/cli/index.go b/plugins/cli/index.go index 63e9079..be228bc 100644 --- a/plugins/cli/index.go +++ b/plugins/cli/index.go @@ -24,14 +24,14 @@ var indexHTML = ` CLI - {{ "{{ item.Name }}" }} + {{ item.name }} - {{ "{{ err }}" }} + {{ err }} @@ -80,13 +80,20 @@ var indexHTML = ` el: '#app', data: { err: '', - nav: {{ .Nav }}, + nav: [], answer: '', correct: 0, textarea: [], user: '', input: '', }, + mounted: function() { + axios.get('/nav') + .then(resp => { + this.nav = resp.data; + }) + .catch(err => console.log(err)) + }, computed: { authenticated: function() { if (this.user !== '') diff --git a/plugins/counter/counter.go b/plugins/counter/counter.go index 0b30ea7..b54614a 100644 --- a/plugins/counter/counter.go +++ b/plugins/counter/counter.go @@ -4,7 +4,6 @@ import ( "database/sql" "encoding/json" "fmt" - "html/template" "math/rand" "net/http" "regexp" @@ -626,10 +625,8 @@ func (p *CounterPlugin) registerWeb() { p.Bot.RegisterWeb("/counter", "Counter") } -var tpl = template.Must(template.New("factoidIndex").Parse(html)) - func (p *CounterPlugin) handleCounter(w http.ResponseWriter, r *http.Request) { - tpl.Execute(w, struct{ Nav []bot.EndPoint }{p.Bot.GetWebNavigation()}) + fmt.Fprint(w, html) } func (p *CounterPlugin) handleCounterAPI(w http.ResponseWriter, r *http.Request) { diff --git a/plugins/counter/html.go b/plugins/counter/html.go index dd75703..f2de8d6 100644 --- a/plugins/counter/html.go +++ b/plugins/counter/html.go @@ -22,14 +22,14 @@ var html = ` Counters - {{ "{{ item.Name }}" }} + {{ item.name }} - {{ "{{ err }}" }} + {{ err }} @@ -37,14 +37,14 @@ var html = ` - {{ "{{ user }}" }}: + {{ user }}: - {{ "{{ thing }}" }}: + {{ thing }}: - {{ "{{ count }}" }} + {{ count }} @@ -72,12 +72,17 @@ var html = ` el: '#app', data: { err: '', - nav: {{ .Nav }}, + nav: [], answer: '', correct: 0, counters: {} }, mounted() { + axios.get('/nav') + .then(resp => { + this.nav = resp.data; + }) + .catch(err => console.log(err)) axios.get('/counter/api') .then(resp => (this.counters = convertData(resp.data))) .catch(err => (this.err = err)); diff --git a/plugins/fact/factoid.go b/plugins/fact/factoid.go index 769ada3..42bc075 100644 --- a/plugins/fact/factoid.go +++ b/plugins/fact/factoid.go @@ -842,8 +842,6 @@ func (p *FactoidPlugin) serveAPI(w http.ResponseWriter, r *http.Request) { w.Write(data) } -var tpl = template.Must(template.New("factoidIndex").Parse(factoidIndex)) - func (p *FactoidPlugin) serveQuery(w http.ResponseWriter, r *http.Request) { - tpl.Execute(w, struct{ Nav []bot.EndPoint }{p.Bot.GetWebNavigation()}) + fmt.Fprint(w, factoidIndex) } diff --git a/plugins/fact/webTemplates.go b/plugins/fact/webTemplates.go index 3027235..fd75fb5 100644 --- a/plugins/fact/webTemplates.go +++ b/plugins/fact/webTemplates.go @@ -32,7 +32,7 @@ var factoidIndex = ` Factoids - {{ "{{ item.Name }}" }} + {{ item.name }} - {{ "{{ err }}" }} + {{ err }} @@ -74,7 +74,7 @@ var factoidIndex = ` router, data: { err: '', - nav: {{ .Nav }}, + nav: [], query: '', results: [], fields: [ @@ -85,6 +85,11 @@ var factoidIndex = ` ] }, mounted() { + axios.get('/nav') + .then(resp => { + this.nav = resp.data; + }) + .catch(err => console.log(err)) if (this.$route.query.query) { this.query = this.$route.query.query; this.runQuery() diff --git a/plugins/git/git.go b/plugins/git/git.go index d5c0231..4578307 100644 --- a/plugins/git/git.go +++ b/plugins/git/git.go @@ -80,5 +80,4 @@ func (p *GitPlugin) registerWeb() { http.HandleFunc("/git/gitea/event", p.giteaEvent) http.HandleFunc("/git/github/event", p.githubEvent) http.HandleFunc("/git/gitlab/event", p.gitlabEvent) - p.b.RegisterWeb("/git", "Git") } diff --git a/plugins/meme/web.go b/plugins/meme/web.go index 3a799e0..0585a0e 100644 --- a/plugins/meme/web.go +++ b/plugins/meme/web.go @@ -25,7 +25,7 @@ var memeIndex = ` Memes - {{ "{{ item.Name }}" }} + {{ item.name }} - {{ "{{ err }}" }} + {{ err }} @@ -58,7 +58,7 @@ var memeIndex = ` :items="results" :fields="fields">