catbase/plugins/cowboy/web.go

34 lines
768 B
Go
Raw Normal View History

2022-07-21 15:25:10 +00:00
package cowboy
import (
"fmt"
"net/http"
"github.com/go-chi/chi/v5"
)
func (p *Cowboy) registerWeb() {
r := chi.NewRouter()
2022-07-23 04:17:44 +00:00
r.HandleFunc("/img/{overlay}/{what}", p.handleImage)
2022-07-21 15:25:10 +00:00
p.b.RegisterWeb(r, "/cowboy")
}
func (p *Cowboy) handleImage(w http.ResponseWriter, r *http.Request) {
what := chi.URLParam(r, "what")
2022-07-23 04:17:44 +00:00
overlay := chi.URLParam(r, "overlay")
overlays := p.c.GetMap("cowboy.overlays", defaultOverlays)
overlayPath, ok := overlays[overlay]
if !ok {
w.WriteHeader(http.StatusNotFound)
return
}
img, err := encode(cowboy(p.emojyPath, p.baseEmojyURL, overlayPath, what))
2022-07-21 15:25:10 +00:00
if err != nil {
2022-07-23 04:17:44 +00:00
w.WriteHeader(http.StatusInternalServerError)
2022-07-21 15:25:10 +00:00
fmt.Fprintf(w, "Error: %s", err)
return
}
w.Header().Add("Content-Type", "image/png")
w.Write(img)
}