chore: extract jwt from server to api

This commit is contained in:
Steven
2023-06-24 13:58:38 +08:00
parent c4388fb211
commit 0a811d2568
8 changed files with 41 additions and 48 deletions

View File

@@ -5,6 +5,7 @@ import (
"io/fs"
"net/http"
"github.com/boojack/shortify/internal/util"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
@@ -21,6 +22,11 @@ func getFileSystem(path string) http.FileSystem {
return http.FS(fs)
}
func defaultAPIRequestSkipper(c echo.Context) bool {
path := c.Path()
return util.HasPrefixes(path, "/api")
}
func embedFrontend(e *echo.Echo) {
// Use echo static middleware to serve the built dist folder
// refer: https://github.com/labstack/echo/blob/master/middleware/static.go
@@ -30,14 +36,14 @@ func embedFrontend(e *echo.Echo) {
Filesystem: getFileSystem("dist"),
}))
g := e.Group("assets")
g.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
assetsGroup := e.Group("assets")
assetsGroup.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.Response().Header().Set(echo.HeaderCacheControl, "max-age=31536000, immutable")
return next(c)
}
})
g.Use(middleware.StaticWithConfig(middleware.StaticConfig{
assetsGroup.Use(middleware.StaticWithConfig(middleware.StaticConfig{
Skipper: defaultAPIRequestSkipper,
HTML5: true,
Filesystem: getFileSystem("dist/assets"),