Skip to content

Commit

Permalink
remove unused code and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
it512 committed Mar 9, 2019
1 parent 47ddf3d commit e2343ac
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 55 deletions.
11 changes: 11 additions & 0 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,14 @@ func (r *NamedRoute) SetName(name string) {
func (r *NamedRoute) Type() string {
return "handler"
}

// 获取当前请求路径
func GetReqPath(r *http.Request) string {
path := r.URL.RawPath

if path == "" {
path = r.URL.Path
}

return path
}
26 changes: 11 additions & 15 deletions mux_muti.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,17 @@ func (f MatcherFunc) Match(req *http.Request) bool {
return f(req)
}

type MatchedMux struct {
type matchedMux struct {
Muxer
Matcher
}

func NewMatchedMux(m Muxer, f MatcherFunc) *MatchedMux {
return &MatchedMux{
Muxer: m,
Matcher: f,
}
}

type Muxes struct {
ms []*MatchedMux
type muxes struct {
ms []*matchedMux
def Muxer
}

func (m *Muxes) Lookup(method string, path string, req *http.Request) MuxerCtx {
func (m *muxes) Lookup(method string, path string, req *http.Request) MuxerCtx {
for _, mux := range m.ms {
if mux.Match(req) {
return mux.Lookup(method, path, req)
Expand All @@ -41,15 +34,18 @@ func (m *Muxes) Lookup(method string, path string, req *http.Request) MuxerCtx {
return m.def.Lookup(method, path, req)
}

func (m *Muxes) AddHandler(method string, path string, h HandlerFunc, ms ...MiddlewareFunc) Router {
func (m *muxes) AddHandler(method string, path string, h HandlerFunc, ms ...MiddlewareFunc) Router {
return m.def.AddHandler(method, path, h, ms...)
}

func (m *Muxes) Use(ms ...MiddlewareFunc) {
func (m *muxes) Use(ms ...MiddlewareFunc) {
m.def.Use(ms...)
}

func (m *Muxes) AddMuxer(mux Muxer, f MatcherFunc) {
mf := NewMatchedMux(mux, f)
func (m *muxes) AddMuxer(mux Muxer, match Matcher) {
mf := &matchedMux{
Muxer: m,
Matcher: match,
}
m.ms = append(m.ms, mf)
}
27 changes: 20 additions & 7 deletions twig.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"os"
)

// M 全局通用的map
type M map[string]interface{}

// Identifier 标识符接口
type Identifier interface {
ID() string
Expand All @@ -18,6 +21,13 @@ type Attacher interface {
Attach(*Twig)
}

// Attach 设置关联关系
func Attach(i interface{}, t *Twig) {
if attacher, ok := i.(Attacher); ok {
attacher.Attach(t)
}
}

// Cycler 设置周期管理
type Cycler interface {
Start() error
Expand All @@ -36,7 +46,7 @@ type Twig struct {

Logger Logger // Logger 组件负责日志输出
lead *Lead // Server 组
muxes *Muxes // 路由器
muxes *muxes // 路由器

Debug bool

Expand Down Expand Up @@ -79,7 +89,7 @@ func TODO() *Twig {
t: t,
}

t.muxes = &Muxes{
t.muxes = &muxes{
def: NewRadixTree(),
}

Expand All @@ -94,11 +104,16 @@ func (t *Twig) AddServer(s ...Server) {
t.lead.AddServer(s...)
}

func (t *Twig) AddMuxer(mux Muxer, match MatcherFunc) *Conf {
// AddMuxer 增加Muxer, match 决定这个muxer在何种情况使用
func (t *Twig) AddMuxer(mux Muxer, match Matcher) *Conf {
t.muxes.AddMuxer(mux, match)
return TwigConfig(mux, t)
}

func (t *Twig) AddMuxerMatcherFunc(mux Muxer, match MatcherFunc) *Conf {
return t.AddMuxer(mux, match)
}

func (t *Twig) EnableDebug() {
t.Debug = true
}
Expand Down Expand Up @@ -173,15 +188,13 @@ func (t *Twig) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Start Cycler#Start
func (t *Twig) Start() error {
t.Logger.Printf("Twig@%s(id = %s ver = %s)\n", t.Name(), t.ID(), Version)

for _, p := range t.plugins {
if cycler, ok := p.(Cycler); ok {
if err := cycler.Start(); err != nil {
t.Logger.Printf("Plugin (id = %s) start fatal, Err = %v\n", p.ID(), err)
}
}
}

return t.lead.Start()
}

Expand Down Expand Up @@ -222,7 +235,7 @@ func (t *Twig) SetType(typ string) {
t.typ = typ
}

// Config Configer#Config
// Config 默认配置
func (t *Twig) Config() *Conf {
return TwigConfig(t.muxes.def, t)
return TwigConfig(t.muxes, t)
}
36 changes: 3 additions & 33 deletions util.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package twig

import (
"net/http"
)

// 组装器
type Assembler interface {
Register
Expand All @@ -12,18 +8,13 @@ type Assembler interface {

type target struct {
Register
*Twig
}

// Use 消除冲突
func (t *target) Use(m ...MiddlewareFunc) {
t.Register.Use(m...)
PluginHelper
}

func newTarget(r Register, twig *Twig) Assembler {
return &target{
Register: r,
Twig: twig,
Register: r,
PluginHelper: twig,
}
}

Expand All @@ -38,27 +29,6 @@ func (m MountFunc) Mount(target Assembler) {
m(target)
}

// M 全局通用的map
type M map[string]interface{}

// 获取当前请求路径
func GetReqPath(r *http.Request) string {
path := r.URL.RawPath

if path == "" {
path = r.URL.Path
}

return path
}

// Attach 设置关联关系
func Attach(i interface{}, t *Twig) {
if attacher, ok := i.(Attacher); ok {
attacher.Attach(t)
}
}

// Conf Twig路由配置工具
type Conf struct {
target Assembler
Expand Down

0 comments on commit e2343ac

Please sign in to comment.