Compare commits
2 Commits
4ac5b1c101
...
5dbbdc62e6
| Author | SHA1 | Date | |
|---|---|---|---|
|
5dbbdc62e6
|
|||
|
200614a5c9
|
@@ -34,7 +34,7 @@ func Init() {
|
||||
// Conect to db
|
||||
db, err := drivers.Postgres(exDSN)
|
||||
if err != nil {
|
||||
slog.Error("[Database] Error connecting to db!")
|
||||
slog.Error("[Database] Error connecting to db!", "err", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
|
||||
import (
|
||||
"nixcn-cms/exception"
|
||||
"nixcn-cms/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -10,7 +11,14 @@ func ApiVersionCheck() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
apiVersion := c.GetHeader("X-Api-Version")
|
||||
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
|
||||
}
|
||||
c.Next()
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"nixcn-cms/exception"
|
||||
"nixcn-cms/pkgs/authtoken"
|
||||
"nixcn-cms/utils"
|
||||
|
||||
@@ -16,8 +16,15 @@ func JWTAuth() gin.HandlerFunc {
|
||||
authtoken := new(authtoken.Token)
|
||||
uid, err := authtoken.HeaderVerify(auth)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
utils.HttpAbort(c, 401, "", "unauthorized")
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.ErrorStatusServer).
|
||||
SetService(exception.MiddlewareJwtService).
|
||||
SetEndpoint(exception.MiddlewareEndpoint).
|
||||
SetType(exception.ErrorTypeSpecific).
|
||||
SetOriginal(exception.CommonErrorUnauthorized).
|
||||
Build()
|
||||
|
||||
utils.HttpAbort(c, 401, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,8 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const ()
|
||||
|
||||
func Exchange(c *gin.Context) {
|
||||
var exchangeReq struct {
|
||||
ClientId string `json:"client_id"`
|
||||
|
||||
@@ -9,16 +9,16 @@ import (
|
||||
|
||||
type RespStatus struct {
|
||||
Code int `json:"code"`
|
||||
ErrorId string `json:"error_id"`
|
||||
Status string `json:"status"`
|
||||
ErrorId string `json:"error_id"`
|
||||
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{
|
||||
Code: code,
|
||||
ErrorId: id,
|
||||
Status: status,
|
||||
Status: http.StatusText(code),
|
||||
ErrorId: errId,
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func HttpResponse(c *gin.Context, code int, id string, status string, data ...any) {
|
||||
render(c, code, id, status, data, false)
|
||||
func HttpResponse(c *gin.Context, code int, errId string, data ...any) {
|
||||
render(c, code, errId, data, false)
|
||||
}
|
||||
|
||||
func HttpAbort(c *gin.Context, code int, id string, status string, data ...any) {
|
||||
render(c, code, id, status, data, true)
|
||||
func HttpAbort(c *gin.Context, code int, errId string, data ...any) {
|
||||
render(c, code, errId, data, true)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user