From c46fdd898594115288b53fcbdacbe6324cff8cdd Mon Sep 17 00:00:00 2001 From: hoteas Date: Sat, 29 May 2021 02:57:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=85=A5RESTfull=E9=A3=8E=E6=A0=BC?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application.go | 16 +--------- example/admin/init.go | 9 ++++++ example/admin/user.go | 23 ++++++++++++++ example/main.go | 74 ++++++++++++++++++++++++------------------- var.go | 2 +- 5 files changed, 75 insertions(+), 49 deletions(-) create mode 100644 example/admin/init.go create mode 100644 example/admin/user.go diff --git a/application.go b/application.go index 8b6f424..ed7db0a 100644 --- a/application.go +++ b/application.go @@ -289,14 +289,11 @@ func (that *Application) handler(w http.ResponseWriter, req *http.Request) { cookie, err := req.Cookie(that.Config.GetString("sessionName")) sessionId := Md5(strconv.Itoa(Rand(10))) token := req.FormValue("token") - //isFirst:=false + if err != nil || (len(token) == 32 && cookie.Value != token) { if len(token) == 32 { sessionId = token } - //else{ - // isFirst=true; - //} http.SetCookie(w, &http.Cookie{Name: that.Config.GetString("sessionName"), Value: sessionId, Path: "/"}) } else { sessionId = cookie.Value @@ -339,17 +336,6 @@ func (that *Application) handler(w http.ResponseWriter, req *http.Request) { } //接口服务 - //if len(s) == 3 { - // //如果满足规则则路由到对应控制器去 - // if that.Router[s[0]] != nil && that.Router[s[0]][s[1]] != nil && that.Router[s[0]][s[1]][s[2]] != nil { - // //控制层 - // that.Router[s[0]][s[1]][s[2]](&context) - // //header.Set("Content-Type", "text/html; charset=utf-8") - // context.View() - // return - // } - // - //} //验证接口严格模式 modeRouterStrict := that.Config.GetBool("modeRouterStrict") tempHandlerStr := context.HandlerStr diff --git a/example/admin/init.go b/example/admin/init.go new file mode 100644 index 0000000..084f46e --- /dev/null +++ b/example/admin/init.go @@ -0,0 +1,9 @@ +package admin + +import ( + . "../../../hotime" +) + +var AdminProj = Proj{ + "user": UserCtr, +} diff --git a/example/admin/user.go b/example/admin/user.go new file mode 100644 index 0000000..eab8397 --- /dev/null +++ b/example/admin/user.go @@ -0,0 +1,23 @@ +package admin + +import ( + . "../../../hotime" +) + +var UserCtr = Ctr{ + "info": func(this *Context) { + + }, + "add": func(this *Context) { + + }, + "update": func(this *Context) { + + }, + "remove": func(this *Context) { + + }, + "search": func(this *Context) { + + }, +} diff --git a/example/main.go b/example/main.go index 355a971..5a5e171 100644 --- a/example/main.go +++ b/example/main.go @@ -2,6 +2,7 @@ package main import ( "../../hotime" + "./admin" "errors" "fmt" "golang.org/x/net/websocket" @@ -10,44 +11,51 @@ import ( func main() { - //appIns := hotime.Application{} appIns := hotime.Init("example/config/config.json") + //RESTfull接口适配 appIns.SetConnectListener(func(context *hotime.Context) bool { - //fmt.Println(context.HandlerStr + time.Now().Format(" 2006-01-02 15:04 ") + hotime.Substr(context.Req.RemoteAddr, 0, strings.Index(context.Req.RemoteAddr, ":"))) - - //this.HandlerStr = "/test.html" - return true + if len(context.RouterString) < 2 || len(context.RouterString) > 3 || + !(context.Router[context.RouterString[0]] != nil && + context.Router[context.RouterString[0]][context.RouterString[1]] != nil) { + return true + } + //排除无效操作 + if len(context.RouterString) == 2 && + context.Req.Method != "GET" && + context.Req.Method != "POST" { + return true + } + //列表检索 + if len(context.RouterString) == 2 && + context.Req.Method == "GET" { + context.Router[context.RouterString[0]][context.RouterString[1]]["search"](context) + } + //新建 + if len(context.RouterString) == 2 && + context.Req.Method == "POST" { + context.Router[context.RouterString[0]][context.RouterString[1]]["add"](context) + } + //查询单条 + if len(context.RouterString) == 3 && + context.Req.Method == "GET" { + context.Router[context.RouterString[0]][context.RouterString[1]]["info"](context) + } + //更新 + if len(context.RouterString) == 3 && + context.Req.Method == "PUT" { + context.Router[context.RouterString[0]][context.RouterString[1]]["update"](context) + } + //移除 + if len(context.RouterString) == 3 && + context.Req.Method == "DELETE" { + context.Router[context.RouterString[0]][context.RouterString[1]]["remove"](context) + } + context.View() + return false }) - //手动模式, - //appIns.SetConfig("example/config/config.json") - //redis缓存接入 - //ca := cache.CacheIns(&cache.CacheRedis{Host: appIns.Config.GetString("redisHost"), Pwd: appIns.Config.GetString("redisPwd"), Time: appIns.Config.GetCeilInt64("cacheLongTime")}) - //ca.Cache("xyzm", "dasdas") - //ca.Cache("xyzn", "dasdas") - //ca.Cache("xyzo", "dasdas") - //ca.Cache("xyz*", nil) - //fmt.Println(ca.Cache("xyzm").Data) - //mysql - //mysql.SetDB(&appIns) - //自动选择数据库 - //dbInterface := cache.HoTimeDBInterface(&appIns.Db) - //appIns.SetSession(cache.CacheIns(&cache.CacheMemory{}), cache.CacheIns(&cache.CacheDb{Db: dbInterface, Time: appIns.Config.GetInt64("cacheTime")})) - //appIns.SetCache(cache.CacheIns(&cache.CacheMemory{})) - - //快捷模式 - //appIns.SetDefault(func(err ...*common.Error) (*sql.DB, *sql.DB) { - // query := appIns.Config.GetString("dbUser") + ":" + appIns.Config.GetString("dbPwd") + - // "@tcp(" + appIns.Config.GetString("dbHost") + ":" + appIns.Config.GetString("dbPort") + ")/" + appIns.Config.GetString("dbName") + "?charset=utf8" - // DB, e := sql.Open("mysql", query) - // if e != nil && len(err) != 0 { - // err[0].SetError(e) - // } - // return DB, nil - //}) - //init - appIns.Run(hotime.Router{ + "admin": admin.AdminProj, "app": hotime.Proj{ "index": hotime.Ctr{ "test": func(this *hotime.Context) { diff --git a/var.go b/var.go index e367d78..37d428c 100644 --- a/var.go +++ b/var.go @@ -100,7 +100,7 @@ var ConfigNote = Map{ "modeRouterStrict": "默认false,必须,路由严格模式false,为大小写忽略必须匹配,true必须大小写匹配", //路由严格模式/a/b/c "sessionName": "默认HOTIME,必须,设置session的cookie名", "port": "默认80,必须,web服务开启Http端口,0为不启用http服务,默认80", - "tlsPort": "默认443,非必须,web服务https端口,0为不启用https服务", + "tlsPort": "默认空,非必须,web服务https端口,0为不启用https服务", "tlsKey": "默认空,非必须,https密钥", "tlsCert": "默认空,非必须,https证书", }