Skip to content

Commit

Permalink
feat:custom plugin weights
Browse files Browse the repository at this point in the history
  • Loading branch information
tanzhuo committed Nov 16, 2023
1 parent d5c5dc6 commit 2110518
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 34 deletions.
24 changes: 16 additions & 8 deletions plug/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,29 @@ var plugName = "grpc"

type ServiceGrpc struct {
grpc *grpc.Server
weight int
tls bool
serverCrt []byte
serverKey []byte
rootCA []byte
}

type Option func(o *ServiceGrpc)
type Option func(g *ServiceGrpc)

func EnableTls() Option {
return func(o *ServiceGrpc) {
o.tls = true
return func(g *ServiceGrpc) {
g.tls = true
}
}

func Weight(w int) Option {
return func(g *ServiceGrpc) {
g.weight = w
}
}

func (g *ServiceGrpc) Weight() int {
return 500
return g.weight
}

func (g *ServiceGrpc) Name() string {
Expand Down Expand Up @@ -139,11 +146,12 @@ func GetGRPC() *grpc.Server {
}

func Grpc(opts ...Option) plug.Plug {
s := ServiceGrpc{
tls: false,
s := &ServiceGrpc{
tls: false,
weight: 500,
}
for _, option := range opts {
option(&s)
option(s)
}
return &s
return s
}
24 changes: 20 additions & 4 deletions plug/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,24 @@ import (
var plugName = "http"

type ServiceHttp struct {
http *http.Server
http *http.Server
weight int
}

type Option func(h *ServiceHttp)

func Weight(w int) Option {
return func(h *ServiceHttp) {
h.weight = w
}
}

func (h *ServiceHttp) Name() string {
return plugName
}

func (h *ServiceHttp) Weight() int {
return 600
return h.weight
}

func (h *ServiceHttp) Load(b *conf.Bootstrap) (plug.Plug, error) {
Expand Down Expand Up @@ -72,6 +81,13 @@ func GetHTTP() *http.Server {
return boot.GetPlug(plugName).(*ServiceHttp).http
}

func Http() plug.Plug {
return &ServiceHttp{}
func Http(opts ...Option) plug.Plug {
s := &ServiceHttp{
weight: 600,
}

for _, option := range opts {
option(s)
}
return s
}
37 changes: 26 additions & 11 deletions plug/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,27 @@ import (
var plugName = "mysql"

type PlugMysql struct {
dri *sql.Driver
dri *sql.Driver
weight int
}

func (m *PlugMysql) Name() string {
type Option func(db *PlugMysql)

func Weight(w int) Option {
return func(db *PlugMysql) {
db.weight = w
}
}

func (db *PlugMysql) Name() string {
return plugName
}

func (m *PlugMysql) Weight() int {
return 1000
func (db *PlugMysql) Weight() int {
return db.weight
}

func (m *PlugMysql) Load(b *conf.Bootstrap) (plug.Plug, error) {
func (db *PlugMysql) Load(b *conf.Bootstrap) (plug.Plug, error) {
boot.GetHelper().Infof("Initializing database")
drv, err := sql.Open(
b.Data.Database.Driver,
Expand All @@ -47,14 +56,14 @@ func (m *PlugMysql) Load(b *conf.Bootstrap) (plug.Plug, error) {
return nil, err
}

m.dri = drv
db.dri = drv
boot.GetHelper().Infof("Database successfully initialized")
return m, nil
return db, nil
}

func (m *PlugMysql) Unload() error {
func (db *PlugMysql) Unload() error {
boot.GetHelper().Info("message", "Closing the DataBase resources")
if err := m.dri.Close(); err != nil {
if err := db.dri.Close(); err != nil {
boot.GetHelper().Error(err)
return err
}
Expand All @@ -65,6 +74,12 @@ func GetDB() *sql.Driver {
return boot.GetPlug(plugName).(*PlugMysql).dri
}

func Mysql() plug.Plug {
return &PlugMysql{}
func Mysql(opts ...Option) plug.Plug {
db := &PlugMysql{
weight: 1000,
}
for _, opt := range opts {
opt(db)
}
return db
}
23 changes: 19 additions & 4 deletions plug/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,24 @@ import (
var plugName = "redis"

type PlugRedis struct {
rdb *redis.Client
rdb *redis.Client
weight int
}

type Option func(r *PlugRedis)

func Weight(w int) Option {
return func(r *PlugRedis) {
r.weight = w
}
}

func (r *PlugRedis) Name() string {
return plugName
}

func (r *PlugRedis) Weight() int {
return 999
return r.weight
}

func (r *PlugRedis) Load(b *conf.Bootstrap) (plug.Plug, error) {
Expand Down Expand Up @@ -56,6 +65,12 @@ func GetRedis() *redis.Client {
return boot.GetPlug(plugName).(*PlugRedis).rdb
}

func Redis() plug.Plug {
return &PlugRedis{}
func Redis(opts ...Option) plug.Plug {
r := &PlugRedis{
weight: 1001,
}
for _, opt := range opts {
opt(r)
}
return r
}
10 changes: 6 additions & 4 deletions plug/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import (
var plugName = "token"

type PlugToken struct {
t conf.Token
l []LoaderToken
t conf.Token
l []LoaderToken
weight int
}

func (t *PlugToken) Weight() int {
return 300
return t.weight
}

func (t *PlugToken) Name() string {
Expand Down Expand Up @@ -70,7 +71,8 @@ func GetName() string {

func Token(l ...LoaderToken) plug.Plug {
return &PlugToken{
l: l,
weight: 0,
l: l,
}
}

Expand Down
20 changes: 17 additions & 3 deletions plug/tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@ type PlugTracer struct {
weight int
}

type Option func(t *PlugTracer)

func Weight(w int) Option {
return func(t *PlugTracer) {
t.weight = w
}
}

func (t *PlugTracer) Weight() int {
return 700
return t.weight
}

func (t *PlugTracer) Name() string {
Expand Down Expand Up @@ -55,6 +63,12 @@ func (t *PlugTracer) Unload() error {
return nil
}

func Tracer() plug.Plug {
return &PlugTracer{}
func Tracer(opts ...Option) plug.Plug {
t := &PlugTracer{
weight: 700,
}
for _, opt := range opts {
opt(t)
}
return t
}

0 comments on commit 2110518

Please sign in to comment.