chore(go): use json instead of jsonapi

This commit is contained in:
email
2022-02-04 16:51:48 +08:00
parent a8f0c9a7b1
commit d6418f5ff9
16 changed files with 131 additions and 109 deletions

View File

@@ -1,19 +1,19 @@
package server
import (
"encoding/json"
"fmt"
"memos/api"
"memos/common"
"net/http"
"github.com/google/jsonapi"
"github.com/labstack/echo/v4"
)
func (s *Server) registerAuthRoutes(g *echo.Group) {
g.POST("/auth/login", func(c echo.Context) error {
login := &api.Login{}
if err := jsonapi.UnmarshalPayload(c.Request().Body, login); err != nil {
if err := json.NewDecoder(c.Request().Body).Decode(login); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted login request").SetInternal(err)
}
@@ -40,7 +40,7 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, user); err != nil {
if err := json.NewEncoder(c.Response().Writer).Encode(user); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal create user response").SetInternal(err)
}
@@ -57,7 +57,7 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
})
g.POST("/auth/signup", func(c echo.Context) error {
signup := &api.Signup{}
if err := jsonapi.UnmarshalPayload(c.Request().Body, signup); err != nil {
if err := json.NewDecoder(c.Request().Body).Decode(signup); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted signup request").SetInternal(err)
}
@@ -88,7 +88,7 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, user); err != nil {
if err := json.NewEncoder(c.Response().Writer).Encode(user); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal create user response").SetInternal(err)
}

View File

@@ -1,13 +1,13 @@
package server
import (
"encoding/json"
"fmt"
"memos/api"
"memos/common"
"net/http"
"strconv"
"github.com/google/jsonapi"
"github.com/labstack/echo/v4"
)
@@ -17,7 +17,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
memoCreate := &api.MemoCreate{
CreatorId: userId,
}
if err := jsonapi.UnmarshalPayload(c.Request().Body, memoCreate); err != nil {
if err := json.NewDecoder(c.Request().Body).Decode(memoCreate); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo request").SetInternal(err)
}
@@ -27,7 +27,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, memo); err != nil {
if err := json.NewEncoder(c.Response().Writer).Encode(memo); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal memo response").SetInternal(err)
}
@@ -42,7 +42,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
memoPatch := &api.MemoPatch{
Id: memoId,
}
if err := jsonapi.UnmarshalPayload(c.Request().Body, memoPatch); err != nil {
if err := json.NewDecoder(c.Request().Body).Decode(memoPatch); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch memo request").SetInternal(err)
}
@@ -52,7 +52,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, memo); err != nil {
if err := json.NewEncoder(c.Response().Writer).Encode(memo); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal memo response").SetInternal(err)
}
@@ -69,7 +69,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, list); err != nil {
if err := json.NewEncoder(c.Response().Writer).Encode(list); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal memo list response").SetInternal(err)
}
@@ -93,7 +93,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, memo); err != nil {
if err := json.NewEncoder(c.Response().Writer).Encode(memo); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal memo response").SetInternal(err)
}

View File

@@ -1,13 +1,13 @@
package server
import (
"encoding/json"
"fmt"
"io/ioutil"
"memos/api"
"net/http"
"strconv"
"github.com/google/jsonapi"
"github.com/labstack/echo/v4"
)
@@ -53,7 +53,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, resource); err != nil {
if err := json.NewEncoder(c.Response().Writer).Encode(resource); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal shortcut response").SetInternal(err)
}
@@ -70,7 +70,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, list); err != nil {
if err := json.NewEncoder(c.Response().Writer).Encode(list); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal resource list response").SetInternal(err)
}

View File

@@ -29,9 +29,10 @@ func NewServer() *Server {
e.HidePort = false
e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
Root: "web/dist",
Browse: false,
HTML5: true,
Skipper: middleware.DefaultSkipper,
Root: "web/dist",
Browse: false,
HTML5: true,
}))
e.Use(session.Middleware(sessions.NewCookieStore([]byte(common.GenUUID()))))

View File

@@ -1,13 +1,12 @@
package server
import (
"encoding/json"
"fmt"
"memos/api"
"net/http"
"strconv"
"time"
"github.com/google/jsonapi"
"github.com/labstack/echo/v4"
)
@@ -17,7 +16,7 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
shortcutCreate := &api.ShortcutCreate{
CreatorId: userId,
}
if err := jsonapi.UnmarshalPayload(c.Request().Body, shortcutCreate); err != nil {
if err := json.NewDecoder(c.Request().Body).Decode(shortcutCreate); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post shortcut request").SetInternal(err)
}
@@ -27,7 +26,7 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, shortcut); err != nil {
if err := json.NewEncoder(c.Response().Writer).Encode(shortcut); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal shortcut response").SetInternal(err)
}
@@ -42,25 +41,17 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
shortcutPatch := &api.ShortcutPatch{
Id: shortcutId,
}
if err := jsonapi.UnmarshalPayload(c.Request().Body, shortcutPatch); err != nil {
if err := json.NewDecoder(c.Request().Body).Decode(shortcutPatch); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch shortcut request").SetInternal(err)
}
if shortcutPatch.Pinned != nil {
pinnedTs := int64(0)
if *shortcutPatch.Pinned {
pinnedTs = time.Now().Unix()
}
shortcutPatch.PinnedTs = &pinnedTs
}
shortcut, err := s.ShortcutService.PatchShortcut(shortcutPatch)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch shortcut").SetInternal(err)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, shortcut); err != nil {
if err := json.NewEncoder(c.Response().Writer).Encode(shortcut); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal shortcut response").SetInternal(err)
}
@@ -71,13 +62,13 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
shortcutFind := &api.ShortcutFind{
CreatorId: &userId,
}
list, err := s.ShortcutService.FindShortcut(shortcutFind)
list, err := s.ShortcutService.FindShortcutList(shortcutFind)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch shortcut list").SetInternal(err)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, list); err != nil {
if err := json.NewEncoder(c.Response().Writer).Encode(list); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal shortcut list response").SetInternal(err)
}
@@ -98,7 +89,7 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, shortcut); err != nil {
if err := json.NewEncoder(c.Response().Writer).Encode(shortcut); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal shortcut response").SetInternal(err)
}

View File

@@ -1,11 +1,11 @@
package server
import (
"encoding/json"
"memos/api"
"memos/common"
"net/http"
"github.com/google/jsonapi"
"github.com/labstack/echo/v4"
)
@@ -21,9 +21,35 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, user); err != nil {
if err := json.NewEncoder(c.Response().Writer).Encode(user); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal user response").SetInternal(err)
}
return nil
})
g.POST("/user/rename_check", func(c echo.Context) error {
userRenameCheck := &api.UserRenameCheck{}
if err := json.NewDecoder(c.Request().Body).Decode(userRenameCheck); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post user rename check request").SetInternal(err)
}
if userRenameCheck.Name == "" {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post user rename check request")
}
userFind := &api.UserFind{
Name: &userRenameCheck.Name,
}
user, err := s.UserService.FindUser(userFind)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(user); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal user response").SetInternal(err)
}
return nil
})
g.PATCH("user/me", func(c echo.Context) error {
@@ -31,7 +57,7 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
userPatch := &api.UserPatch{
Id: userId,
}
if err := jsonapi.UnmarshalPayload(c.Request().Body, userPatch); err != nil {
if err := json.NewDecoder(c.Request().Body).Decode(userPatch); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch user request").SetInternal(err)
}
@@ -46,7 +72,7 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, user); err != nil {
if err := json.NewEncoder(c.Response().Writer).Encode(user); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal user response").SetInternal(err)
}

View File

@@ -1,12 +1,12 @@
package server
import (
"encoding/json"
"fmt"
"memos/api"
"net/http"
"strconv"
"github.com/google/jsonapi"
"github.com/labstack/echo/v4"
)
@@ -31,7 +31,7 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
memoCreate := &api.MemoCreate{
CreatorId: user.Id,
}
if err := jsonapi.UnmarshalPayload(c.Request().Body, memoCreate); err != nil {
if err := json.NewDecoder(c.Request().Body).Decode(memoCreate); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo request by open api").SetInternal(err)
}
@@ -41,7 +41,7 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, memo); err != nil {
if err := json.NewEncoder(c.Response().Writer).Encode(memo); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal memo response").SetInternal(err)
}