forked from golang/hotime
This commit is contained in:
parent
7da74469a5
commit
4057279471
@ -5,6 +5,7 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"errors"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@ -15,6 +16,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Application struct {
|
type Application struct {
|
||||||
|
MethodRouter
|
||||||
Router
|
Router
|
||||||
contextBase
|
contextBase
|
||||||
Port string //端口号
|
Port string //端口号
|
||||||
@ -41,6 +43,24 @@ func (this *Application) Run(router Router) {
|
|||||||
|
|
||||||
|
|
||||||
this.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 = port
|
||||||
this.Port = this.Config.GetString("port")
|
this.Port = this.Config.GetString("port")
|
||||||
|
|
||||||
@ -55,8 +75,8 @@ func (this *Application) Run(router Router) {
|
|||||||
//异常处理
|
//异常处理
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
//e.SetError(errors.New(ObjToStr(err)), LOG_FMT)
|
this.SetError(errors.New(fmt.Sprint(err)), LOG_FMT)
|
||||||
fmt.Println(err)
|
//fmt.Println(err)
|
||||||
this.Run(router)
|
this.Run(router)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@ -261,16 +281,22 @@ func (this *Application) handler(w http.ResponseWriter, req *http.Request) {
|
|||||||
o, s = this.urlSer(context.HandlerStr)
|
o, s = this.urlSer(context.HandlerStr)
|
||||||
context.RouterString = s
|
context.RouterString = s
|
||||||
//接口服务
|
//接口服务
|
||||||
if len(s) == 3 {
|
//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 {
|
// 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)
|
// this.Router[s[0]][s[1]][s[2]](&context)
|
||||||
//header.Set("Content-Type", "text/html; charset=utf-8")
|
// //header.Set("Content-Type", "text/html; charset=utf-8")
|
||||||
context.View()
|
// context.View()
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
|
//}
|
||||||
|
|
||||||
|
if this.MethodRouter[context.HandlerStr]!=nil{
|
||||||
|
this.MethodRouter[context.HandlerStr](&context)
|
||||||
|
context.View()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//url赋值
|
//url赋值
|
||||||
|
4
type.go
4
type.go
@ -1,9 +1,11 @@
|
|||||||
package hotime
|
package hotime
|
||||||
|
|
||||||
//控制器
|
//控制器
|
||||||
type Ctr map[string]func(this *Context)
|
type Ctr map[string]Method
|
||||||
type Proj map[string]Ctr
|
type Proj map[string]Ctr
|
||||||
type Router map[string]Proj
|
type Router map[string]Proj
|
||||||
|
type MethodRouter map[string]Method//直接字符串关联函数
|
||||||
|
type Method func(this *Context)
|
||||||
|
|
||||||
type CacheIns interface {
|
type CacheIns interface {
|
||||||
//set(key string, value interface{}, time int64)
|
//set(key string, value interface{}, time int64)
|
||||||
|
Loading…
Reference in New Issue
Block a user