This commit is contained in:
hoteas 2018-11-14 02:31:08 +00:00
parent 7da74469a5
commit 4057279471
2 changed files with 40 additions and 12 deletions

View File

@ -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,18 +281,24 @@ 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")
//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赋值
path := this.Config.GetString("tpt") + o

View File

@ -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)