mirror of
https://github.com/eolinker/apinto
synced 2025-12-24 13:28:15 +08:00
优化结构
This commit is contained in:
@@ -26,9 +26,9 @@ import (
|
||||
proxy_rewriteV2 "github.com/eolinker/apinto/drivers/plugins/proxy_rewrite_v2"
|
||||
rate_limiting "github.com/eolinker/apinto/drivers/plugins/rate-limiting"
|
||||
response_rewrite "github.com/eolinker/apinto/drivers/plugins/response-rewrite"
|
||||
http_router "github.com/eolinker/apinto/v2/router-http-driver"
|
||||
service_http "github.com/eolinker/apinto/v2/service-http"
|
||||
template "github.com/eolinker/apinto/v2/template"
|
||||
http_router "github.com/eolinker/apinto/drivers/router/http-router"
|
||||
service "github.com/eolinker/apinto/drivers/service"
|
||||
template "github.com/eolinker/apinto/drivers/template"
|
||||
//upstream_http "github.com/eolinker/apinto/drivers/upstream/upstream-http"
|
||||
plugin_manager "github.com/eolinker/apinto/plugin-manager"
|
||||
"github.com/eolinker/eosc"
|
||||
@@ -49,7 +49,7 @@ func Register(extenderRegister eosc.IExtenderDriverRegister) {
|
||||
template.Register(extenderRegister)
|
||||
|
||||
// service
|
||||
service_http.Register(extenderRegister)
|
||||
service.Register(extenderRegister)
|
||||
|
||||
////// upstream
|
||||
//upstream_http.Register(extenderRegister)
|
||||
|
||||
@@ -11,8 +11,8 @@ type Config struct {
|
||||
Host []string `json:"host" yaml:"host" label:"域名"`
|
||||
Path string `json:"location"`
|
||||
Rules []Rule `json:"rules" yaml:"rules" label:"路由规则"`
|
||||
Service eosc.RequireId `json:"Service" yaml:"Service" skill:"github.com/eolinker/apinto/scheme/http/http.IService" required:"true" label:"目标服务"`
|
||||
Template eosc.RequireId `json:"template" yaml:"template" skill:"github.com/eolinker/apinto/scheme/http/http.ITemplate" required:"true" label:"插件模版"`
|
||||
Service eosc.RequireId `json:"Service" yaml:"Service" skill:"github.com/eolinker/apinto/service/service.IService" required:"true" label:"目标服务"`
|
||||
Template eosc.RequireId `json:"template" yaml:"template" skill:"github.com/eolinker/apinto/template/template.ITemplate" required:"true" label:"插件模版"`
|
||||
Disable bool `json:"disable" yaml:"disable" label:"禁用路由"`
|
||||
Plugins map[string]*plugin.Config `json:"plugins" yaml:"plugins" label:"插件配置"`
|
||||
Retry int `json:"retry" label:"重试次数" yaml:"retry"`
|
||||
@@ -2,15 +2,15 @@ package http_router
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
router_http_manager "github.com/eolinker/apinto/v2/router-http-manager"
|
||||
"github.com/eolinker/apinto/drivers/router/http-router/manager"
|
||||
"github.com/eolinker/apinto/service"
|
||||
"github.com/eolinker/apinto/template"
|
||||
"github.com/eolinker/eosc/utils/config"
|
||||
"reflect"
|
||||
|
||||
"github.com/eolinker/apinto/plugin"
|
||||
"github.com/eolinker/eosc/common/bean"
|
||||
|
||||
service "github.com/eolinker/apinto/v2"
|
||||
|
||||
"github.com/eolinker/eosc"
|
||||
)
|
||||
|
||||
@@ -18,7 +18,15 @@ import (
|
||||
type HTTPRouterDriver struct {
|
||||
configType reflect.Type
|
||||
pluginManager plugin.IPluginManager
|
||||
routerManger router_http_manager.IManger
|
||||
routerManger manager.IManger
|
||||
}
|
||||
|
||||
func (h *HTTPRouterDriver) Check(v interface{}, workers map[eosc.RequireId]eosc.IWorker) error {
|
||||
_, _, _, err := h.check(v, workers)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//NewHTTPRouter 创建一个http路由驱动
|
||||
@@ -48,20 +56,31 @@ func (h *HTTPRouterDriver) Create(id, name string, v interface{}, workers map[eo
|
||||
}
|
||||
|
||||
//check 检查http路由驱动配置
|
||||
func (h *HTTPRouterDriver) check(v interface{}, workers map[eosc.RequireId]eosc.IWorker) (*Config, service.IService, error) {
|
||||
func (h *HTTPRouterDriver) check(v interface{}, workers map[eosc.RequireId]eosc.IWorker) (*Config, service.IService, template.ITemplate, error) {
|
||||
conf, ok := v.(*Config)
|
||||
if !ok {
|
||||
return nil, nil, fmt.Errorf("get %s but %s %w", config.TypeNameOf(v), config.TypeNameOf(new(Config)), eosc.ErrorRequire)
|
||||
return nil, nil, nil, fmt.Errorf("get %s but %s %w", config.TypeNameOf(v), config.TypeNameOf(new(Config)), eosc.ErrorRequire)
|
||||
}
|
||||
ser, has := workers[conf.Service]
|
||||
if !has {
|
||||
return nil, nil, fmt.Errorf("target %w", eosc.ErrorRequire)
|
||||
return nil, nil, nil, fmt.Errorf("target %s: %w", conf.Service, eosc.ErrorRequire)
|
||||
}
|
||||
target, ok := ser.(service.IService)
|
||||
if !ok {
|
||||
return nil, nil, fmt.Errorf("target name: %s type of %s,target %w", conf.Service, config.TypeNameOf(ser), eosc.ErrorNotGetSillForRequire)
|
||||
return nil, nil, nil, fmt.Errorf("target name: %s type of %s,target %w", conf.Service, config.TypeNameOf(ser), eosc.ErrorNotGetSillForRequire)
|
||||
}
|
||||
return conf, target, nil
|
||||
var tmp template.ITemplate
|
||||
if conf.Template != "" {
|
||||
tp, has := workers[conf.Template]
|
||||
if !has {
|
||||
return nil, nil, nil, fmt.Errorf("target %s %w", conf.Template, eosc.ErrorRequire)
|
||||
}
|
||||
tmp, ok = tp.(template.ITemplate)
|
||||
if !ok {
|
||||
return nil, nil, nil, fmt.Errorf("target name: %s type of %s,target %w", conf.Template, config.TypeNameOf(tp), eosc.ErrorNotGetSillForRequire)
|
||||
}
|
||||
}
|
||||
return conf, target, tmp, nil
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package http_router
|
||||
|
||||
import (
|
||||
service "github.com/eolinker/apinto/v2"
|
||||
"github.com/eolinker/apinto/service"
|
||||
"github.com/eolinker/eosc/eocontext"
|
||||
)
|
||||
|
||||
5
drivers/router/http-router/manager/append.go
Normal file
5
drivers/router/http-router/manager/append.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package manager
|
||||
|
||||
import http_router "github.com/eolinker/apinto/router/http-router"
|
||||
|
||||
type AppendRule = http_router.AppendRule
|
||||
@@ -1,20 +1,22 @@
|
||||
package router_http_manager
|
||||
package manager
|
||||
|
||||
import "github.com/eolinker/apinto/v2/router"
|
||||
import (
|
||||
http_router "github.com/eolinker/apinto/router/http-router"
|
||||
)
|
||||
|
||||
var _ IRouterData = (*RouterData)(nil)
|
||||
|
||||
type IRouterData interface {
|
||||
Set(id string, port int, hosts []string, method []string, path string, append []AppendRule, router router.IRouterHandler) IRouterData
|
||||
Set(id string, port int, hosts []string, method []string, path string, append []AppendRule, router http_router.IRouterHandler) IRouterData
|
||||
Delete(id string) IRouterData
|
||||
Parse() (router.IMatcher, error)
|
||||
Parse() (http_router.IMatcher, error)
|
||||
}
|
||||
type RouterData struct {
|
||||
data map[string]*Router
|
||||
}
|
||||
|
||||
func (rs *RouterData) Parse() (router.IMatcher, error) {
|
||||
root := router.NewRoot()
|
||||
func (rs *RouterData) Parse() (http_router.IMatcher, error) {
|
||||
root := http_router.NewRoot()
|
||||
for _, v := range rs.data {
|
||||
err := root.Add(v.Id, v.HttpHandler, v.Port, v.Hosts, v.Method, v.Path, v.Appends)
|
||||
if err != nil {
|
||||
@@ -28,7 +30,7 @@ func (rs *RouterData) set(r *Router) *RouterData {
|
||||
rs.data[r.Id] = r
|
||||
return rs
|
||||
}
|
||||
func (rs *RouterData) Set(id string, port int, hosts []string, method []string, path string, append []AppendRule, router router.IRouterHandler) IRouterData {
|
||||
func (rs *RouterData) Set(id string, port int, hosts []string, method []string, path string, append []AppendRule, router http_router.IRouterHandler) IRouterData {
|
||||
r := &Router{
|
||||
Id: id,
|
||||
Port: port,
|
||||
@@ -1,6 +1,6 @@
|
||||
package router_http_manager
|
||||
package manager
|
||||
|
||||
import "github.com/eolinker/apinto/v2/router"
|
||||
import http_router "github.com/eolinker/apinto/router/http-router"
|
||||
|
||||
type Router struct {
|
||||
Id string
|
||||
@@ -9,5 +9,5 @@ type Router struct {
|
||||
Method []string
|
||||
Path string
|
||||
Appends []AppendRule
|
||||
HttpHandler router.IRouterHandler
|
||||
HttpHandler http_router.IRouterHandler
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package router_http_manager
|
||||
package manager
|
||||
|
||||
import (
|
||||
"github.com/eolinker/apinto/plugin"
|
||||
@@ -1,10 +1,10 @@
|
||||
package router_http_manager
|
||||
package manager
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
http_context "github.com/eolinker/apinto/node/http-context"
|
||||
"github.com/eolinker/apinto/v2/router"
|
||||
http_router "github.com/eolinker/apinto/router/http-router"
|
||||
"github.com/eolinker/eosc/config"
|
||||
eoscContext "github.com/eolinker/eosc/eocontext"
|
||||
http_service "github.com/eolinker/eosc/eocontext/http-context"
|
||||
@@ -19,19 +19,19 @@ var _ IManger = (*Manager)(nil)
|
||||
var notFound = new(NotFoundHandler)
|
||||
|
||||
type IManger interface {
|
||||
Set(id string, port int, hosts []string, method []string, path string, append []AppendRule, router router.IRouterHandler) error
|
||||
Set(id string, port int, hosts []string, method []string, path string, append []AppendRule, router http_router.IRouterHandler) error
|
||||
Delete(id string)
|
||||
}
|
||||
|
||||
type Manager struct {
|
||||
lock sync.RWMutex
|
||||
matcher router.IMatcher
|
||||
matcher http_router.IMatcher
|
||||
|
||||
routersData IRouterData
|
||||
globalFilters eoscContext.IChain
|
||||
}
|
||||
|
||||
func (m *Manager) Set(id string, port int, hosts []string, method []string, path string, append []AppendRule, router router.IRouterHandler) error {
|
||||
func (m *Manager) Set(id string, port int, hosts []string, method []string, path string, append []AppendRule, router http_router.IRouterHandler) error {
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
routersData := m.routersData.Set(id, port, hosts, method, path, append, router)
|
||||
@@ -1,10 +1,12 @@
|
||||
package http_router
|
||||
|
||||
import (
|
||||
"github.com/eolinker/apinto/drivers/router/http-router/manager"
|
||||
"github.com/eolinker/apinto/plugin"
|
||||
service "github.com/eolinker/apinto/v2"
|
||||
"github.com/eolinker/apinto/v2/router"
|
||||
router_http_manager "github.com/eolinker/apinto/v2/router-http-manager"
|
||||
http_router "github.com/eolinker/apinto/router/http-router"
|
||||
"github.com/eolinker/apinto/service"
|
||||
"github.com/eolinker/apinto/template"
|
||||
|
||||
"github.com/eolinker/eosc"
|
||||
"github.com/eolinker/eosc/eocontext"
|
||||
"time"
|
||||
@@ -17,7 +19,7 @@ type HttpRouter struct {
|
||||
handler *Handler
|
||||
|
||||
pluginManager plugin.IPluginManager
|
||||
routerManager router_http_manager.IManger
|
||||
routerManager manager.IManger
|
||||
}
|
||||
|
||||
func (h *HttpRouter) Destroy() error {
|
||||
@@ -59,11 +61,11 @@ func (h *HttpRouter) reset(conf interface{}, workers map[eosc.RequireId]eosc.IWo
|
||||
var plugins eocontext.IChain
|
||||
if cfg.Template != "" {
|
||||
templateWorker, has := workers[cfg.Template]
|
||||
if !has || !templateWorker.CheckSkill(service.TemplateSkill) {
|
||||
if !has || !templateWorker.CheckSkill(template.TemplateSkill) {
|
||||
return eosc.ErrorNotGetSillForRequire
|
||||
}
|
||||
template := templateWorker.(service.ITemplate)
|
||||
plugins = template.Create(h.id, cfg.Plugins)
|
||||
tp := templateWorker.(template.ITemplate)
|
||||
plugins = tp.Create(h.id, cfg.Plugins)
|
||||
} else {
|
||||
plugins = h.pluginManager.CreateRequest(h.id, cfg.Plugins)
|
||||
}
|
||||
@@ -79,9 +81,9 @@ func (h *HttpRouter) reset(conf interface{}, workers map[eosc.RequireId]eosc.IWo
|
||||
service: serviceHandler,
|
||||
filters: plugins,
|
||||
}
|
||||
appendRule := make([]router.AppendRule, 0, len(cfg.Rules))
|
||||
appendRule := make([]http_router.AppendRule, 0, len(cfg.Rules))
|
||||
for _, r := range cfg.Rules {
|
||||
appendRule = append(appendRule, router.AppendRule{
|
||||
appendRule = append(appendRule, http_router.AppendRule{
|
||||
Type: r.Type,
|
||||
Name: r.Name,
|
||||
Pattern: r.Value,
|
||||
@@ -1,4 +1,4 @@
|
||||
package service_http
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
@@ -1,4 +1,4 @@
|
||||
package service_http
|
||||
package service
|
||||
|
||||
import (
|
||||
"github.com/eolinker/eosc/utils/schema"
|
||||
@@ -33,9 +33,9 @@ func (d *driver) Render() interface{} {
|
||||
func (d *driver) Create(id, name string, v interface{}, workers map[eosc.RequireId]eosc.IWorker) (eosc.IWorker, error) {
|
||||
|
||||
w := &serviceWorker{
|
||||
id: id,
|
||||
name: name,
|
||||
driver: d.driver,
|
||||
id: id,
|
||||
name: name,
|
||||
driver: d.driver,
|
||||
Service: Service{},
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package service_http
|
||||
package service
|
||||
|
||||
import (
|
||||
round_robin "github.com/eolinker/apinto/upstream/round-robin"
|
||||
@@ -1,4 +1,4 @@
|
||||
package service_http
|
||||
package service
|
||||
|
||||
import (
|
||||
"github.com/eolinker/apinto/upstream/balance"
|
||||
@@ -1,4 +1,4 @@
|
||||
package service_http
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -86,7 +86,7 @@ func (s *Service) Reset(conf interface{}, workers map[eosc.RequireId]eosc.IWorke
|
||||
|
||||
s.timeout = time.Duration(data.Timeout) * time.Millisecond
|
||||
s.BalanceHandler = balanceHandler
|
||||
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
package service_http
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/eolinker/apinto/v2"
|
||||
"github.com/eolinker/apinto/service"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -2,14 +2,12 @@ package template
|
||||
|
||||
import (
|
||||
"github.com/eolinker/apinto/plugin"
|
||||
service "github.com/eolinker/apinto/v2"
|
||||
"github.com/eolinker/apinto/template"
|
||||
|
||||
"github.com/eolinker/eosc"
|
||||
"github.com/eolinker/eosc/eocontext"
|
||||
"github.com/eolinker/eosc/utils/config"
|
||||
)
|
||||
|
||||
var it service.ITemplate = (*Template)(nil)
|
||||
|
||||
type Template struct {
|
||||
id string
|
||||
name string
|
||||
@@ -53,7 +51,7 @@ func (t *Template) Stop() error {
|
||||
}
|
||||
|
||||
func (t *Template) CheckSkill(skill string) bool {
|
||||
return skill == config.TypeNameOf(it)
|
||||
return template.CheckSkill(skill)
|
||||
}
|
||||
|
||||
func (t *Template) Create(id string, conf map[string]*plugin.Config) eocontext.IChain {
|
||||
@@ -14,8 +14,6 @@ type Config struct {
|
||||
type IPluginManager interface {
|
||||
CreateRequest(id string, conf map[string]*Config) eocontext.IChain
|
||||
GetConfigType(name string) (reflect.Type, bool)
|
||||
|
||||
//CreateUpstream(id string, conf map[string]*Config) IPlugin
|
||||
}
|
||||
|
||||
func MergeConfig(high, low map[string]*Config) map[string]*Config {
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
package professions
|
||||
|
||||
const (
|
||||
SpaceProfession = "professions"
|
||||
)
|
||||
|
||||
//type Professions struct {
|
||||
// *professions.Professions
|
||||
//}
|
||||
//
|
||||
//func NewProfessions() (*Professions, error) {
|
||||
//
|
||||
// p := &Professions{
|
||||
// Professions: professions.NewProfessions(),
|
||||
// }
|
||||
// p.Professions.Reset(ApintoProfession())
|
||||
// return p, nil
|
||||
//}
|
||||
//
|
||||
//func (p *Professions) Reset([]*eosc.ProfessionConfig) {
|
||||
// return
|
||||
//}
|
||||
@@ -1,4 +1,4 @@
|
||||
package router
|
||||
package http_router
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type RuleType string
|
||||
type RuleType = string
|
||||
|
||||
const (
|
||||
HttpHeader RuleType = "header"
|
||||
@@ -33,17 +33,17 @@ func Parse(rules []AppendRule) MatcherChecker {
|
||||
ck, _ := checker.Parse(r.Pattern)
|
||||
|
||||
switch strings.ToLower(r.Type) {
|
||||
case "header":
|
||||
case HttpHeader:
|
||||
rls = append(rls, &HeaderChecker{
|
||||
name: r.Name,
|
||||
Checker: ck,
|
||||
})
|
||||
case "query":
|
||||
case HttpQuery:
|
||||
rls = append(rls, &QueryChecker{
|
||||
name: r.Name,
|
||||
Checker: ck,
|
||||
})
|
||||
case "cookie":
|
||||
case HttpCookie:
|
||||
rls = append(rls, &CookieChecker{
|
||||
name: r.Name,
|
||||
Checker: ck,
|
||||
@@ -1,4 +1,4 @@
|
||||
package router
|
||||
package http_router
|
||||
|
||||
import (
|
||||
"github.com/eolinker/apinto/checker"
|
||||
@@ -1,4 +1,4 @@
|
||||
package router
|
||||
package http_router
|
||||
|
||||
import (
|
||||
eoscContext "github.com/eolinker/eosc/eocontext"
|
||||
@@ -1,4 +1,4 @@
|
||||
package router
|
||||
package http_router
|
||||
|
||||
import (
|
||||
"errors"
|
||||
@@ -1,4 +1,4 @@
|
||||
package router
|
||||
package http_router
|
||||
|
||||
import (
|
||||
"testing"
|
||||
@@ -1,22 +1,19 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"github.com/eolinker/apinto/plugin"
|
||||
"github.com/eolinker/eosc"
|
||||
eoscContext "github.com/eolinker/eosc/eocontext"
|
||||
"github.com/eolinker/eosc/utils/config"
|
||||
)
|
||||
|
||||
var (
|
||||
ServiceSkill string
|
||||
TemplateSkill string
|
||||
ServiceSkill string
|
||||
)
|
||||
|
||||
func init() {
|
||||
var s IService
|
||||
ServiceSkill = config.TypeNameOf(&s)
|
||||
var t ITemplate
|
||||
TemplateSkill = config.TypeNameOf(&t)
|
||||
|
||||
}
|
||||
|
||||
type IService interface {
|
||||
@@ -25,11 +22,6 @@ type IService interface {
|
||||
eoscContext.BalanceHandler
|
||||
}
|
||||
|
||||
type ITemplate interface {
|
||||
eosc.IWorker
|
||||
Create(id string, conf map[string]*plugin.Config) eoscContext.IChain
|
||||
}
|
||||
|
||||
//CheckSkill 检查目标技能是否符合
|
||||
func CheckSkill(skill string) bool {
|
||||
return skill == ServiceSkill
|
||||
@@ -1,5 +0,0 @@
|
||||
package router_http_manager
|
||||
|
||||
import "github.com/eolinker/apinto/v2/router"
|
||||
|
||||
type AppendRule = router.AppendRule
|
||||
@@ -1,14 +0,0 @@
|
||||
package router_http_manager
|
||||
|
||||
import (
|
||||
"github.com/eolinker/apinto/v2/router"
|
||||
http_service "github.com/eolinker/eosc/eocontext/http-context"
|
||||
)
|
||||
|
||||
type Routers struct {
|
||||
}
|
||||
|
||||
func (r *Routers) Match(port int, request http_service.IRequestReader) (router.IRouterHandler, bool) {
|
||||
|
||||
return nil, false
|
||||
}
|
||||
Reference in New Issue
Block a user