Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions pkg/config/gormdb/casbin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package gormdb

/*
* @Author: lwnmengjing<lwnmengjing@qq.com>
* @Date: 2023/3/4 00:24:48
* @Last Modified by: lwnmengjing<lwnmengjing@qq.com>
* @Last Modified time: 2023/3/4 00:24:48
*/

// CasbinRule casbin rule
type CasbinRule struct {
PType string `json:"pType" gorm:"size:100;"`
V0 string `json:"v0" gorm:"size:100;"`
V1 string `json:"v1" gorm:"size:100;"`
V2 string `json:"v2" gorm:"size:100;"`
V3 string `json:"v3" gorm:"size:100;"`
V4 string `json:"v4" gorm:"size:100;"`
V5 string `json:"v5" gorm:"size:100;"`
}

// TableName table name
func (*CasbinRule) TableName() string {
return "casbin_rule"
}
8 changes: 4 additions & 4 deletions pkg/config/gormdb/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (e *Database) Init() {
if e.CasbinModel != "" {
//set casbin adapter
var a persist.Adapter
a, err = gormadapter.NewAdapterByDBUseTableName(DB, "mss_boot", "casbin_rule")
a, err = gormadapter.NewAdapterByDBWithCustomTable(DB, &CasbinRule{})
if err != nil {
log.Fatalf("gormadapter.NewAdapterByDB error : %s", err.Error())
}
Expand All @@ -112,8 +112,8 @@ func (e *Database) Init() {
if err != nil {
log.Fatalf("Enforcer.LoadPolicy error : %s", err.Error())
}
Enforcer.EnableAutoSave(true)
Enforcer.EnableAutoBuildRoleLinks(true)
Enforcer.EnableLog(true)
//Enforcer.EnableAutoSave(true)
//Enforcer.EnableAutoBuildRoleLinks(true)
//Enforcer.EnableLog(true)
}
}
10 changes: 2 additions & 8 deletions pkg/config/gormdb/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ import (
"gorm.io/gorm/utils"
)

var log loggerCore.Logger

func init() {
log = loggerCore.NewLogger()
}

// Colors
const (
Reset = "\033[0m"
Expand All @@ -42,11 +36,11 @@ type gormLogger struct {
func (l *gormLogger) getLogger(ctx context.Context) loggerCore.Logger {
requestID := ctx.Value("X-Request-ID")
if requestID != nil {
return log.Fields(map[string]interface{}{
return loggerCore.DefaultLogger.Fields(map[string]interface{}{
"x-request-id": requestID,
})
}
return log
return loggerCore.DefaultLogger
}

// LogMode log mode
Expand Down
83 changes: 0 additions & 83 deletions pkg/middlewares/auth.go

This file was deleted.

8 changes: 1 addition & 7 deletions pkg/response/actions/get_gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ package actions
*/

import (
"errors"
"gorm.io/gorm"
"net/http"

"github.com/gin-gonic/gin"
Expand All @@ -32,12 +30,8 @@ func NewGetGorm(m schema.Tabler, key string) *Get {
func (e *Get) getGorm(c *gin.Context, key string) {
api := response.Make(c)
m := pkg.TablerDeepCopy(e.ModelGorm)
err := gormdb.DB.First(m, "id = ?", c.Param(key)).Error
err := gormdb.DB.First(m, c.Param(key)).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
api.Err(http.StatusNotFound)
return
}
api.Log.Error(err)
api.AddError(err)
api.Err(http.StatusInternalServerError)
Expand Down
4 changes: 2 additions & 2 deletions pkg/response/actions/get_mgm.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func (*Get) String() string {
// Handler action handler
func (e *Get) Handler() gin.HandlerFunc {
return func(c *gin.Context) {
if e.ModelGorm != nil {
e.getGorm(c, e.Key)
if e.ModelMgm != nil {
e.getMgm(c, e.Key)
return
}
if e.ModelMgm != nil {
Expand Down
5 changes: 0 additions & 5 deletions pkg/response/actions/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ type ModelGorm struct {
DeletedAt gorm.DeletedAt `gorm:"index" bson:"-" json:"-"`
}

func (e *ModelGorm) BeforeCreate(_ *gorm.DB) (err error) {
_, err = e.PrepareID(nil)
return err
}

// PrepareID prepare id
func (e *ModelGorm) PrepareID(_ any) (any, error) {
if e.ID == "" {
Expand Down
18 changes: 9 additions & 9 deletions pkg/response/model.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package response

import (
"strconv"
"strings"
)

// Response response
type Response struct {
Success bool `json:"success,omitempty"`
Status string `json:"status,omitempty"`
Code int `json:"code,omitempty"`
ErrorCode string `json:"errorCode,omitempty"`
ErrorMessage string `json:"errorMessage,omitempty"`
ShowType uint8 `json:"showType,omitempty"`
Expand All @@ -18,7 +17,7 @@ type Response struct {

type response struct {
Response
List interface{} `json:"list,omitempty"`
Data interface{} `json:"data"`
}

// Page page
Expand All @@ -34,8 +33,8 @@ type page struct {
}

// SetData set data
func (e *response) SetList(data interface{}) {
e.List = data
func (e *response) SetData(data interface{}) {
e.Data = data
}

// Clone clone
Expand All @@ -55,10 +54,11 @@ func (e *response) SetMsg(s ...string) {
}

// SetCode set code
func (e *response) SetCode(code int) {
e.Code = code
func (e *response) SetCode(code int32) {
e.ErrorCode = strconv.Itoa(int(code))
}

func (e *response) SetStatus(status string) {
e.Status = status
// SetSuccess set success
func (e *response) SetSuccess(success bool) {
e.Success = success
}
20 changes: 11 additions & 9 deletions pkg/response/return.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func Error(c *gin.Context, code int, err error, msg ...string) {
}
res.SetMsg(msg...)
res.SetTraceID(pkg.GenerateMsgIDFromContext(c))
res.SetCode(code)
res.SetStatus("error")
res.SetCode(int32(code))
res.SetSuccess(false)
c.Set("result", res)
c.Set("status", code)
c.AbortWithStatusJSON(code, res)
Expand All @@ -35,20 +35,22 @@ func Error(c *gin.Context, code int, err error, msg ...string) {
func OK(c *gin.Context, data interface{}, msg ...string) {
checkContext(c)
res := Default.Clone()
res.SetList(data)
res.SetData(data)
res.SetSuccess(true)
res.SetMsg(msg...)
res.SetTraceID(pkg.GenerateMsgIDFromContext(c))
switch c.Request.Method {
case http.MethodDelete:
res.SetCode(http.StatusNoContent)
c.AbortWithStatusJSON(http.StatusNoContent, data)
c.AbortWithStatusJSON(http.StatusNoContent, res)
return
case http.MethodPost:
res.SetCode(http.StatusCreated)
c.AbortWithStatusJSON(http.StatusCreated, data)
c.AbortWithStatusJSON(http.StatusCreated, res)
return
default:
res.SetCode(http.StatusOK)
c.AbortWithStatusJSON(http.StatusOK, data)
c.AbortWithStatusJSON(http.StatusOK, res)
}
}

Expand All @@ -59,10 +61,10 @@ func PageOK(c *gin.Context, result interface{}, count int64, pageIndex int64, pa
res.Count = count
res.Current = pageIndex
res.PageSize = pageSize
res.response.SetList(result)
//res.response.SetMsg(msg...)
res.response.SetData(result)
res.response.SetMsg(msg...)
res.response.SetTraceID(pkg.GenerateMsgIDFromContext(c))
//res.response.SetCode(http.StatusOK)
res.response.SetCode(http.StatusOK)
c.Set("result", res)
c.Set("status", http.StatusOK)
c.AbortWithStatusJSON(http.StatusOK, res)
Expand Down
6 changes: 3 additions & 3 deletions pkg/response/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ package response

// Responses responses
type Responses interface {
SetCode(int)
SetCode(int32)
SetTraceID(string)
SetMsg(...string)
SetList(interface{})
SetStatus(string)
SetData(interface{})
SetSuccess(bool)
Clone() Responses
}
2 changes: 1 addition & 1 deletion pkg/security/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ type Verifier interface {
GetRoleID() string
GetEmail() string
GetUsername() string
Verify() (bool, Verifier, error)
Verify(tenantID string, username string, password string) (bool, Verifier, error)
}
32 changes: 0 additions & 32 deletions pkg/store/oauth2.go

This file was deleted.