From cf19a2bc152afd8805a2dbd93d4dd0aa33e2a9b9 Mon Sep 17 00:00:00 2001 From: Chris Sexton <3216719+chrissexton@users.noreply.github.com> Date: Fri, 22 Jul 2022 09:44:42 -0400 Subject: [PATCH] cowboy: anchor emojy pages to URLs This also refactored a bunch of junk and eliminated about 4k calls to the config/database on emojy listing. --- bot/bot.go | 6 +- plugins/cowboy/cowboy.go | 11 +- plugins/cowboy/cowboy_image.go | 17 ++-- plugins/cowboy/web.go | 2 +- plugins/emojy/emojy.go | 19 ++-- plugins/emojy/list.html | 106 +++++++++++++++++++ plugins/emojy/stats.html | 118 ++++++++++++++++++++++ plugins/emojy/{index.html => upload.html} | 34 +------ plugins/emojy/web.go | 23 +++-- 9 files changed, 278 insertions(+), 58 deletions(-) create mode 100644 plugins/emojy/list.html create mode 100644 plugins/emojy/stats.html rename plugins/emojy/{index.html => upload.html} (75%) diff --git a/bot/bot.go b/bot/bot.go index 2648565..d3cfecf 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -118,7 +118,11 @@ func New(config *config.Config, connector Connector) Bot { log.Debug().Msgf("created web router") - bot.router.Use(middleware.Logger) + // Make the http logger optional + // It has never served a purpose in production and with the emojy page, can make a rather noisy log + if bot.Config().GetInt("bot.useLogger", 0) == 1 { + bot.router.Use(middleware.Logger) + } bot.router.Use(middleware.StripSlashes) bot.router.HandleFunc("/", bot.serveRoot) diff --git a/plugins/cowboy/cowboy.go b/plugins/cowboy/cowboy.go index a377950..a8ad362 100644 --- a/plugins/cowboy/cowboy.go +++ b/plugins/cowboy/cowboy.go @@ -12,12 +12,19 @@ import ( type Cowboy struct { b bot.Bot c *config.Config + + emojyPath string + baseEmojyURL string } func New(b bot.Bot) *Cowboy { + emojyPath := b.Config().Get("emojy.path", "emojy") + baseURL := b.Config().Get("emojy.baseURL", "/emojy/file") c := Cowboy{ - b: b, - c: b.Config(), + b: b, + c: b.Config(), + emojyPath: emojyPath, + baseEmojyURL: baseURL, } c.register() c.registerWeb() diff --git a/plugins/cowboy/cowboy_image.go b/plugins/cowboy/cowboy_image.go index e72e280..8647137 100644 --- a/plugins/cowboy/cowboy_image.go +++ b/plugins/cowboy/cowboy_image.go @@ -15,8 +15,8 @@ import ( "github.com/velour/catbase/plugins/emojy" ) -func getEmojy(c *config.Config, name string) (image.Image, error) { - files, _, err := emojy.AllFiles(c) +func getEmojy(emojyPath, baseEmojyURL, name string) (image.Image, error) { + files, _, err := emojy.AllFiles(emojyPath, baseEmojyURL) if err != nil { return nil, err } @@ -35,8 +35,7 @@ func getEmojy(c *config.Config, name string) (image.Image, error) { return img, nil } -func getCowboyHat(c *config.Config) (image.Image, error) { - emojyPath := c.Get("emojy.path", "emojy") +func getCowboyHat(c *config.Config, emojyPath string) (image.Image, error) { p := path.Join(emojyPath, c.Get("cowboy.hatname", "hat.png")) p = path.Clean(p) f, err := os.Open(p) @@ -50,8 +49,8 @@ func getCowboyHat(c *config.Config) (image.Image, error) { return img, nil } -func cowboyifyImage(c *config.Config, input image.Image) (image.Image, error) { - hat, err := getCowboyHat(c) +func cowboyifyImage(c *config.Config, emojyPath string, input image.Image) (image.Image, error) { + hat, err := getCowboyHat(c, emojyPath) if err != nil { return nil, err } @@ -66,12 +65,12 @@ func cowboyifyImage(c *config.Config, input image.Image) (image.Image, error) { return dst, nil } -func cowboy(c *config.Config, name string) ([]byte, error) { - emojy, err := getEmojy(c, name) +func cowboy(c *config.Config, emojyPath, baseEmojyURL, name string) ([]byte, error) { + emojy, err := getEmojy(emojyPath, baseEmojyURL, name) if err != nil { return nil, err } - img, err := cowboyifyImage(c, emojy) + img, err := cowboyifyImage(c, emojyPath, emojy) if err != nil { return nil, err } diff --git a/plugins/cowboy/web.go b/plugins/cowboy/web.go index 39ec95b..7024d3a 100644 --- a/plugins/cowboy/web.go +++ b/plugins/cowboy/web.go @@ -15,7 +15,7 @@ func (p *Cowboy) registerWeb() { func (p *Cowboy) handleImage(w http.ResponseWriter, r *http.Request) { what := chi.URLParam(r, "what") - img, err := cowboy(p.c, what) + img, err := cowboy(p.c, p.emojyPath, p.baseEmojyURL, what) if err != nil { w.WriteHeader(500) fmt.Fprintf(w, "Error: %s", err) diff --git a/plugins/emojy/emojy.go b/plugins/emojy/emojy.go index 33f51cc..f18f9d2 100644 --- a/plugins/emojy/emojy.go +++ b/plugins/emojy/emojy.go @@ -26,16 +26,23 @@ type EmojyPlugin struct { b bot.Bot c *config.Config db *sqlx.DB + + emojyPath string + baseURL string } const maxLen = 32 func New(b bot.Bot) *EmojyPlugin { log.Debug().Msgf("emojy.New") + emojyPath := b.Config().Get("emojy.path", "emojy") + baseURL := b.Config().Get("emojy.baseURL", "/emojy/file") p := &EmojyPlugin{ - b: b, - c: b.Config(), - db: b.DB(), + b: b, + c: b.Config(), + db: b.DB(), + emojyPath: emojyPath, + baseURL: baseURL, } p.setupDB() p.register() @@ -193,11 +200,9 @@ func (p *EmojyPlugin) allCounts() (map[string][]EmojyCount, error) { return out, nil } -func AllFiles(c *config.Config) (map[string]string, map[string]string, error) { +func AllFiles(emojyPath, baseURL string) (map[string]string, map[string]string, error) { files := map[string]string{} urls := map[string]string{} - emojyPath := c.Get("emojy.path", "emojy") - baseURL := c.Get("emojy.baseURL", "/emojy/file") entries, err := os.ReadDir(emojyPath) if err != nil { return nil, nil, err @@ -214,7 +219,7 @@ func AllFiles(c *config.Config) (map[string]string, map[string]string, error) { } func (p *EmojyPlugin) isKnownEmojy(name string) (bool, string, string, error) { - allFiles, allURLs, err := AllFiles(p.c) + allFiles, allURLs, err := AllFiles(p.emojyPath, p.baseURL) if err != nil { return false, "", "", err } diff --git a/plugins/emojy/list.html b/plugins/emojy/list.html new file mode 100644 index 0000000..e8f5db4 --- /dev/null +++ b/plugins/emojy/list.html @@ -0,0 +1,106 @@ + + + + + + + + + + + + + + + + + Memes + + + +
+ + Emojys + + {{ item.name + }} + + + + + + + Stats + List + Upload + + + +
+ {{ err }} +
+ + +
+ + + + \ No newline at end of file diff --git a/plugins/emojy/stats.html b/plugins/emojy/stats.html new file mode 100644 index 0000000..4e71cfc --- /dev/null +++ b/plugins/emojy/stats.html @@ -0,0 +1,118 @@ + + + + + + + + + + + + + + + + + Memes + + + +
+ + Emojys + + {{ item.name + }} + + + + + + + Stats + List + Upload + + + +
+ {{ err }} +
+ + +
+ + + + \ No newline at end of file diff --git a/plugins/emojy/index.html b/plugins/emojy/upload.html similarity index 75% rename from plugins/emojy/index.html rename to plugins/emojy/upload.html index ce6e71a..5fd7dd9 100644 --- a/plugins/emojy/index.html +++ b/plugins/emojy/upload.html @@ -30,9 +30,9 @@ - Stats - List - Upload + Stats + List + Upload @@ -44,7 +44,7 @@ {{ err }} -
+
- - - -