mirror of
https://github.com/eolinker/apinto
synced 2025-10-31 03:56:24 +08:00
临时提交
This commit is contained in:
@@ -1,9 +1,12 @@
|
|||||||
package router_http
|
package router_http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/tls"
|
||||||
"errors"
|
"errors"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/eolinker/eosc/config"
|
||||||
|
|
||||||
"github.com/eolinker/eosc/log"
|
"github.com/eolinker/eosc/log"
|
||||||
|
|
||||||
traffic_http_fast "github.com/eolinker/eosc/traffic/traffic-http-fast"
|
traffic_http_fast "github.com/eolinker/eosc/traffic/traffic-http-fast"
|
||||||
@@ -28,12 +31,14 @@ var manager iManager
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
var tf traffic.ITraffic
|
var tf traffic.ITraffic
|
||||||
|
var cfg *config.ListensMsg
|
||||||
bean.Autowired(&tf)
|
bean.Autowired(&tf)
|
||||||
|
bean.Autowired(&cfg)
|
||||||
|
|
||||||
bean.AddInitializingBeanFunc(func() {
|
bean.AddInitializingBeanFunc(func() {
|
||||||
log.Debug("init router manager")
|
log.Debug("init router manager")
|
||||||
|
|
||||||
manager = NewManager(tf)
|
manager = NewManager(tf, cfg)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,13 +61,32 @@ func (m *Manager) Cancel() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//NewManager 创建路由管理器
|
//NewManager 创建路由管理器
|
||||||
func NewManager(tf traffic.ITraffic) *Manager {
|
func NewManager(tf traffic.ITraffic, listenCfg *config.ListensMsg) *Manager {
|
||||||
|
log.Debug("new router manager")
|
||||||
m := &Manager{
|
m := &Manager{
|
||||||
routers: NewRouters(),
|
routers: NewRouters(),
|
||||||
tf: traffic_http_fast.NewHttpTraffic(tf),
|
tf: traffic_http_fast.NewHttpTraffic(tf),
|
||||||
locker: sync.Mutex{},
|
locker: sync.Mutex{},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, cfg := range listenCfg.Listens {
|
||||||
|
port := int(cfg.Port)
|
||||||
|
l, err := tf.ListenTcp("", port)
|
||||||
|
if err != nil {
|
||||||
|
log.Warn("worker listen tcp error:", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if cfg.Scheme == "https" {
|
||||||
|
cert, err := config.NewCert(cfg.Certificate, listenCfg.Dir)
|
||||||
|
if err != nil {
|
||||||
|
log.Warn("worker create certificate error:", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
m.tf.Set(port, traffic_http_fast.NewHttpService(tls.NewListener(l, &tls.Config{GetCertificate: cert.GetCertificate})))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
m.tf.Set(port, traffic_http_fast.NewHttpService(l))
|
||||||
|
}
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,18 +94,27 @@ func NewManager(tf traffic.ITraffic) *Manager {
|
|||||||
func (m *Manager) Add(port int, id string, config *Config) error {
|
func (m *Manager) Add(port int, id string, config *Config) error {
|
||||||
m.locker.Lock()
|
m.locker.Lock()
|
||||||
defer m.locker.Unlock()
|
defer m.locker.Unlock()
|
||||||
|
if port == 0 {
|
||||||
|
srv := m.tf.All()
|
||||||
|
for p, s := range srv {
|
||||||
|
router, _, err := m.routers.Set(p, id, config)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
s.Set(router.Handler)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
router, _, err := m.routers.Set(port, id, config)
|
router, _, err := m.routers.Set(port, id, config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if config.Protocol == "https" {
|
service, has := m.tf.Get(port)
|
||||||
certs := newCerts(config.Cert)
|
if !has {
|
||||||
m.tf.Get(port).SetHttps(router.Handler(), certs.certs)
|
log.Debug("not has port")
|
||||||
|
return nil
|
||||||
} else {
|
|
||||||
m.tf.Get(port).SetHttp(router.Handler())
|
|
||||||
}
|
}
|
||||||
|
service.Set(router.Handler)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,11 @@ type IRouter interface {
|
|||||||
SetRouter(id string, config *Config) error
|
SetRouter(id string, config *Config) error
|
||||||
Count() int
|
Count() int
|
||||||
Del(id string) int
|
Del(id string) int
|
||||||
Handler() fasthttp.RequestHandler
|
Handler(ctx *fasthttp.RequestCtx)
|
||||||
|
}
|
||||||
|
|
||||||
|
type IRouterFilter interface {
|
||||||
|
DoFilter(ctx *http_context.Context) (isContinue bool, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
//Router 实现了路由树接口
|
//Router 实现了路由树接口
|
||||||
@@ -28,6 +32,7 @@ type Router struct {
|
|||||||
data eosc.IUntyped
|
data eosc.IUntyped
|
||||||
match IMatcher
|
match IMatcher
|
||||||
handler fasthttp.RequestHandler
|
handler fasthttp.RequestHandler
|
||||||
|
chain []IRouterFilter
|
||||||
}
|
}
|
||||||
|
|
||||||
//NewRouter 新建路由树
|
//NewRouter 新建路由树
|
||||||
@@ -45,22 +50,25 @@ func (r *Router) Count() int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Handler 路由树的handler方法
|
//Handler 路由树的handler方法
|
||||||
func (r *Router) Handler() fasthttp.RequestHandler {
|
func (r *Router) Handler(requestCtx *fasthttp.RequestCtx) {
|
||||||
return func(requestCtx *fasthttp.RequestCtx) {
|
match := r.match
|
||||||
match := r.match
|
if r.match == nil {
|
||||||
if match == nil {
|
requestCtx.NotFound()
|
||||||
requestCtx.NotFound()
|
return
|
||||||
return
|
|
||||||
}
|
|
||||||
log.Debug("router handler", requestCtx.Request.String())
|
|
||||||
ctx := http_context.NewContext(requestCtx)
|
|
||||||
h, e, has := match.Match(ctx.Request())
|
|
||||||
if !has {
|
|
||||||
requestCtx.NotFound()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
h.Handle(ctx, NewEndPoint(e))
|
|
||||||
}
|
}
|
||||||
|
log.Debug("router handler", requestCtx.Request.String())
|
||||||
|
ctx := http_context.NewContext(requestCtx)
|
||||||
|
// TODO: 执行全局的Filter
|
||||||
|
h, e, has := match.Match(ctx.Request())
|
||||||
|
if !has {
|
||||||
|
requestCtx.NotFound()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
h.Handle(ctx, NewEndPoint(e))
|
||||||
|
for _, c := range r.chain {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//SetRouter 将路由配置加入到路由树中
|
//SetRouter 将路由配置加入到路由树中
|
||||||
|
|||||||
@@ -34,7 +34,6 @@ func (rs *Routers) Set(port int, id string, conf *Config) (IRouter, bool, error)
|
|||||||
rs.data.Set(name, router)
|
rs.data.Set(name, router)
|
||||||
return router, true, nil
|
return router, true, nil
|
||||||
}
|
}
|
||||||
// todo 这里需要校验端口已使用的的http协议是否与之前配置冲突,并返回新的合并后的证书列表
|
|
||||||
|
|
||||||
router := r.(IRouter)
|
router := r.(IRouter)
|
||||||
err := router.SetRouter(id, conf)
|
err := router.SetRouter(id, conf)
|
||||||
@@ -51,17 +50,6 @@ func NewRouters() *Routers {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//func (rs *Routers) GetEmployee(port int) (IRouter, bool) {
|
|
||||||
// name := strconv.Itoa(port)
|
|
||||||
// r, has := rs.data.GetEmployee(name)
|
|
||||||
// if !has {
|
|
||||||
// var router IRouter = NewRouter()
|
|
||||||
// rs.data.SetStatus(name, router)
|
|
||||||
// return router, true
|
|
||||||
// }
|
|
||||||
// return r.(IRouter), false
|
|
||||||
//}
|
|
||||||
|
|
||||||
//Del 将路由配置从对应端口的路由树中删去
|
//Del 将路由配置从对应端口的路由树中删去
|
||||||
func (rs *Routers) Del(port int, id string) (IRouter, bool) {
|
func (rs *Routers) Del(port int, id string) (IRouter, bool) {
|
||||||
name := strconv.Itoa(port)
|
name := strconv.Itoa(port)
|
||||||
|
|||||||
Reference in New Issue
Block a user