diff --git a/application.go b/application.go index 08dacb6..aea543d 100644 --- a/application.go +++ b/application.go @@ -5,6 +5,7 @@ import ( "database/sql" "encoding/json" "fmt" + "errors" "io/ioutil" "net/http" "os" @@ -15,6 +16,7 @@ import ( ) type Application struct { + MethodRouter Router contextBase Port string //端口号 @@ -41,6 +43,24 @@ func (this *Application) Run(router Router) { this.Router = router + //重新设置MethodRouter//直达路由 + this.MethodRouter=MethodRouter{} + + if router!=nil{ + for pk,pv:= range router{ + if pv!=nil{ + for ck,cv:=range pv{ + if cv!=nil{ + for mk,mv:=range cv{ + this.MethodRouter["/"+pk+"/"+ck+"/"+mk]=mv + } + } + } + } + } + } + + //this.Port = port this.Port = this.Config.GetString("port") @@ -55,8 +75,8 @@ func (this *Application) Run(router Router) { //异常处理 defer func() { if err := recover(); err != nil { - //e.SetError(errors.New(ObjToStr(err)), LOG_FMT) - fmt.Println(err) + this.SetError(errors.New(fmt.Sprint(err)), LOG_FMT) + //fmt.Println(err) this.Run(router) } }() @@ -261,16 +281,22 @@ func (this *Application) handler(w http.ResponseWriter, req *http.Request) { o, s = this.urlSer(context.HandlerStr) context.RouterString = s //接口服务 - if len(s) == 3 { - //如果满足规则则路由到对应控制器去 - if this.Router[s[0]] != nil && this.Router[s[0]][s[1]] != nil && this.Router[s[0]][s[1]][s[2]] != nil { - //控制层 - this.Router[s[0]][s[1]][s[2]](&context) - //header.Set("Content-Type", "text/html; charset=utf-8") - context.View() - return - } + //if len(s) == 3 { + // //如果满足规则则路由到对应控制器去 + // if this.Router[s[0]] != nil && this.Router[s[0]][s[1]] != nil && this.Router[s[0]][s[1]][s[2]] != nil { + // //控制层 + // this.Router[s[0]][s[1]][s[2]](&context) + // //header.Set("Content-Type", "text/html; charset=utf-8") + // context.View() + // return + // } + // + //} + if this.MethodRouter[context.HandlerStr]!=nil{ + this.MethodRouter[context.HandlerStr](&context) + context.View() + return } //url赋值 diff --git a/type.go b/type.go index 9fab6fe..25f9140 100644 --- a/type.go +++ b/type.go @@ -1,9 +1,11 @@ package hotime //控制器 -type Ctr map[string]func(this *Context) +type Ctr map[string]Method type Proj map[string]Ctr type Router map[string]Proj +type MethodRouter map[string]Method//直接字符串关联函数 +type Method func(this *Context) type CacheIns interface { //set(key string, value interface{}, time int64)