init repo
This commit is contained in:
49
pkg/common/errors.go
Normal file
49
pkg/common/errors.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package common
|
||||
|
||||
type ErrorCode int
|
||||
|
||||
const (
|
||||
CodeSuccess = 200
|
||||
CodeParamError = 400
|
||||
CodeUnauthorized = 401
|
||||
CodeForbidden = 403
|
||||
CodeNotFound = 404
|
||||
CodeInvalidStatus = 405
|
||||
CodeDBError = 500
|
||||
CodeSystemError = 501
|
||||
CodeTaskNotComplete = 1001
|
||||
CodeRecordRepeat = 1002
|
||||
CodeSmsCodeError = 1003
|
||||
)
|
||||
|
||||
const (
|
||||
CodeSuccessMsg = "success"
|
||||
CodeParamErrorMsg = "param error"
|
||||
CodeUnauthorizedMsg = "unauthorized"
|
||||
CodeForbiddenMsg = "forbidden"
|
||||
CodeNotFoundMsg = "not found"
|
||||
CodeInvalidStatusMsg = "invalid status"
|
||||
CodeDBErrorMsg = "database error"
|
||||
CodeSystemErrorMsg = "system error"
|
||||
CodeTaskNotCompleteMsg = "task not complete"
|
||||
CodeRecordRepeatMsg = "record repeat"
|
||||
CodeSmsCodeErrorMsg = "sms code error"
|
||||
)
|
||||
|
||||
type BusinessError struct {
|
||||
Code ErrorCode
|
||||
Message string
|
||||
Err error
|
||||
}
|
||||
|
||||
func (e *BusinessError) Error() string {
|
||||
return e.Message
|
||||
}
|
||||
|
||||
func NewError(code ErrorCode, message string, err error) *BusinessError {
|
||||
return &BusinessError{
|
||||
Code: code,
|
||||
Message: message,
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
59
pkg/common/middleware.go
Normal file
59
pkg/common/middleware.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"gitea.com/bitwsd/document_ai/pkg/constant"
|
||||
"gitea.com/bitwsd/document_ai/pkg/jwt"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func MiddlewareContext(c *gin.Context) {
|
||||
c.Set(constant.ContextIP, c.ClientIP())
|
||||
}
|
||||
|
||||
func GetIPFromContext(ctx context.Context) string {
|
||||
return ctx.Value(constant.ContextIP).(string)
|
||||
}
|
||||
|
||||
func GetUserIDFromContext(ctx *gin.Context) int64 {
|
||||
return ctx.GetInt64(constant.ContextUserID)
|
||||
}
|
||||
|
||||
func AuthMiddleware(ctx *gin.Context) {
|
||||
token := ctx.GetHeader("Authorization")
|
||||
if token == "" {
|
||||
ctx.JSON(http.StatusOK, ErrorResponse(ctx, CodeUnauthorized, CodeUnauthorizedMsg))
|
||||
ctx.Abort()
|
||||
}
|
||||
|
||||
token = strings.TrimPrefix(token, "Bearer ")
|
||||
|
||||
claims, err := jwt.ParseToken(token)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, ErrorResponse(ctx, CodeUnauthorized, CodeUnauthorizedMsg))
|
||||
ctx.Abort()
|
||||
}
|
||||
|
||||
if claims == nil {
|
||||
ctx.JSON(http.StatusOK, ErrorResponse(ctx, CodeUnauthorized, CodeUnauthorizedMsg))
|
||||
ctx.Abort()
|
||||
}
|
||||
|
||||
ctx.Set(constant.ContextUserID, claims.UserId)
|
||||
}
|
||||
|
||||
func GetAuthMiddleware() gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
token := ctx.GetHeader("Authorization")
|
||||
if token != "" {
|
||||
token = strings.TrimPrefix(token, "Bearer ")
|
||||
claims, err := jwt.ParseToken(token)
|
||||
if err == nil {
|
||||
ctx.Set(constant.ContextUserID, claims.UserId)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
31
pkg/common/response.go
Normal file
31
pkg/common/response.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitea.com/bitwsd/document_ai/pkg/constant"
|
||||
)
|
||||
|
||||
type Response struct {
|
||||
RequestID string `json:"request_id"`
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
func ErrorResponse(ctx context.Context, code int, message string) *Response {
|
||||
return &Response{
|
||||
RequestID: ctx.Value(constant.ContextRequestID).(string),
|
||||
Code: code,
|
||||
Message: message,
|
||||
}
|
||||
}
|
||||
|
||||
func SuccessResponse(ctx context.Context, data interface{}) *Response {
|
||||
return &Response{
|
||||
RequestID: ctx.Value(constant.ContextRequestID).(string),
|
||||
Code: CodeSuccess,
|
||||
Message: "success",
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user