hotime/example/main.go

107 lines
3.2 KiB
Go
Raw Normal View History

2017-08-17 02:37:00 +00:00
package main
import (
2019-11-10 08:42:49 +00:00
"code.hoteas.com/hoteas/hotime"
"code.hoteas.com/hoteas/hotime/tools/db"
2017-09-05 03:09:13 +00:00
"fmt"
2018-04-08 16:02:13 +00:00
//"go.hoteas.com/hotime/cache"
2017-09-05 03:09:13 +00:00
"golang.org/x/net/websocket"
2018-04-08 16:02:13 +00:00
"time"
2018-04-09 17:17:06 +00:00
//"go.hoteas.com/hotime/cache"
2017-08-17 02:37:00 +00:00
)
func main() {
2018-04-04 18:44:00 +00:00
2017-09-05 03:09:13 +00:00
appIns := hotime.Application{}
2018-04-04 18:44:00 +00:00
2017-09-05 03:09:13 +00:00
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, ":")))
2019-02-12 09:37:59 +00:00
2017-09-05 03:09:13 +00:00
//this.HandlerStr = "/test.html"
return true
})
2017-08-17 02:37:00 +00:00
2017-09-05 03:09:13 +00:00
//手动模式,
2018-04-03 17:54:27 +00:00
appIns.SetConfig("example/config/config.json")
2018-04-09 17:16:24 +00:00
//redis缓存接入
2018-04-08 16:02:13 +00:00
//ca:=hotime.CacheIns(&cache.CacheRedis{Host:appIns.Config.GetString("redisHost"),Pwd:appIns.Config.GetString("redisPwd"),Time:appIns.Config.GetCeilInt64("cacheLongTime")})
2018-04-09 17:16:24 +00:00
//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)
//自动选择数据库
db.SetDB(&appIns)
2018-04-04 18:44:00 +00:00
2019-11-10 08:42:49 +00:00
//appIns.SetConnectDB(func(err ...*hotime.Error) *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
//})
2018-04-09 20:10:58 +00:00
//内存缓存数据库数据,错误则删除
2019-11-10 08:42:49 +00:00
// appIns.Db.CacheIns=hotime.CacheIns(&hotime.CacheMemory{})
2018-04-09 20:10:58 +00:00
2017-09-05 03:09:13 +00:00
appIns.SetSession(hotime.CacheIns(&hotime.CacheMemory{}), hotime.CacheIns(&hotime.CacheDb{Db: &appIns.Db, Time: appIns.Config.GetInt64("cacheTime")}))
appIns.SetCache(hotime.CacheIns(&hotime.CacheMemory{}))
2017-08-17 02:37:00 +00:00
2017-09-05 03:09:13 +00:00
//快捷模式
//appIns.SetDefault(func(err ...*hotime.Error) *sql.DB {
2017-08-17 02:37:00 +00:00
// 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
//})
2017-09-05 03:09:13 +00:00
appIns.Run(hotime.Router{
"app": hotime.Proj{
"index": hotime.Ctr{
"test": func(this *hotime.Context) {
fmt.Println(this.Db.GetTag())
2017-10-27 04:28:47 +00:00
//x:=this.Db.Action(func(db hotime.HoTimeDB) bool {
//
// db.Insert("user",hotime.Map{"unickname":"dasdas"})
//
// return true
//})
hotime.LogError("dasdasdasdasdas")
2017-10-27 04:28:47 +00:00
this.Display(5, "dsadas")
2017-09-05 03:09:13 +00:00
},
"websocket": func(this *hotime.Context) {
hdler := websocket.Handler(func(ws *websocket.Conn) {
for true {
msg := make([]byte, 5120)
n, err := ws.Read(msg)
2018-04-08 16:02:13 +00:00
go func() {
2019-11-10 08:42:49 +00:00
time.Sleep(time.Second * 5)
2018-04-08 16:02:13 +00:00
ws.Write([]byte("dsadasdasgregergrerge"))
2017-09-05 03:09:13 +00:00
2018-04-08 16:02:13 +00:00
}()
2017-09-05 03:09:13 +00:00
if err != nil {
return
}
fmt.Printf("Receive: %s\n", msg[:n])
send_msg := "[" + string(msg[:n]) + "]"
m, err := ws.Write([]byte(send_msg))
if err != nil {
return
}
fmt.Printf("Send: %s\n", msg[:m])
}
})
hdler.ServeHTTP(this.Resp, this.Req)
},
},
},
})
2017-08-17 02:37:00 +00:00
}