From 8603315a5d99ca8cc25d93e9bb66905b01ad088d Mon Sep 17 00:00:00 2001 From: Paul Pan Date: Tue, 20 Sep 2022 14:15:21 +0800 Subject: [PATCH] chore: rename Err to Status --- cmd/app/main.go | 1 + internal/api/user/create.go | 16 ++++++------- internal/api/user/login.go | 16 ++++++------- internal/api/user/logout.go | 4 ++-- internal/api/user/profile.go | 1 + internal/e/code.go | 38 +++++++++++++++--------------- internal/e/err.go | 4 ++-- internal/e/resp.go | 12 +++++----- internal/global/jwt.go | 4 ++-- internal/pkg/cast/cast.go | 2 +- internal/pkg/metrics/middleware.go | 2 +- internal/pkg/metrics/prometheus.go | 2 +- internal/service/jwt/middleware.go | 6 ++--- internal/service/jwt/token.go | 4 ++-- internal/service/user/create.go | 2 +- internal/service/user/login.go | 2 +- internal/service/user/service.go | 6 ++--- internal/service/user/version.go | 2 +- 18 files changed, 63 insertions(+), 61 deletions(-) create mode 100644 internal/api/user/profile.go diff --git a/cmd/app/main.go b/cmd/app/main.go index afae6fa..0d1f51f 100644 --- a/cmd/app/main.go +++ b/cmd/app/main.go @@ -10,6 +10,7 @@ import ( func main() { a := &cli.App{ Name: "OJ", + Usage: "woj-server", Compiled: getBuildTime(), Version: Version, EnableBashCompletion: true, diff --git a/internal/api/user/create.go b/internal/api/user/create.go index 68425f0..f00b5f2 100644 --- a/internal/api/user/create.go +++ b/internal/api/user/create.go @@ -37,21 +37,21 @@ func (h *handler) Create(c *gin.Context) { Password: req.Password, } - u, err := h.userService.Create(createData) - if err != e.Success { - e.Pong(c, err, nil) + u, status := h.userService.Create(createData) + if status != e.Success { + e.Pong(c, status, nil) return } - version, err := h.userService.IncrVersion(u.ID) - if err != e.Success { - e.Pong(c, err, nil) + version, status := h.userService.IncrVersion(u.ID) + if status != e.Success { + e.Pong(c, status, nil) return } claim := &global.Claim{ UID: u.ID, Version: version, } - token, err := h.jwtService.SignClaim(claim) - e.Pong(c, err, token) + token, status := h.jwtService.SignClaim(claim) + e.Pong(c, status, token) } diff --git a/internal/api/user/login.go b/internal/api/user/login.go index c199ec3..79ae4ed 100644 --- a/internal/api/user/login.go +++ b/internal/api/user/login.go @@ -34,22 +34,22 @@ func (h *handler) Login(c *gin.Context) { UserName: req.Username, Password: []byte(req.Password), } - user, err := h.userService.Login(userData) - if err != e.Success { - e.Pong(c, err, nil) + user, status := h.userService.Login(userData) + if status != e.Success { + e.Pong(c, status, nil) return } // sign and return token - version, err := h.userService.IncrVersion(user.ID) - if err != e.Success { - e.Pong(c, err, nil) + version, status := h.userService.IncrVersion(user.ID) + if status != e.Success { + e.Pong(c, status, nil) return } claim := &global.Claim{ UID: user.ID, Version: version, } - token, err := h.jwtService.SignClaim(claim) - e.Pong(c, err, token) + token, status := h.jwtService.SignClaim(claim) + e.Pong(c, status, token) } diff --git a/internal/api/user/logout.go b/internal/api/user/logout.go index 0a66bb7..f9a288c 100644 --- a/internal/api/user/logout.go +++ b/internal/api/user/logout.go @@ -21,6 +21,6 @@ func (h *handler) Logout(c *gin.Context) { return } - _, err := h.userService.IncrVersion(claim.(*global.Claim).UID) - e.Pong(c, err, nil) + _, status := h.userService.IncrVersion(claim.(*global.Claim).UID) + e.Pong(c, status, nil) } diff --git a/internal/api/user/profile.go b/internal/api/user/profile.go new file mode 100644 index 0000000..a00006b --- /dev/null +++ b/internal/api/user/profile.go @@ -0,0 +1 @@ +package user diff --git a/internal/e/code.go b/internal/e/code.go index 83abc4f..5a6fd2b 100644 --- a/internal/e/code.go +++ b/internal/e/code.go @@ -1,31 +1,31 @@ package e const ( - Success Err = 0 - Unknown Err = 1 + Success Status = 0 + Unknown Status = 1 - InternalError Err = 100 - InvalidParameter Err = 101 - NotFound Err = 102 - DatabaseError Err = 103 + InternalError Status = 100 + InvalidParameter Status = 101 + NotFound Status = 102 + DatabaseError Status = 103 - TokenUnknown Err = 200 - TokenEmpty Err = 201 - TokenMalformed Err = 202 - TokenTimeError Err = 203 - TokenInvalid Err = 204 - TokenSignError Err = 205 - TokenRevoked Err = 206 + TokenUnknown Status = 200 + TokenEmpty Status = 201 + TokenMalformed Status = 202 + TokenTimeError Status = 203 + TokenInvalid Status = 204 + TokenSignError Status = 205 + TokenRevoked Status = 206 - UserNotFound Err = 300 - UserWrongPassword Err = 301 - UserDuplicated Err = 302 - UserUnauthenticated Err = 303 + UserNotFound Status = 300 + UserWrongPassword Status = 301 + UserDuplicated Status = 302 + UserUnauthenticated Status = 303 - RedisError Err = 400 + RedisError Status = 400 ) -var msgText = map[Err]string{ +var msgText = map[Status]string{ Success: "Success", Unknown: "Unknown error", diff --git a/internal/e/err.go b/internal/e/err.go index 240f897..44204e0 100644 --- a/internal/e/err.go +++ b/internal/e/err.go @@ -1,8 +1,8 @@ package e -type Err int +type Status int -func (code Err) String() string { +func (code Status) String() string { msg, ok := msgText[code] if ok { return msg diff --git a/internal/e/resp.go b/internal/e/resp.go index 44b6b66..25da040 100644 --- a/internal/e/resp.go +++ b/internal/e/resp.go @@ -12,14 +12,14 @@ type Response struct { Body interface{} `json:"body"` } -func Wrap(err Err, body interface{}) interface{} { +func Wrap(status Status, body interface{}) interface{} { return Response{ - Code: int(err), - Msg: err.String(), - Body: utils.If(err == Success, body, nil), + Code: int(status), + Msg: status.String(), + Body: utils.If(status == Success, body, nil), } } -func Pong(c *gin.Context, err Err, body interface{}) { - c.JSON(http.StatusOK, Wrap(err, body)) +func Pong(c *gin.Context, status Status, body interface{}) { + c.JSON(http.StatusOK, Wrap(status, body)) } diff --git a/internal/global/jwt.go b/internal/global/jwt.go index dbd5e77..24cf814 100644 --- a/internal/global/jwt.go +++ b/internal/global/jwt.go @@ -13,8 +13,8 @@ type Claim struct { } type JwtService interface { - ParseToken(tokenText string) (*Claim, e.Err) - SignClaim(claim *Claim) (string, e.Err) + ParseToken(tokenText string) (*Claim, e.Status) + SignClaim(claim *Claim) (string, e.Status) Validate(claim *Claim) bool Handler() gin.HandlerFunc diff --git a/internal/pkg/cast/cast.go b/internal/pkg/cast/cast.go index 16639fa..d73466d 100644 --- a/internal/pkg/cast/cast.go +++ b/internal/pkg/cast/cast.go @@ -13,7 +13,7 @@ func ToString(obj interface{}) string { return strconv.FormatBool(t) case []byte: return string(t) - case e.Err: + case e.Status: return t.String() case error: return t.Error() diff --git a/internal/pkg/metrics/middleware.go b/internal/pkg/metrics/middleware.go index 255f17b..679c322 100644 --- a/internal/pkg/metrics/middleware.go +++ b/internal/pkg/metrics/middleware.go @@ -30,7 +30,7 @@ func (m *Metrics) Handler() gin.HandlerFunc { status := c.Writer.Status() success := !c.IsAborted() && (status == http.StatusOK) errCode, _ := c.Get("err") - err, ok := errCode.(e.Err) + err, ok := errCode.(e.Status) if !ok { success = false err = e.Unknown diff --git a/internal/pkg/metrics/prometheus.go b/internal/pkg/metrics/prometheus.go index 4940b09..8426604 100644 --- a/internal/pkg/metrics/prometheus.go +++ b/internal/pkg/metrics/prometheus.go @@ -44,7 +44,7 @@ func (m *Metrics) Setup(namespace string, subsystem string) { prometheus.MustRegister(m.counter, m.hist) } -func (m *Metrics) Record(method, url string, success bool, httpCode int, errCode e.Err, elapsed float64) { +func (m *Metrics) Record(method, url string, success bool, httpCode int, errCode e.Status, elapsed float64) { m.counter.With(prometheus.Labels{ "method": method, "url": url, diff --git a/internal/service/jwt/middleware.go b/internal/service/jwt/middleware.go index 22145e9..c998f59 100644 --- a/internal/service/jwt/middleware.go +++ b/internal/service/jwt/middleware.go @@ -17,9 +17,9 @@ func (s *service) Handler() gin.HandlerFunc { } token := tokenHeader[len(tokenPrefix):] - claim, err := s.ParseToken(token) - if err != e.Success { - e.Pong(c, err, nil) + claim, status := s.ParseToken(token) + if status != e.Success { + e.Pong(c, status, nil) c.Abort() return } diff --git a/internal/service/jwt/token.go b/internal/service/jwt/token.go index 19a2412..876757e 100644 --- a/internal/service/jwt/token.go +++ b/internal/service/jwt/token.go @@ -11,7 +11,7 @@ import ( "time" ) -func (s *service) ParseToken(tokenText string) (*global.Claim, e.Err) { +func (s *service) ParseToken(tokenText string) (*global.Claim, e.Status) { if tokenText == "" { return nil, e.TokenEmpty } @@ -48,7 +48,7 @@ func (s *service) ParseToken(tokenText string) (*global.Claim, e.Err) { return nil, e.TokenInvalid } -func (s *service) SignClaim(claim *global.Claim) (string, e.Err) { +func (s *service) SignClaim(claim *global.Claim) (string, e.Status) { now := time.Now() claim.IssuedAt = jwt.NewNumericDate(now) diff --git a/internal/service/user/create.go b/internal/service/user/create.go index a0b2b8d..72d4854 100644 --- a/internal/service/user/create.go +++ b/internal/service/user/create.go @@ -14,7 +14,7 @@ type CreateData struct { Password string } -func (s *service) Create(data *CreateData) (*model.User, e.Err) { +func (s *service) Create(data *CreateData) (*model.User, e.Status) { hashed, err := bcrypt.GenerateFromPassword([]byte(data.Password), bcrypt.DefaultCost) if err != nil { s.log.Debug("bcrypt error", zap.Error(err), zap.String("password", data.Password)) diff --git a/internal/service/user/login.go b/internal/service/user/login.go index 48e8ee4..4a5aaa3 100644 --- a/internal/service/user/login.go +++ b/internal/service/user/login.go @@ -8,7 +8,7 @@ import ( "gorm.io/gorm" ) -func (s *service) Login(data *model.User) (*model.User, e.Err) { +func (s *service) Login(data *model.User) (*model.User, e.Status) { user := &model.User{UserName: data.UserName} err := s.db.Where(user).First(&user).Error diff --git a/internal/service/user/service.go b/internal/service/user/service.go index 00ae8f1..e684c72 100644 --- a/internal/service/user/service.go +++ b/internal/service/user/service.go @@ -12,9 +12,9 @@ import ( var _ Service = (*service)(nil) type Service interface { - Create(data *CreateData) (*model.User, e.Err) - Login(data *model.User) (*model.User, e.Err) - IncrVersion(id uint) (int64, e.Err) + Create(data *CreateData) (*model.User, e.Status) + Login(data *model.User) (*model.User, e.Status) + IncrVersion(id uint) (int64, e.Status) } type service struct { diff --git a/internal/service/user/version.go b/internal/service/user/version.go index 1485dad..a342668 100644 --- a/internal/service/user/version.go +++ b/internal/service/user/version.go @@ -7,7 +7,7 @@ import ( "go.uber.org/zap" ) -func (s *service) IncrVersion(id uint) (int64, e.Err) { +func (s *service) IncrVersion(id uint) (int64, e.Status) { version, err := s.redis.Incr(context.Background(), fmt.Sprintf("Version:%d", id)).Result() if err != nil { s.log.Debug("redis.Incr error", zap.Error(err))