bot: add rate limiting

- emojy: lazy load images so they don't break/spam the server
This commit is contained in:
Chris Sexton 2022-08-04 09:17:41 -04:00
parent 7c0a777737
commit 37e4dcb5c8
2 changed files with 27 additions and 11 deletions

View File

@ -4,6 +4,8 @@ package bot
import ( import (
"fmt" "fmt"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/httprate"
"math/rand" "math/rand"
"net/http" "net/http"
"os" "os"
@ -14,7 +16,6 @@ import (
"time" "time"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/velour/catbase/bot/history" "github.com/velour/catbase/bot/history"
@ -120,21 +121,34 @@ func New(config *config.Config, connector Connector) Bot {
log.Debug().Msgf("created web router") log.Debug().Msgf("created web router")
// Make the http logger optional bot.setupHTTP()
// 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)
bot.router.HandleFunc("/nav", bot.serveNav)
connector.RegisterEvent(bot.Receive) connector.RegisterEvent(bot.Receive)
return bot return bot
} }
func (b *bot) setupHTTP() {
// 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 b.Config().GetInt("bot.useLogger", 0) == 1 {
b.router.Use(middleware.Logger)
}
reqCount := b.Config().GetInt("bot.httprate.requests", 500)
reqTime := time.Duration(b.Config().GetInt("bot.httprate.seconds", 5))
if reqCount > 0 && reqTime > 0 {
b.router.Use(httprate.LimitByIP(reqCount, reqTime*time.Second))
}
b.router.Use(middleware.RequestID)
b.router.Use(middleware.Recoverer)
b.router.Use(middleware.StripSlashes)
b.router.HandleFunc("/", b.serveRoot)
b.router.HandleFunc("/nav", b.serveNav)
}
func (b *bot) ListenAndServe() { func (b *bot) ListenAndServe() {
addr := b.config.Get("HttpAddr", "127.0.0.1:1337") addr := b.config.Get("HttpAddr", "127.0.0.1:1337")
stop := make(chan os.Signal, 1) stop := make(chan os.Signal, 1)

View File

@ -45,8 +45,10 @@
<div class="row row-cols-5"> <div class="row row-cols-5">
<div class="card text-center" v-for="name in fileKeys" key="name"> <div class="card text-center" v-for="name in fileKeys" key="name">
<img :src="fileList[name]" class="card-img-top mx-auto d-block" :alt="name" style="max-width: 100px">
<div class="card-body"> <div class="card-body">
<span>
<b-img-lazy :src="fileList[name]" class="card-img-top mx-auto d-block" :alt="name" width=100 style="max-width: 100px">
</span>
<h5 class="card-title">{{name}}</h5> <h5 class="card-title">{{name}}</h5>
</div> </div>
</div> </div>