接入RESTfull风格接口
This commit is contained in:
parent
9fd0a5fd61
commit
c46fdd8985
@ -289,14 +289,11 @@ func (that *Application) handler(w http.ResponseWriter, req *http.Request) {
|
|||||||
cookie, err := req.Cookie(that.Config.GetString("sessionName"))
|
cookie, err := req.Cookie(that.Config.GetString("sessionName"))
|
||||||
sessionId := Md5(strconv.Itoa(Rand(10)))
|
sessionId := Md5(strconv.Itoa(Rand(10)))
|
||||||
token := req.FormValue("token")
|
token := req.FormValue("token")
|
||||||
//isFirst:=false
|
|
||||||
if err != nil || (len(token) == 32 && cookie.Value != token) {
|
if err != nil || (len(token) == 32 && cookie.Value != token) {
|
||||||
if len(token) == 32 {
|
if len(token) == 32 {
|
||||||
sessionId = token
|
sessionId = token
|
||||||
}
|
}
|
||||||
//else{
|
|
||||||
// isFirst=true;
|
|
||||||
//}
|
|
||||||
http.SetCookie(w, &http.Cookie{Name: that.Config.GetString("sessionName"), Value: sessionId, Path: "/"})
|
http.SetCookie(w, &http.Cookie{Name: that.Config.GetString("sessionName"), Value: sessionId, Path: "/"})
|
||||||
} else {
|
} else {
|
||||||
sessionId = cookie.Value
|
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")
|
modeRouterStrict := that.Config.GetBool("modeRouterStrict")
|
||||||
tempHandlerStr := context.HandlerStr
|
tempHandlerStr := context.HandlerStr
|
||||||
|
9
example/admin/init.go
Normal file
9
example/admin/init.go
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package admin
|
||||||
|
|
||||||
|
import (
|
||||||
|
. "../../../hotime"
|
||||||
|
)
|
||||||
|
|
||||||
|
var AdminProj = Proj{
|
||||||
|
"user": UserCtr,
|
||||||
|
}
|
23
example/admin/user.go
Normal file
23
example/admin/user.go
Normal file
@ -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) {
|
||||||
|
|
||||||
|
},
|
||||||
|
}
|
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"../../hotime"
|
"../../hotime"
|
||||||
|
"./admin"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"golang.org/x/net/websocket"
|
"golang.org/x/net/websocket"
|
||||||
@ -10,44 +11,51 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
//appIns := hotime.Application{}
|
|
||||||
appIns := hotime.Init("example/config/config.json")
|
appIns := hotime.Init("example/config/config.json")
|
||||||
|
//RESTfull接口适配
|
||||||
appIns.SetConnectListener(func(context *hotime.Context) bool {
|
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, ":")))
|
if len(context.RouterString) < 2 || len(context.RouterString) > 3 ||
|
||||||
|
!(context.Router[context.RouterString[0]] != nil &&
|
||||||
//this.HandlerStr = "/test.html"
|
context.Router[context.RouterString[0]][context.RouterString[1]] != nil) {
|
||||||
return true
|
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{
|
appIns.Run(hotime.Router{
|
||||||
|
"admin": admin.AdminProj,
|
||||||
"app": hotime.Proj{
|
"app": hotime.Proj{
|
||||||
"index": hotime.Ctr{
|
"index": hotime.Ctr{
|
||||||
"test": func(this *hotime.Context) {
|
"test": func(this *hotime.Context) {
|
||||||
|
2
var.go
2
var.go
@ -100,7 +100,7 @@ var ConfigNote = Map{
|
|||||||
"modeRouterStrict": "默认false,必须,路由严格模式false,为大小写忽略必须匹配,true必须大小写匹配", //路由严格模式/a/b/c
|
"modeRouterStrict": "默认false,必须,路由严格模式false,为大小写忽略必须匹配,true必须大小写匹配", //路由严格模式/a/b/c
|
||||||
"sessionName": "默认HOTIME,必须,设置session的cookie名",
|
"sessionName": "默认HOTIME,必须,设置session的cookie名",
|
||||||
"port": "默认80,必须,web服务开启Http端口,0为不启用http服务,默认80",
|
"port": "默认80,必须,web服务开启Http端口,0为不启用http服务,默认80",
|
||||||
"tlsPort": "默认443,非必须,web服务https端口,0为不启用https服务",
|
"tlsPort": "默认空,非必须,web服务https端口,0为不启用https服务",
|
||||||
"tlsKey": "默认空,非必须,https密钥",
|
"tlsKey": "默认空,非必须,https密钥",
|
||||||
"tlsCert": "默认空,非必须,https证书",
|
"tlsCert": "默认空,非必须,https证书",
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user