chore: format server code

This commit is contained in:
email
2022-02-18 22:21:10 +08:00
parent 6f3663cd96
commit 3874523e61
8 changed files with 36 additions and 12 deletions

View File

@ -11,8 +11,9 @@ type Memo struct {
} }
type MemoCreate struct { type MemoCreate struct {
Content string `json:"content"`
CreatorId int CreatorId int
Content string `json:"content"`
} }
type MemoPatch struct { type MemoPatch struct {
@ -29,8 +30,7 @@ type MemoFind struct {
} }
type MemoDelete struct { type MemoDelete struct {
Id *int `json:"id"` Id *int `json:"id"`
CreatorId *int
} }
type MemoService interface { type MemoService interface {

View File

@ -1,10 +1,10 @@
package cmd package cmd
import ( import (
"flag"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
) )
@ -40,22 +40,29 @@ func checkDSN(dataDir string) (string, error) {
// GetDevProfile will return a profile for dev. // GetDevProfile will return a profile for dev.
func GetProfile() Profile { func GetProfile() Profile {
mode := flag.String("mode", "dev", "") mode := os.Getenv("mode")
port := flag.Int("port", 8080, "") if mode != "dev" && mode != "release" {
data := flag.String("data", "", "") mode = "dev"
flag.Parse() }
dataDir, err := checkDSN(*data) port, err := strconv.Atoi(os.Getenv("port"))
if err != nil {
port = 8080
}
data := os.Getenv("data")
dataDir, err := checkDSN(data)
if err != nil { if err != nil {
fmt.Printf("Failed to check dsn: %s, err: %+v\n", dataDir, err) fmt.Printf("Failed to check dsn: %s, err: %+v\n", dataDir, err)
os.Exit(1) os.Exit(1)
} }
dsn := fmt.Sprintf("file:%s/memos_%s.db", dataDir, *mode) dsn := fmt.Sprintf("file:%s/memos_%s.db", dataDir, mode)
return Profile{ return Profile{
mode: *mode, mode: mode,
port: *port, port: port,
dsn: dsn, dsn: dsn,
} }
} }

View File

@ -47,6 +47,7 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
return nil return nil
}) })
g.POST("/auth/logout", func(c echo.Context) error { g.POST("/auth/logout", func(c echo.Context) error {
err := removeUserSession(c) err := removeUserSession(c)
if err != nil { if err != nil {
@ -56,6 +57,7 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
c.Response().WriteHeader(http.StatusOK) c.Response().WriteHeader(http.StatusOK)
return nil return nil
}) })
g.POST("/auth/signup", func(c echo.Context) error { g.POST("/auth/signup", func(c echo.Context) error {
signup := &api.Signup{} signup := &api.Signup{}
if err := json.NewDecoder(c.Request().Body).Decode(signup); err != nil { if err := json.NewDecoder(c.Request().Body).Decode(signup); err != nil {

View File

@ -33,6 +33,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
return nil return nil
}) })
g.PATCH("/memo/:memoId", func(c echo.Context) error { g.PATCH("/memo/:memoId", func(c echo.Context) error {
memoId, err := strconv.Atoi(c.Param("memoId")) memoId, err := strconv.Atoi(c.Param("memoId"))
if err != nil { if err != nil {
@ -58,6 +59,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
return nil return nil
}) })
g.GET("/memo", func(c echo.Context) error { g.GET("/memo", func(c echo.Context) error {
userId := c.Get(getUserIdContextKey()).(int) userId := c.Get(getUserIdContextKey()).(int)
memoFind := &api.MemoFind{ memoFind := &api.MemoFind{
@ -86,6 +88,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
return nil return nil
}) })
g.GET("/memo/:memoId", func(c echo.Context) error { g.GET("/memo/:memoId", func(c echo.Context) error {
memoId, err := strconv.Atoi(c.Param("memoId")) memoId, err := strconv.Atoi(c.Param("memoId"))
if err != nil { if err != nil {
@ -111,6 +114,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
return nil return nil
}) })
g.DELETE("/memo/:memoId", func(c echo.Context) error { g.DELETE("/memo/:memoId", func(c echo.Context) error {
memoId, err := strconv.Atoi(c.Param("memoId")) memoId, err := strconv.Atoi(c.Param("memoId"))
if err != nil { if err != nil {

View File

@ -59,6 +59,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
return nil return nil
}) })
g.GET("/resource", func(c echo.Context) error { g.GET("/resource", func(c echo.Context) error {
userId := c.Get(getUserIdContextKey()).(int) userId := c.Get(getUserIdContextKey()).(int)
resourceFind := &api.ResourceFind{ resourceFind := &api.ResourceFind{
@ -76,6 +77,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
return nil return nil
}) })
g.DELETE("/resource/:resourceId", func(c echo.Context) error { g.DELETE("/resource/:resourceId", func(c echo.Context) error {
resourceId, err := strconv.Atoi(c.Param("resourceId")) resourceId, err := strconv.Atoi(c.Param("resourceId"))
if err != nil { if err != nil {

View File

@ -32,6 +32,7 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
return nil return nil
}) })
g.PATCH("/shortcut/:shortcutId", func(c echo.Context) error { g.PATCH("/shortcut/:shortcutId", func(c echo.Context) error {
shortcutId, err := strconv.Atoi(c.Param("shortcutId")) shortcutId, err := strconv.Atoi(c.Param("shortcutId"))
if err != nil { if err != nil {
@ -57,6 +58,7 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
return nil return nil
}) })
g.GET("/shortcut", func(c echo.Context) error { g.GET("/shortcut", func(c echo.Context) error {
userId := c.Get(getUserIdContextKey()).(int) userId := c.Get(getUserIdContextKey()).(int)
shortcutFind := &api.ShortcutFind{ shortcutFind := &api.ShortcutFind{
@ -74,6 +76,7 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
return nil return nil
}) })
g.GET("/shortcut/:shortcutId", func(c echo.Context) error { g.GET("/shortcut/:shortcutId", func(c echo.Context) error {
shortcutId, err := strconv.Atoi(c.Param("shortcutId")) shortcutId, err := strconv.Atoi(c.Param("shortcutId"))
if err != nil { if err != nil {
@ -95,6 +98,7 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
return nil return nil
}) })
g.DELETE("/shortcut/:shortcutId", func(c echo.Context) error { g.DELETE("/shortcut/:shortcutId", func(c echo.Context) error {
shortcutId, err := strconv.Atoi(c.Param("shortcutId")) shortcutId, err := strconv.Atoi(c.Param("shortcutId"))
if err != nil { if err != nil {

View File

@ -34,6 +34,7 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
return nil return nil
}) })
g.POST("/user/rename_check", func(c echo.Context) error { g.POST("/user/rename_check", func(c echo.Context) error {
userRenameCheck := &api.UserRenameCheck{} userRenameCheck := &api.UserRenameCheck{}
if err := json.NewDecoder(c.Request().Body).Decode(userRenameCheck); err != nil { if err := json.NewDecoder(c.Request().Body).Decode(userRenameCheck); err != nil {
@ -64,6 +65,7 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
return nil return nil
}) })
g.POST("/user/password_check", func(c echo.Context) error { g.POST("/user/password_check", func(c echo.Context) error {
userId := c.Get(getUserIdContextKey()).(int) userId := c.Get(getUserIdContextKey()).(int)
userPasswordCheck := &api.UserPasswordCheck{} userPasswordCheck := &api.UserPasswordCheck{}
@ -96,6 +98,7 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
return nil return nil
}) })
g.PATCH("/user/me", func(c echo.Context) error { g.PATCH("/user/me", func(c echo.Context) error {
userId := c.Get(getUserIdContextKey()).(int) userId := c.Get(getUserIdContextKey()).(int)
userPatch := &api.UserPatch{ userPatch := &api.UserPatch{

View File

@ -14,6 +14,7 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
g.GET("/test", func(c echo.Context) error { g.GET("/test", func(c echo.Context) error {
return c.HTML(http.StatusOK, "<strong>Hello, World!</strong>") return c.HTML(http.StatusOK, "<strong>Hello, World!</strong>")
}) })
g.POST("/:openId/memo", func(c echo.Context) error { g.POST("/:openId/memo", func(c echo.Context) error {
openId := c.Param("openId") openId := c.Param("openId")
@ -47,6 +48,7 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
return nil return nil
}) })
g.GET("/:openId/memo", func(c echo.Context) error { g.GET("/:openId/memo", func(c echo.Context) error {
openId := c.Param("openId") openId := c.Param("openId")