Compare commits

..

No commits in common. "bfd50a346d5f67210a5a840594864d36443cbc3d" and "3dc8c77505e6a87d28cfcdb059e117fe0267cdfb" have entirely different histories.

6 changed files with 73 additions and 96 deletions

View File

@ -147,8 +147,6 @@ func (b *bot) setupHTTP() {
b.router.HandleFunc("/", b.serveRoot)
b.router.HandleFunc("/nav", b.serveNav)
b.router.HandleFunc("/navHTML", b.serveNavHTML)
b.router.HandleFunc("/navHTML/{currentPage}", b.serveNavHTML)
}
func (b *bot) ListenAndServe() {

View File

@ -1,22 +0,0 @@
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="/">catbase</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
{{- $currentPage := .CurrentPage -}}
{{range .Items}}
<li class="nav-item">
{{ if (eq $currentPage .Name) }}
<a class="nav-link active" aria-current="page" href="{{.URL}}">{{.Name}}</a>
{{ else }}
<a class="nav-link" href="{{.URL}}">{{.Name}}</a>
{{ end }}
</li>
{{end}}
</ul>
</div>
</div>
</nav>

View File

@ -3,12 +3,8 @@ package bot
import (
"embed"
"encoding/json"
"fmt"
"github.com/go-chi/chi/v5"
"github.com/rs/zerolog/log"
"net/http"
"strings"
"text/template"
)
//go:embed *.html
@ -19,19 +15,6 @@ func (b *bot) serveRoot(w http.ResponseWriter, r *http.Request) {
w.Write(index)
}
func (b *bot) serveNavHTML(w http.ResponseWriter, r *http.Request) {
currentPage := chi.URLParam(r, "currentPage")
tpl := template.Must(template.ParseFS(embeddedFS, "nav.html"))
if err := tpl.Execute(w, struct {
CurrentPage string
Items []EndPoint
}{currentPage, b.GetWebNavigation()}); err != nil {
log.Error().Err(err).Msg("template error")
w.WriteHeader(500)
fmt.Fprint(w, "Error parsing nav template")
}
}
func (b *bot) serveNav(w http.ResponseWriter, r *http.Request) {
enc := json.NewEncoder(w)
err := enc.Encode(b.GetWebNavigation())

View File

@ -1,36 +1,77 @@
<!doctype html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>vars</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">
<script src="https://unpkg.com/htmx.org@1.9.4"></script>
<!-- Load required Bootstrap and BootstrapVue CSS -->
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css"/>
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap-vue@^2/dist/bootstrap-vue.min.css"/>
<!-- Load polyfills to support older browsers -->
<script src="//polyfill.io/v3/polyfill.min.js?features=es2015%2CMutationObserver"></script>
<!-- Load Vue followed by BootstrapVue -->
<script src="//unpkg.com/vue@^2/dist/vue.min.js"></script>
<script src="//unpkg.com/bootstrap-vue@^2/dist/bootstrap-vue.min.js"></script>
<script src="//unpkg.com/axios/dist/axios.min.js"></script>
<meta charset="UTF-8">
<title>Vars</title>
</head>
<body>
<div hx-get="/navHTML/Variables" hx-trigger="load" hx-swap="outerHTML"></div>
<div class="container">
<table class="table-responsive table-striped">
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
{{range .Items}}
<tr>
<td>{{.Key}}</td><td>{{.Value}}</td>
</tr>
{{else}}
<tr>
<td colspan="2">No data</td>
</tr>
{{end}}
</tbody>
</table>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm" crossorigin="anonymous"></script>
<div id="app">
<b-navbar>
<b-navbar-brand>Variables</b-navbar-brand>
<b-navbar-nav>
<b-nav-item v-for="item in nav" :href="item.url" :active="item.name === 'Variables'">{{ item.name }}
</b-nav-item>
</b-navbar-nav>
</b-navbar>
<b-alert
dismissable
variant="error"
v-if="err"
@dismissed="err = ''">
{{ err }}
</b-alert>
<b-container>
<b-table
fixed
:items="vars"
:sort-by.sync="sortBy"
:fields="fields"></b-table>
</b-container>
</div>
<script>
var app = new Vue({
el: '#app',
data: {
err: '',
nav: [],
vars: [],
sortBy: 'key',
fields: [
{key: {sortable: true}},
'value'
]
},
mounted() {
this.getData();
axios.get('/nav')
.then(resp => {
this.nav = resp.data;
})
.catch(err => console.log(err))
},
methods: {
getData: function () {
axios.get('/vars/api')
.then(resp => {
this.vars = resp.data;
})
.catch(err => this.err = err);
}
}
})
</script>
</body>
</html>

View File

@ -6,7 +6,6 @@ import (
"embed"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"strings"
@ -167,32 +166,9 @@ func writeErr(w http.ResponseWriter, err error) {
fmt.Fprint(w, string(j))
}
type configEntry struct {
Key string `json:"key"`
Value string `json:"value"`
}
func (p *AdminPlugin) handleVars(w http.ResponseWriter, r *http.Request) {
tpl := template.Must(template.ParseFS(embeddedFS, "vars.html"))
var configEntries []configEntry
q := `select key, value from config`
err := p.db.Select(&configEntries, q)
if err != nil {
log.Error().
Err(err).
Msg("Error getting config entries.")
w.WriteHeader(500)
fmt.Fprint(w, err)
return
}
if err := tpl.Execute(w, struct {
Items []configEntry
}{configEntries}); err != nil {
log.Error().Err(err).Msg("template error")
w.WriteHeader(500)
fmt.Fprint(w, "Error parsing template")
}
index, _ := embeddedFS.ReadFile("vars.html")
w.Write(index)
}
func (p *AdminPlugin) handleVarsAPI(w http.ResponseWriter, r *http.Request) {

View File

@ -144,6 +144,7 @@ func (p *FactoidPlugin) findTrigger(fact string) (bool, *Factoid) {
f, err := GetSingleFact(p.db, fact)
if err != nil {
log.Error().Err(err).Msg("GetSingleFact")
return findAlias(p.db, fact)
}
return true, f