mirror of https://github.com/velour/catbase.git
37 lines
830 B
Go
37 lines
830 B
Go
|
package gifmeme
|
||
|
|
||
|
import (
|
||
|
"compress/gzip"
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"github.com/go-chi/chi/v5"
|
||
|
"github.com/gorilla/handlers"
|
||
|
"github.com/rs/zerolog/log"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
func (p *Plugin) registerWeb() {
|
||
|
r := chi.NewRouter()
|
||
|
r.Handle("/api/gif/{id}.gif", handlers.CompressHandlerLevel(http.HandlerFunc(p.handleGif), gzip.BestCompression))
|
||
|
p.b.RegisterWeb(r, "/gifmeme")
|
||
|
}
|
||
|
|
||
|
func (p *Plugin) handleGif(w http.ResponseWriter, r *http.Request) {
|
||
|
//id := r.URL.Query().Get("id")
|
||
|
id := chi.URLParam(r, "id")
|
||
|
gif, ok := p.gifs[id]
|
||
|
keys := []string{}
|
||
|
for k, _ := range p.gifs {
|
||
|
keys = append(keys, k)
|
||
|
}
|
||
|
log.Debug().Msgf("Looking for %s in gifs: %v", id, keys)
|
||
|
if !ok {
|
||
|
w.WriteHeader(404)
|
||
|
e := struct {
|
||
|
error error
|
||
|
}{fmt.Errorf("%s not found", id)}
|
||
|
jsonErr, _ := json.Marshal(e)
|
||
|
w.Write(jsonErr)
|
||
|
}
|
||
|
w.Write(gif)
|
||
|
}
|