Skip to content
Merged

Dev #65

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
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/casbin/mongodb-adapter/v3 v3.5.0
github.com/coreos/go-oidc/v3 v3.7.0
github.com/gin-contrib/pprof v1.4.0
github.com/go-openapi/spec v0.20.9
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/google/uuid v1.4.0
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.0
Expand All @@ -25,6 +26,7 @@ require (
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
github.com/smartystreets/goconvey v1.8.1
github.com/spf13/cast v1.5.1
golang.org/x/time v0.3.0
google.golang.org/grpc v1.59.0
gorm.io/driver/mysql v1.5.2
gorm.io/driver/postgres v1.5.4
Expand All @@ -42,11 +44,16 @@ require (
github.com/emirpasic/gods v1.12.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/go-jose/go-jose/v3 v3.0.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/swag v0.19.15 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
Expand All @@ -61,6 +68,7 @@ require (
golang.org/x/arch v0.5.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231016165738-49dd2c1f3d0b // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

require (
Expand Down
21 changes: 21 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ github.com/glebarez/sqlite v1.10.0 h1:u4gt8y7OND/cCei/NMHmfbLxF6xP2wgKcT/BJf2pYk
github.com/glebarez/sqlite v1.10.0/go.mod h1:IJ+lfSOmiekhQsFTJRx/lHtGYmCdtAiTaf5wI9u5uHA=
github.com/go-jose/go-jose/v3 v3.0.0 h1:s6rrhirfEP/CGIoc6p+PZAeogN2SxKav6Wp7+dyMWVo=
github.com/go-jose/go-jose/v3 v3.0.0/go.mod h1:RNkWWRld676jZEYoV3+XK8L2ZnNSvIsxFMht0mSX+u8=
github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
github.com/go-openapi/jsonpointer v0.19.5 h1:gZr+CIYByUqjcgeLXnQu2gHYQC9o73G2XUeOFYEICuY=
github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
github.com/go-openapi/jsonreference v0.20.0 h1:MYlu0sBgChmCfJxxUKZ8g1cPWFOB37YSZqewK7OKeyA=
github.com/go-openapi/jsonreference v0.20.0/go.mod h1:Ag74Ico3lPc+zR+qjn4XBUmXymS4zJbYVCZmcgkasdo=
github.com/go-openapi/spec v0.20.9 h1:xnlYNQAwKd2VQRRfwTEI0DcK+2cbuvI/0c7jx3gA8/8=
github.com/go-openapi/spec v0.20.9/go.mod h1:2OpW+JddWPrpXSCIX8eOx7lZ5iyuWj3RYR6VaaBKcWA=
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
github.com/go-openapi/swag v0.19.15 h1:D2NRCBzS9/pEY3gP9Nl8aDqGUcPFrwG2p+CNFrLyrCM=
github.com/go-openapi/swag v0.19.15/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
Expand Down Expand Up @@ -193,6 +203,8 @@ github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9Y
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
Expand Down Expand Up @@ -222,6 +234,10 @@ github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.7.6 h1:8yTIVnZgCoiM1TgqoeTl+LfU5Jg6/xL3QhGQnimLYnA=
github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
Expand All @@ -241,6 +257,7 @@ github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJ
github.com/montanaflynn/stats v0.7.0/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE=
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nfjBill/gorm-driver-dm v1.0.1 h1:cE36dxtmfbGtkWWBuPCfEXaR2kgoF0nwQH7CGuMkaEM=
github.com/nfjBill/gorm-driver-dm v1.0.1/go.mod h1:QGyYMleRqsM8759DJds5FgFlHZ6j+eySwgktsmq6+oc=
github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo=
Expand Down Expand Up @@ -410,6 +427,8 @@ golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
Expand All @@ -431,6 +450,7 @@ google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
Expand All @@ -440,6 +460,7 @@ gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"os"
"time"

"github.com/gin-gonic/gin"
"gorm.io/gorm/logger"

"github.com/mss-boot-io/mss-boot/core/logger/writer"
Expand Down Expand Up @@ -73,6 +74,7 @@ func (e *Logger) Init() {
Colorful: true,
})

gin.DefaultWriter = output
}

func (e *Logger) GormLevel() logger.LogLevel {
Expand Down
46 changes: 46 additions & 0 deletions pkg/config/rate_limit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package config

import (
"context"
"golang.org/x/time/rate"
)

/*
* @Author: lwnmengjing<lwnmengjing@qq.com>
* @Date: 2023/11/4 10:41:05
* @Last Modified by: lwnmengjing<lwnmengjing@qq.com>
* @Last Modified time: 2023/11/4 10:41:05
*/

type Limiter interface {
Allow() bool
Wait(context.Context) error
}

type LimiterName string

const (
SingleLimiter LimiterName = "single"
)

type RateLimit struct {
// 限流器类型
Name LimiterName `json:"name" yaml:"name"`
// 限流器配置
Rate float64 `json:"rate" yaml:"rate"`
// 限流器最大存储令牌数
Bursts int `json:"bursts" yaml:"bursts"`
}

func (e *RateLimit) String() string {
return string(e.Name)
}

func (e *RateLimit) Init() Limiter {
switch e.Name {
case SingleLimiter:
return rate.NewLimiter(rate.Limit(e.Rate), e.Bursts)
default:
return nil
}
}
52 changes: 52 additions & 0 deletions pkg/middlewares/limit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package middlewares

import (
"context"
"net/http"

"github.com/gin-gonic/gin"
"github.com/mss-boot-io/mss-boot/pkg/response"
)

/*
* @Author: lwnmengjing<lwnmengjing@qq.com>
* @Date: 2023/11/4 10:26:49
* @Last Modified by: lwnmengjing<lwnmengjing@qq.com>
* @Last Modified time: 2023/11/4 10:26:49
*/

var DefaultLimiter Limiter

type Limiter interface {
Allow() bool
Wait(context.Context) error
}

// LimitMiddleware 限流中间件
func LimitMiddleware() gin.HandlerFunc {
if DefaultLimiter == nil {
return func(c *gin.Context) {
c.Next()
}
}
return func(c *gin.Context) {
var count int8
GATE:
count++
if !DefaultLimiter.Allow() {
if err := DefaultLimiter.Wait(c.Request.Context()); err != nil {
api := response.Make(c)
api.AddError(err).Log.Error("rate limit wait error", "err", err)
api.Err(http.StatusInternalServerError)
return
}
if count > 5 {
api := response.Make(c)
api.Err(http.StatusTooManyRequests)
return
}
goto GATE
}
c.Next()
}
}
10 changes: 0 additions & 10 deletions pkg/response/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"fmt"

"github.com/gin-gonic/gin"

"github.com/mss-boot-io/mss-boot/virtual/model"
)

const (
Expand Down Expand Up @@ -43,11 +41,3 @@ type Searcher interface {
GetPage() int64
GetPageSize() int64
}

// VirtualAction virtual action
type VirtualAction interface {
String() string
Handler() gin.HandlerFunc
SetModel(key string, m *model.Model)
GetModel(ctx *gin.Context) *model.Model
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package authentic
package actions

/*
* @Author: lwnmengjing
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package authentic
package actions

/*
* @Author: lwnmengjing<lwnmengjing@qq.com>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package authentic
package actions

/*
* @Author: lwnmengjing
Expand All @@ -9,7 +9,6 @@ package authentic

import (
"errors"
"fmt"
"net/http"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -64,9 +63,7 @@ func (e *Control) Handler() gin.HandlerFunc {
break
}
default:
response.Error(c,
http.StatusNotImplemented,
fmt.Errorf("not implemented"))
response.Make(c).Err(http.StatusNotImplemented, "not implemented")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package authentic
package actions

/*
* @Author: lwnmengjing<lwnmengjing@qq.com>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package authentic
package actions

/*
* @Author: lwnmengjing
Expand All @@ -9,7 +9,6 @@ package authentic

import (
"errors"
"fmt"
"net/http"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -59,9 +58,7 @@ func (e *Delete) Handler() gin.HandlerFunc {
e.deleteGorm(c, v)
return
}
response.Error(c,
http.StatusNotImplemented,
fmt.Errorf("not implemented"))
response.Make(c).Err(http.StatusNotImplemented, "not implemented")
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package authentic
package actions

/*
* @Author: lwnmengjing<lwnmengjing@qq.com>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package authentic
package actions

/*
* @Author: lwnmengjing
Expand All @@ -9,7 +9,6 @@ package authentic

import (
"errors"
"fmt"
"net/http"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -52,9 +51,7 @@ func (e *Get) Handler() gin.HandlerFunc {
e.getMgm(c, e.Key)
return
}
response.Error(c,
http.StatusNotImplemented,
fmt.Errorf("not implemented"))
response.Make(c).Err(http.StatusNotImplemented, "not implemented")
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package authentic
package actions

/*
* @Author: lwnmengjing<lwnmengjing@qq.com>
Expand Down Expand Up @@ -44,6 +44,12 @@ func (e *Search) searchGorm(c *gin.Context) {
gorms.MakeCondition(req),
gorms.Paginate(int(req.GetPageSize()), int(req.GetPage())),
)
if err := query.Limit(-1).Offset(-1).Count(&count).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
api.AddError(err).Log.ErrorContext(c, "Search error", "error", err)
api.Err(http.StatusInternalServerError)
return
}

rows, err := query.Rows()
if err != nil {
api.AddError(err).Log.ErrorContext(c, "Search error", "error", err)
Expand All @@ -56,18 +62,18 @@ func (e *Search) searchGorm(c *gin.Context) {
m = pkg.TablerDeepCopy(e.ModelGorm)
err = db.ScanRows(rows, m)
if err != nil {
api.AddError(err).Log.ErrorContext(c, "Search error", "error", err)
api.AddError(err).Log.ErrorContext(c, "search error", "error", err)
api.Err(http.StatusInternalServerError)
return
}
items = append(items, m)
}
err = query.Limit(-1).Offset(-1).Count(&count).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
api.AddError(err).Log.ErrorContext(c, "Search error", "error", err)
api.AddError(err).Log.ErrorContext(c, "search error", "error", err)
api.Err(http.StatusInternalServerError)
return
}
response.PageOK(c, items, count, req.GetPage(), req.GetPageSize(), "search success")
api.PageOK(items, count, req.GetPage(), req.GetPageSize(), "search success")
c.Next()
}
Loading