mirror of
				https://github.com/eolinker/apinto
				synced 2025-11-01 04:22:40 +08:00 
			
		
		
		
	临时提交
This commit is contained in:
		| @@ -1,9 +1,12 @@ | ||||
| package router_http | ||||
|  | ||||
| import ( | ||||
| 	"crypto/tls" | ||||
| 	"errors" | ||||
| 	"sync" | ||||
|  | ||||
| 	"github.com/eolinker/eosc/config" | ||||
|  | ||||
| 	"github.com/eolinker/eosc/log" | ||||
|  | ||||
| 	traffic_http_fast "github.com/eolinker/eosc/traffic/traffic-http-fast" | ||||
| @@ -28,12 +31,14 @@ var manager iManager | ||||
|  | ||||
| func init() { | ||||
| 	var tf traffic.ITraffic | ||||
| 	var cfg *config.ListensMsg | ||||
| 	bean.Autowired(&tf) | ||||
| 	bean.Autowired(&cfg) | ||||
|  | ||||
| 	bean.AddInitializingBeanFunc(func() { | ||||
| 		log.Debug("init router manager") | ||||
|  | ||||
| 		manager = NewManager(tf) | ||||
| 		manager = NewManager(tf, cfg) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| @@ -56,13 +61,32 @@ func (m *Manager) Cancel() { | ||||
| } | ||||
|  | ||||
| //NewManager 创建路由管理器 | ||||
| func NewManager(tf traffic.ITraffic) *Manager { | ||||
|  | ||||
| func NewManager(tf traffic.ITraffic, listenCfg *config.ListensMsg) *Manager { | ||||
| 	log.Debug("new router manager") | ||||
| 	m := &Manager{ | ||||
| 		routers: NewRouters(), | ||||
| 		tf:      traffic_http_fast.NewHttpTraffic(tf), | ||||
| 		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 | ||||
| } | ||||
|  | ||||
| @@ -70,18 +94,27 @@ func NewManager(tf traffic.ITraffic) *Manager { | ||||
| func (m *Manager) Add(port int, id string, config *Config) error { | ||||
| 	m.locker.Lock() | ||||
| 	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) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	if config.Protocol == "https" { | ||||
| 		certs := newCerts(config.Cert) | ||||
| 		m.tf.Get(port).SetHttps(router.Handler(), certs.certs) | ||||
|  | ||||
| 	} else { | ||||
| 		m.tf.Get(port).SetHttp(router.Handler()) | ||||
| 	service, has := m.tf.Get(port) | ||||
| 	if !has { | ||||
| 		log.Debug("not has port") | ||||
| 		return nil | ||||
| 	} | ||||
| 	service.Set(router.Handler) | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|   | ||||
| @@ -19,7 +19,11 @@ type IRouter interface { | ||||
| 	SetRouter(id string, config *Config) error | ||||
| 	Count() 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 实现了路由树接口 | ||||
| @@ -28,6 +32,7 @@ type Router struct { | ||||
| 	data    eosc.IUntyped | ||||
| 	match   IMatcher | ||||
| 	handler fasthttp.RequestHandler | ||||
| 	chain   []IRouterFilter | ||||
| } | ||||
|  | ||||
| //NewRouter 新建路由树 | ||||
| @@ -45,22 +50,25 @@ func (r *Router) Count() int { | ||||
| } | ||||
|  | ||||
| //Handler 路由树的handler方法 | ||||
| func (r *Router) Handler() fasthttp.RequestHandler { | ||||
| 	return func(requestCtx *fasthttp.RequestCtx) { | ||||
| 		match := r.match | ||||
| 		if match == nil { | ||||
| 			requestCtx.NotFound() | ||||
| 			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)) | ||||
| func (r *Router) Handler(requestCtx *fasthttp.RequestCtx) { | ||||
| 	match := r.match | ||||
| 	if r.match == nil { | ||||
| 		requestCtx.NotFound() | ||||
| 		return | ||||
| 	} | ||||
| 	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 将路由配置加入到路由树中 | ||||
|   | ||||
| @@ -34,7 +34,6 @@ func (rs *Routers) Set(port int, id string, conf *Config) (IRouter, bool, error) | ||||
| 		rs.data.Set(name, router) | ||||
| 		return router, true, nil | ||||
| 	} | ||||
| 	// todo 这里需要校验端口已使用的的http协议是否与之前配置冲突,并返回新的合并后的证书列表 | ||||
|  | ||||
| 	router := r.(IRouter) | ||||
| 	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 将路由配置从对应端口的路由树中删去 | ||||
| func (rs *Routers) Del(port int, id string) (IRouter, bool) { | ||||
| 	name := strconv.Itoa(port) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Liujian
					Liujian