1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

chore: include avatar rendering into wakapi itself

This commit is contained in:
Ferdinand Mütsch
2022-02-17 09:53:37 +01:00
parent 5cc932177f
commit 660a09475e
10 changed files with 49 additions and 9 deletions

35
routes/api/avatar.go Normal file
View File

@ -0,0 +1,35 @@
package api
import (
"codeberg.org/Codeberg/avatars"
"github.com/gorilla/mux"
conf "github.com/muety/wakapi/config"
"net/http"
)
type AvatarHandler struct {
config *conf.Config
}
func NewAvatarHandler() *AvatarHandler {
return &AvatarHandler{config: conf.Get()}
}
func (h *AvatarHandler) RegisterRoutes(router *mux.Router) {
r := router.PathPrefix("/avatar/{hash}.svg").Subrouter()
r.Path("").Methods(http.MethodGet).HandlerFunc(h.Get)
}
func (h *AvatarHandler) Get(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
if vars["hash"] == "" {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte{})
return
}
w.Header().Set("Content-Type", "image/svg+xml")
w.WriteHeader(http.StatusOK)
w.Write([]byte(avatars.MakeMaleAvatar(vars["hash"])))
}