memos/server/webhook.go

43 lines
1.2 KiB
Go
Raw Normal View History

2022-02-03 15:32:03 +08:00
package server
import (
"fmt"
"net/http"
"strconv"
2022-06-27 22:09:06 +08:00
"github.com/usememos/memos/api"
2022-02-03 15:32:03 +08: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 22:21:10 +08:00
2022-02-04 18:54:24 +08:00
g.GET("/r/:resourceId/:filename", func(c echo.Context) error {
2022-05-03 02:05:43 +08:00
resourceID, err := strconv.Atoi(c.Param("resourceId"))
2022-02-03 15:32:03 +08: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-03 02:05:43 +08:00
ID: &resourceID,
2022-02-03 15:32:03 +08:00
Filename: &filename,
}
2022-05-16 07:37:23 +08:00
resource, err := s.Store.FindResource(resourceFind)
2022-02-03 15:32:03 +08:00
if err != nil {
2022-05-03 02:05:43 +08:00
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch resource ID: %v", resourceID)).SetInternal(err)
2022-02-03 15:32:03 +08:00
}
c.Response().Writer.WriteHeader(http.StatusOK)
2022-03-29 07:30:29 +08: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 15:32:03 +08:00
return nil
})
}