44 lines
969 B
Go
44 lines
969 B
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
database "nefrace.ru/doomer/db"
|
|
"nefrace.ru/doomer/server"
|
|
)
|
|
|
|
func InitServer(host string) server.Api {
|
|
app := server.NewApi(host)
|
|
app.Group("/api").
|
|
Endpoint("gen", ImageHandler).
|
|
Endpoint("hello", HelloHandler)
|
|
app.Group("/").Endpoint("", HealthHandler)
|
|
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)
|
|
}
|
|
|
|
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")
|
|
}
|