Skip to content

Commit

Permalink
Merge branch 'hotfix/application'
Browse files Browse the repository at this point in the history
# Conflicts:
#	go.mod
  • Loading branch information
Dot-Liu committed Oct 18, 2022
2 parents e3806df + c9602ce commit 1b4036c
Show file tree
Hide file tree
Showing 43 changed files with 1,813 additions and 42 deletions.
12 changes: 12 additions & 0 deletions app/apinto/profession.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ func ApintoProfession() []*eosc.ProfessionConfig {
Label: "限流策略",
Desc: "限流策略",
},
{
Id: "eolinker.com:apinto:strategy-cache",
Name: "cache",
Label: "缓存策略",
Desc: "缓存策略",
},
{
Id: "eolinker.com:apinto:strategy-grey",
Name: "grey",
Label: "灰度策略",
Desc: "灰度策略",
},
},
Mod: eosc.ProfessionConfig_Worker,
},
Expand Down
8 changes: 6 additions & 2 deletions app/apinto/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import (
"github.com/eolinker/apinto/drivers/plugins/strategy"
http_router "github.com/eolinker/apinto/drivers/router/http-router"
service "github.com/eolinker/apinto/drivers/service"
limiting_stragety "github.com/eolinker/apinto/drivers/strategy/limiting-stragety"
cache_strategy "github.com/eolinker/apinto/drivers/strategy/cache-strategy"
grey_strategy "github.com/eolinker/apinto/drivers/strategy/grey-strategy"
limiting_strategy "github.com/eolinker/apinto/drivers/strategy/limiting-strategy"
template "github.com/eolinker/apinto/drivers/template"

"github.com/eolinker/eosc"
Expand Down Expand Up @@ -89,5 +91,7 @@ func Register(extenderRegister eosc.IExtenderDriverRegister) {
proxy_rewriteV2.Register(extenderRegister)

strategy.Register(extenderRegister)
limiting_stragety.Register(extenderRegister)
limiting_strategy.Register(extenderRegister)
cache_strategy.Register(extenderRegister)
grey_strategy.Register(extenderRegister)
}
2 changes: 1 addition & 1 deletion application/auth/aksk/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func NewFactory() auth.IAuthFactory {
typ := reflect.TypeOf((*Config)(nil))
render, _ := schema.Generate(typ, nil)

return &factory{configType: typ, render: render}
return &factory{configType: typ, render: render, userType: reflect.TypeOf((*User)(nil))}
}

func toId(tokenName, position string) string {
Expand Down
1 change: 1 addition & 0 deletions application/auth/jwt/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (j *jwt) Set(app application.IApp, users []application.ITransformConfig) {
HideCredential: v.HideCredential,
TokenName: j.tokenName,
Position: j.position,
App: app,
})
}
j.users.Set(app.Id(), infos)
Expand Down
15 changes: 1 addition & 14 deletions drivers/discovery/consul/clients.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package consul

import (
"strconv"
"strings"

"github.com/eolinker/apinto/discovery"
Expand Down Expand Up @@ -75,19 +74,7 @@ func getNodesFromClient(client *api.Client, service string) []discovery.INode {

nodes := make([]discovery.INode, 0, len(serviceEntryArr))
for _, serviceEntry := range serviceEntryArr {
nodeAddr := serviceEntry.Node.Address
addrSlide := append(strings.Split(nodeAddr, ":"))
ip := addrSlide[0]
var port int
if len(addrSlide) > 1 {
port, err = strconv.Atoi(addrSlide[1])
if err != nil {
log.Error(err)
continue
}
}

newNode := discovery.NewNode(serviceEntry.Service.Meta, serviceEntry.Node.ID, ip, port)
newNode := discovery.NewNode(serviceEntry.Service.Meta, serviceEntry.Node.ID, serviceEntry.Service.Address, serviceEntry.Service.Port)
nodes = append(nodes, newNode)
}

Expand Down
12 changes: 6 additions & 6 deletions drivers/plugins/ip-restriction/restriction.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ func (I *IPHandler) Start() error {
}

func (I *IPHandler) Reset(conf interface{}, workers map[eosc.RequireId]eosc.IWorker) error {
confObj, err := I.check(conf)
if err != nil {
return err
}
I.filter = confObj.genFilter()
return nil
confObj, err := I.check(conf)
if err != nil {
return err
}
I.filter = confObj.genFilter()
return nil
}

func (I *IPHandler) Stop() error {
Expand Down
8 changes: 8 additions & 0 deletions drivers/resources/redis/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package redis

type Config struct {
Enable bool `json:"enable"`
Addrs []string `json:"addrs" label:"redis 节点列表"`
Username string `json:"username"`
Password string `json:"password"`
}
95 changes: 95 additions & 0 deletions drivers/resources/redis/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package redis

import (
"context"
"github.com/eolinker/apinto/resources"
"github.com/eolinker/eosc"
"github.com/eolinker/eosc/env"
"github.com/eolinker/eosc/log"
"github.com/go-redis/redis/v8"
"reflect"
)

type Controller struct {
current *_Cacher
config Config
}

func (m *Controller) ConfigType() reflect.Type {
return configType
}
func (m *Controller) shutdown() {
oldClient := m.current
if oldClient != nil {
m.current = nil
resources.ReplaceCacher()
oldClient.client.Close()
}
}
func (m *Controller) Set(conf interface{}) (err error) {
config, ok := conf.(*Config)
if ok && config != nil {
old := m.config
m.config = *config

if reflect.DeepEqual(old, m.config) {
return nil
}

if m.config.Enable && len(m.config.Addrs) > 0 {

client := redis.NewClusterClient(&redis.ClusterOptions{
Addrs: m.config.Addrs,
Username: m.config.Username,
Password: m.config.Password,
})

if res, errPing := client.Ping(context.Background()).Result(); errPing != nil {
log.Info("ping redis:", res, " error:", err)
client.Close()
return errPing
}

if env.Process() == eosc.ProcessWorker {
if m.current == nil {
m.current = newCacher(client)
resources.ReplaceCacher(m.current)
} else {
m.current.client = client
}
} else {
client.Close()
}
return nil
}
}
oldClient := m.current
if oldClient != nil {
resources.ReplaceCacher()
m.current = nil
oldClient.client.Close()
}

return nil
}

func (m *Controller) Get() interface{} {
return m.config
}

func (m *Controller) Mode() eosc.SettingMode {
return eosc.SettingModeSingleton
}

func (m *Controller) Check(cfg interface{}) (profession, name, driver, desc string, err error) {
err = eosc.ErrorUnsupportedKind
return
}

func (m *Controller) AllWorkers() []string {
return []string{"redis@setting"}
}

func NewController() *Controller {
return &Controller{}
}
21 changes: 21 additions & 0 deletions drivers/resources/redis/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package redis

import (
"github.com/eolinker/eosc"
"github.com/eolinker/eosc/setting"
"reflect"
)

var (
singleton *Controller
_ eosc.ISetting = singleton
configType = reflect.TypeOf(new(Config))
)

func init() {
singleton = NewController()
}

func Register(register eosc.IExtenderDriverRegister) {
setting.RegisterSetting("redis", singleton)
}
65 changes: 65 additions & 0 deletions drivers/resources/redis/redis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package redis

import (
"context"
"github.com/eolinker/apinto/resources"
"github.com/go-redis/redis/v8"
"time"
)

var (
_ resources.ICache = (*_Cacher)(nil)
)

type _Cacher struct {
client *redis.ClusterClient
}

func (r *_Cacher) Close() error {
if r.client == nil {
e := r.client.Close()
r.client = nil
return e
}
return nil
}

func (r *_Cacher) Set(ctx context.Context, key string, value []byte, expiration time.Duration) error {

return r.client.Set(ctx, key, value, expiration).Err()
}

func (r *_Cacher) SetNX(ctx context.Context, key string, value []byte, expiration time.Duration) (bool, error) {

return r.client.SetNX(ctx, key, value, expiration).Result()
}

func (r *_Cacher) DecrBy(ctx context.Context, key string, decrement int64) (int64, error) {

return r.client.DecrBy(ctx, key, decrement).Result()
}

func (r *_Cacher) IncrBy(ctx context.Context, key string, decrement int64) (int64, error) {
return r.client.IncrBy(ctx, key, decrement).Result()
}

func (r *_Cacher) Get(ctx context.Context, key string) ([]byte, error) {
return r.client.Get(ctx, key).Bytes()

}

func (r *_Cacher) GetDel(ctx context.Context, key string) ([]byte, error) {
return r.client.GetDel(ctx, key).Bytes()

}

func (r *_Cacher) Del(ctx context.Context, keys ...string) (int64, error) {
return r.client.Del(ctx, keys...).Result()
}

func newCacher(client *redis.ClusterClient) *_Cacher {
if client == nil {
return nil
}
return &_Cacher{client: client}
}
11 changes: 7 additions & 4 deletions drivers/router/http-router/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ type Handler struct {
}

func (h *Handler) ServeHTTP(ctx eocontext.EoContext) {

httpContext, err := http_context.Assert(ctx)
if err != nil {
return
}

if h.disable {
httpContext, err := http_context.Assert(ctx)
if err != nil {
return
}
httpContext.Response().SetStatus(http.StatusNotFound, "")
httpContext.Response().SetBody([]byte("router disable"))
httpContext.FastFinish()
Expand All @@ -36,6 +38,7 @@ func (h *Handler) ServeHTTP(ctx eocontext.EoContext) {
//Set Label
ctx.SetLabel("api", h.routerName)
ctx.SetLabel("service", h.serviceName)
ctx.SetLabel("ip", httpContext.Request().ReadIP())
ctx.SetFinish(&h.finisher)
ctx.SetCompleteHandler(h.completeHandler)
ctx.SetApp(h.service)
Expand Down
Loading

0 comments on commit 1b4036c

Please sign in to comment.