From 02619882d1e376a1c426df8a372b9621d5eaf7db Mon Sep 17 00:00:00 2001 From: steven Date: Fri, 30 Sep 2022 08:41:35 +0800 Subject: [PATCH] feat: add redirect api --- server/acl.go | 2 +- server/embed_frontend.go | 2 +- server/redirect.go | 52 ++++++++++++++++++++++++++++++++++++++++ server/server.go | 6 +++++ server/shortcut.go | 1 + web/vite.config.ts | 4 ++++ 6 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 server/redirect.go diff --git a/server/acl.go b/server/acl.go index 222f355..06892de 100644 --- a/server/acl.go +++ b/server/acl.go @@ -104,7 +104,7 @@ func aclMiddleware(s *Server, next echo.HandlerFunc) echo.HandlerFunc { } } - if common.HasPrefixes(path, "/api/workspace/:workspaceName/shortcut/:shortcutName") && c.Request().Method == http.MethodGet { + if common.HasPrefixes(path, "/o/:shortcutName", "/api/workspace/:workspaceName/shortcut/:shortcutName") && c.Request().Method == http.MethodGet { return next(c) } diff --git a/server/embed_frontend.go b/server/embed_frontend.go index f934bac..c102894 100644 --- a/server/embed_frontend.go +++ b/server/embed_frontend.go @@ -24,7 +24,7 @@ func getFileSystem(path string) http.FileSystem { func skipper(c echo.Context) bool { path := c.Path() - return common.HasPrefixes(path, "/api") + return common.HasPrefixes(path, "/api", "/o") } func embedFrontend(e *echo.Echo) { diff --git a/server/redirect.go b/server/redirect.go new file mode 100644 index 0000000..ca82eed --- /dev/null +++ b/server/redirect.go @@ -0,0 +1,52 @@ +package server + +import ( + "net/http" + + "github.com/boojack/corgi/api" + "github.com/labstack/echo/v4" +) + +func (s *Server) registerRedirectRoutes(g *echo.Group) { + g.GET("/:shortcutName", func(c echo.Context) error { + ctx := c.Request().Context() + userID, ok := c.Get(getUserIDContextKey()).(int) + if !ok { + return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session") + } + shortcutName := c.Param("shortcutName") + if shortcutName == "" { + return echo.NewHTTPError(http.StatusBadRequest, "Missing shortcut name") + } + + list := []*api.Shortcut{} + + shortcutFind := &api.ShortcutFind{ + Name: &shortcutName, + MemberID: &userID, + } + shortcutFind.VisibilityList = []api.Visibility{api.VisibilityWorkspace, api.VisibilityPublic} + visibleShortcutList, err := s.Store.FindShortcutList(ctx, shortcutFind) + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch shortcut list").SetInternal(err) + } + list = append(list, visibleShortcutList...) + + shortcutFind.VisibilityList = []api.Visibility{api.VisibilityPrivite} + shortcutFind.CreatorID = &userID + privateShortcutList, err := s.Store.FindShortcutList(ctx, shortcutFind) + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch private shortcut list").SetInternal(err) + } + list = append(list, privateShortcutList...) + + if len(list) == 0 { + return echo.NewHTTPError(http.StatusNotFound, "Not found shortcut").SetInternal(err) + } + + // TODO(steven): improve the matched result later + matchedShortcut := list[0] + return c.Redirect(http.StatusPermanentRedirect, matchedShortcut.Link) + }) + +} diff --git a/server/server.go b/server/server.go index edc8687..fd603ad 100644 --- a/server/server.go +++ b/server/server.go @@ -56,6 +56,12 @@ func NewServer(profile *profile.Profile) *Server { Profile: profile, } + redirectGroup := e.Group("/o") + redirectGroup.Use(func(next echo.HandlerFunc) echo.HandlerFunc { + return aclMiddleware(s, next) + }) + s.registerRedirectRoutes(redirectGroup) + apiGroup := e.Group("/api") apiGroup.Use(func(next echo.HandlerFunc) echo.HandlerFunc { return aclMiddleware(s, next) diff --git a/server/shortcut.go b/server/shortcut.go index 10656d9..248059e 100644 --- a/server/shortcut.go +++ b/server/shortcut.go @@ -91,6 +91,7 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) { } list := []*api.Shortcut{} + if shortcutFind.WorkspaceID == nil { shortcutFind.MemberID = &userID } diff --git a/web/vite.config.ts b/web/vite.config.ts index 9c6b728..18e9944 100644 --- a/web/vite.config.ts +++ b/web/vite.config.ts @@ -11,6 +11,10 @@ export default defineConfig({ target: "http://localhost:8081/", changeOrigin: true, }, + "/o": { + target: "http://localhost:8081/", + changeOrigin: true, + }, }, }, });