feat: add shortcut favicon

This commit is contained in:
Steven
2023-06-27 23:13:28 +08:00
parent c8751df012
commit 2a9d601df2
6 changed files with 155 additions and 3 deletions

29
api/v1/favicon.go Normal file
View File

@ -0,0 +1,29 @@
package v1
import (
"net/http"
"github.com/labstack/echo/v4"
"go.deanishe.net/favicon"
)
func (*APIV1Service) registerFaviconRoutes(g *echo.Group) {
g.GET("/favicon", func(c echo.Context) error {
url := c.QueryParam("url")
icons, err := favicon.Find(url)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find favicon")
}
availableIcons := []*favicon.Icon{}
for _, icon := range icons {
if icon.Width == icon.Height {
availableIcons = append(availableIcons, icon)
}
}
if len(availableIcons) == 0 {
return echo.NewHTTPError(http.StatusNotFound, "No favicon found")
}
return c.JSON(http.StatusOK, availableIcons[0].URL)
})
}

View File

@ -24,6 +24,7 @@ func (s *APIV1Service) Start(apiGroup *echo.Group, secret string) {
apiV1Group.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return JWTMiddleware(s, next, secret)
})
s.registerFaviconRoutes(apiV1Group)
s.registerWorkspaceRoutes(apiV1Group)
s.registerAuthRoutes(apiV1Group, secret)
s.registerUserRoutes(apiV1Group)