-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
routes.go
40 lines (33 loc) · 1.17 KB
/
routes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"log/slog"
"net/http"
"github.com/labstack/echo/v4"
)
func (app *application) addRoutes(e *echo.Echo) {
e.GET("/test_panic", app.handleTestPanic)
e.GET("/test_notifications", app.handleTestNotification)
e.POST("/convert", func(c echo.Context) error {
type jsonData struct {
Input []byte `json:"input"`
Resources map[string][]byte `json:"resources"`
Template string `json:"template"`
}
type jsonResponse struct {
Content []byte `json:"content"`
}
var d jsonData
if err := c.Bind(&d); err != nil {
return c.JSON(http.StatusBadRequest, newEchoJsonError(err, http.StatusBadRequest, "invalid input"))
}
if d.Input == nil || d.Template == "" {
return c.JSON(http.StatusBadRequest, echo.NewHTTPError(http.StatusBadRequest, "invalid input"))
}
bin, err := app.convert(c.Request().Context(), d.Input, d.Resources, d.Template)
if err != nil {
app.logger.Error("error on convert", slog.String("error", err.Error()))
return c.JSON(http.StatusBadRequest, newEchoJsonError(err, http.StatusBadRequest, "error converting markdown"))
}
return c.JSON(http.StatusOK, jsonResponse{Content: bin})
})
}