memos/server/webhook.go

44 lines
1.2 KiB
Go
Raw Normal View History

2022-02-03 10:32:03 +03:00
package server
import (
"fmt"
"net/http"
"strconv"
2022-06-27 17:09:06 +03:00
"github.com/usememos/memos/api"
2022-02-03 10:32:03 +03:00
"github.com/labstack/echo/v4"
)
func (s *Server) registerWebhookRoutes(g *echo.Group) {
g.GET("/test", func(c echo.Context) error {
return c.HTML(http.StatusOK, "<strong>Hello, World!</strong>")
})
2022-02-18 17:21:10 +03:00
2022-02-04 13:54:24 +03:00
g.GET("/r/:resourceId/:filename", func(c echo.Context) error {
2022-08-07 05:17:12 +03:00
ctx := c.Request().Context()
2022-05-02 21:05:43 +03:00
resourceID, err := strconv.Atoi(c.Param("resourceId"))
2022-02-03 10:32:03 +03:00
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("resourceId"))).SetInternal(err)
}
filename := c.Param("filename")
resourceFind := &api.ResourceFind{
2022-05-02 21:05:43 +03:00
ID: &resourceID,
2022-02-03 10:32:03 +03:00
Filename: &filename,
}
2022-08-07 05:17:12 +03:00
resource, err := s.Store.FindResource(ctx, resourceFind)
2022-02-03 10:32:03 +03:00
if err != nil {
2022-05-02 21:05:43 +03:00
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch resource ID: %v", resourceID)).SetInternal(err)
2022-02-03 10:32:03 +03:00
}
c.Response().Writer.WriteHeader(http.StatusOK)
2022-03-29 02:30:29 +03:00
c.Response().Writer.Header().Set("Content-Type", resource.Type)
if _, err := c.Response().Writer.Write(resource.Blob); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to write response").SetInternal(err)
}
2022-02-03 10:32:03 +03:00
return nil
})
}