doomer/webapi.go

44 lines
969 B
Go
Raw Normal View History

2022-12-06 02:04:05 +03:00
package main
import (
"net/http"
database "nefrace.ru/doomer/db"
"nefrace.ru/doomer/server"
)
func InitServer(host string) server.Api {
app := server.NewApi(host)
2022-12-06 14:26:47 +03:00
app.Group("/api").
Endpoint("gen", ImageHandler).
Endpoint("hello", HelloHandler)
app.Group("/").Endpoint("", HealthHandler)
2022-12-06 02:04:05 +03:00
return app
}
func ImageHandler(w http.ResponseWriter, r *http.Request) error {
db := database.DB()
doomer := NewDoomer(db)
text := r.URL.Query().Get("text")
img, err := doomer.GetImage(text, Black)
if err != nil {
return server.ApiError{
Err: "cannot generate image: " + err.Error(),
Status: http.StatusInternalServerError,
}
}
return server.WriteBlob(w, 200, "image/png", *img)
}
2022-12-06 14:26:47 +03:00
type hello struct {
Hello string
}
func HelloHandler(w http.ResponseWriter, r *http.Request) error {
return server.WriteJSON(w, 200, hello{"world"})
}
func HealthHandler(w http.ResponseWriter, r *http.Request) error {
return server.WritePlain(w, 200, "ok")
}