hotime/example/main.go

94 lines
2.8 KiB
Go
Raw Normal View History

2017-08-17 02:37:00 +00:00
package main
import (
2021-05-21 20:52:03 +00:00
"../../hotime"
2021-05-23 23:27:41 +00:00
"../../hotime/cache"
2017-09-05 03:09:13 +00:00
"fmt"
"golang.org/x/net/websocket"
2018-04-08 16:02:13 +00:00
"time"
2017-08-17 02:37:00 +00:00
)
func main() {
2018-04-04 18:44:00 +00:00
2021-05-25 11:53:34 +00:00
//appIns := hotime.Application{}
appIns := hotime.Init("example/config/config.json")
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
//手动模式,
2021-05-25 11:53:34 +00:00
//appIns.SetConfig("example/config/config.json")
2018-04-09 17:16:24 +00:00
//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)
2018-04-09 17:16:24 +00:00
//fmt.Println(ca.Cache("xyzm").Data)
//mysql
//mysql.SetDB(&appIns)
//自动选择数据库
2021-05-23 23:27:41 +00:00
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{}))
2017-08-17 02:37:00 +00:00
2017-09-05 03:09:13 +00:00
//快捷模式
2021-05-25 11:53:34 +00:00
//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
2017-09-05 03:09:13 +00:00
appIns.Run(hotime.Router{
"app": hotime.Proj{
"index": hotime.Ctr{
"test": func(this *hotime.Context) {
2021-05-24 21:28:56 +00:00
//fmt.Println(this.Db.GetTag())
//this.Application.Log.Error("dasdasdas")
//this.Log.Error("dadasdasd")
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
//})
2021-05-23 23:27:41 +00:00
//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
}