First merge from develop to main (WIP) #7
55
exception/builder.go
Normal file
55
exception/builder.go
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
package exception
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 12 chars len
|
||||||
|
// :1=status
|
||||||
|
// :3=service
|
||||||
|
// :2=endpoint
|
||||||
|
// :1=common/specific
|
||||||
|
// :5=original
|
||||||
|
|
||||||
|
type Builder struct {
|
||||||
|
Status string
|
||||||
|
Service string
|
||||||
|
Endpoint string
|
||||||
|
Type string
|
||||||
|
Original string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *Builder) SetStatus(s string) *Builder {
|
||||||
|
self.Status = s
|
||||||
|
return self
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *Builder) SetService(s string) *Builder {
|
||||||
|
self.Service = s
|
||||||
|
return self
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *Builder) SetEndpoint(s string) *Builder {
|
||||||
|
self.Endpoint = s
|
||||||
|
return self
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *Builder) SetType(s string) *Builder {
|
||||||
|
self.Type = s
|
||||||
|
return self
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *Builder) SetOriginal(s string) *Builder {
|
||||||
|
self.Original = s
|
||||||
|
return self
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *Builder) Build() string {
|
||||||
|
return fmt.Sprintf("%s%s%s%s%s",
|
||||||
|
self.Status,
|
||||||
|
self.Service,
|
||||||
|
self.Endpoint,
|
||||||
|
self.Type,
|
||||||
|
self.Original,
|
||||||
|
)
|
||||||
|
}
|
||||||
6
exception/common.go
Normal file
6
exception/common.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package exception
|
||||||
|
|
||||||
|
const (
|
||||||
|
CommonErrorInvalidInput = "00001"
|
||||||
|
CommonErrorUnauthorized = "00002"
|
||||||
|
)
|
||||||
32
exception/endpoints.go
Normal file
32
exception/endpoints.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package exception
|
||||||
|
|
||||||
|
// Middleware Service Endpoints
|
||||||
|
const (
|
||||||
|
MiddlewareEndpoint = "01"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Auth Service Endpoints
|
||||||
|
const (
|
||||||
|
AuthRedirectEndpoint = "01"
|
||||||
|
AuthMagicEndpoint = "02"
|
||||||
|
AuthTokenEndpoint = "03"
|
||||||
|
AuthRefreshEndpoint = "04"
|
||||||
|
AuthExchangeEndpoint = "05"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Event Service Endpoints
|
||||||
|
const (
|
||||||
|
EventInfoEndpoint = "01"
|
||||||
|
EventCheckinEndpoint = "02"
|
||||||
|
EventCheckinQueryEndpoint = "03"
|
||||||
|
EventCheckinSubmitEndpoint = "04"
|
||||||
|
)
|
||||||
|
|
||||||
|
// User Service Endpoints
|
||||||
|
const (
|
||||||
|
UserInfoEndpoint = "01"
|
||||||
|
UserUpdateEndpoint = "02"
|
||||||
|
UserListEndpoint = "03"
|
||||||
|
UserFullEndpoint = "04"
|
||||||
|
UserCreateEndpoint = "05"
|
||||||
|
)
|
||||||
16
exception/services.go
Normal file
16
exception/services.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package exception
|
||||||
|
|
||||||
|
// Middleware Services
|
||||||
|
const (
|
||||||
|
MiddlewareGinLoggerService = "901"
|
||||||
|
MiddlewareJwtService = "902"
|
||||||
|
MiddlewarePermissionService = "903"
|
||||||
|
MiddlewareApiVersionService = "904"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Application Services
|
||||||
|
const (
|
||||||
|
AuthService = "001"
|
||||||
|
UserService = "002"
|
||||||
|
EventService = "003"
|
||||||
|
)
|
||||||
5
exception/specific.go
Normal file
5
exception/specific.go
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package exception
|
||||||
|
|
||||||
|
const (
|
||||||
|
ApiVersionNotFound = "00001"
|
||||||
|
)
|
||||||
8
exception/status.go
Normal file
8
exception/status.go
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package exception
|
||||||
|
|
||||||
|
const (
|
||||||
|
ErrorStatusSuccess = "2"
|
||||||
|
ErrorStatusUser = "4"
|
||||||
|
ErrorStatusServer = "5"
|
||||||
|
ErrorStatusClient = "6"
|
||||||
|
)
|
||||||
6
exception/types.go
Normal file
6
exception/types.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package exception
|
||||||
|
|
||||||
|
const (
|
||||||
|
ErrorTypeCommon = "0"
|
||||||
|
ErrorTypeSpecific = "1"
|
||||||
|
)
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"nixcn-cms/exception"
|
||||||
"nixcn-cms/utils"
|
"nixcn-cms/utils"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -10,7 +11,14 @@ func ApiVersionCheck() gin.HandlerFunc {
|
|||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
apiVersion := c.GetHeader("X-Api-Version")
|
apiVersion := c.GetHeader("X-Api-Version")
|
||||||
if apiVersion == "" {
|
if apiVersion == "" {
|
||||||
utils.HttpAbort(c, 400, "", "api version not found")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusServer).
|
||||||
|
SetService(exception.MiddlewareApiVersionService).
|
||||||
|
SetEndpoint(exception.MiddlewareEndpoint).
|
||||||
|
SetType(exception.ErrorTypeSpecific).
|
||||||
|
SetOriginal(exception.ApiVersionNotFound).
|
||||||
|
Build()
|
||||||
|
utils.HttpAbort(c, 400, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.Next()
|
c.Next()
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"nixcn-cms/exception"
|
||||||
"nixcn-cms/pkgs/authtoken"
|
"nixcn-cms/pkgs/authtoken"
|
||||||
"nixcn-cms/utils"
|
"nixcn-cms/utils"
|
||||||
|
|
||||||
@@ -16,8 +16,15 @@ func JWTAuth() gin.HandlerFunc {
|
|||||||
authtoken := new(authtoken.Token)
|
authtoken := new(authtoken.Token)
|
||||||
uid, err := authtoken.HeaderVerify(auth)
|
uid, err := authtoken.HeaderVerify(auth)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
errorCode := new(exception.Builder).
|
||||||
utils.HttpAbort(c, 401, "", "unauthorized")
|
SetStatus(exception.ErrorStatusServer).
|
||||||
|
SetService(exception.MiddlewareJwtService).
|
||||||
|
SetEndpoint(exception.MiddlewareEndpoint).
|
||||||
|
SetType(exception.ErrorTypeSpecific).
|
||||||
|
SetOriginal(exception.CommonErrorUnauthorized).
|
||||||
|
Build()
|
||||||
|
|
||||||
|
utils.HttpAbort(c, 401, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ import (
|
|||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const ()
|
||||||
|
|
||||||
func Exchange(c *gin.Context) {
|
func Exchange(c *gin.Context) {
|
||||||
var exchangeReq struct {
|
var exchangeReq struct {
|
||||||
ClientId string `json:"client_id"`
|
ClientId string `json:"client_id"`
|
||||||
|
|||||||
@@ -9,16 +9,16 @@ import (
|
|||||||
|
|
||||||
type RespStatus struct {
|
type RespStatus struct {
|
||||||
Code int `json:"code"`
|
Code int `json:"code"`
|
||||||
ErrorId string `json:"error_id"`
|
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
|
ErrorId string `json:"error_id"`
|
||||||
Data any `json:"data"`
|
Data any `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func render(c *gin.Context, code int, id string, status string, data []any, abort bool) {
|
func render(c *gin.Context, code int, errId string, data []any, abort bool) {
|
||||||
resp := RespStatus{
|
resp := RespStatus{
|
||||||
Code: code,
|
Code: code,
|
||||||
ErrorId: id,
|
Status: http.StatusText(code),
|
||||||
Status: status,
|
ErrorId: errId,
|
||||||
}
|
}
|
||||||
|
|
||||||
switch len(data) {
|
switch len(data) {
|
||||||
@@ -45,10 +45,10 @@ func render(c *gin.Context, code int, id string, status string, data []any, abor
|
|||||||
_, _ = c.Writer.Write(jsonBytes)
|
_, _ = c.Writer.Write(jsonBytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
func HttpResponse(c *gin.Context, code int, id string, status string, data ...any) {
|
func HttpResponse(c *gin.Context, code int, errId string, data ...any) {
|
||||||
render(c, code, id, status, data, false)
|
render(c, code, errId, data, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func HttpAbort(c *gin.Context, code int, id string, status string, data ...any) {
|
func HttpAbort(c *gin.Context, code int, errId string, data ...any) {
|
||||||
render(c, code, id, status, data, true)
|
render(c, code, errId, data, true)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user