hotime/example/main.go
2021-05-29 00:15:13 +08:00

101 lines
3.0 KiB
Go

package main
import (
"../../hotime"
"errors"
"fmt"
"golang.org/x/net/websocket"
"time"
)
func main() {
//appIns := hotime.Application{}
appIns := hotime.Init("example/config/config.json")
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
})
//手动模式,
//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{
"app": hotime.Proj{
"index": hotime.Ctr{
"test": func(this *hotime.Context) {
data := this.Db.Get("cached", "*")
fmt.Println(data)
fmt.Println(this.Session("test").ToCeilInt())
this.Session("test1", 98984984)
fmt.Println(this.Session("test1").Data)
this.Error.SetError(errors.New("dasdasdas"))
//fmt.Println(this.Db.GetTag())
//this.Application.Log.Error("dasdasdas")
//this.Log.Error("dadasdasd")
//x:=this.Db.Action(func(db hotime.HoTimeDB) bool {
//
// db.Insert("user",hotime.Map{"unickname":"dasdas"})
//
// return true
//})
//hotime.LogError("dasdasdasdasdas")
this.Display(5, "dsadas")
},
"websocket": func(this *hotime.Context) {
hdler := websocket.Handler(func(ws *websocket.Conn) {
for true {
msg := make([]byte, 5120)
n, err := ws.Read(msg)
go func() {
time.Sleep(time.Second * 5)
ws.Write([]byte("dsadasdasgregergrerge"))
}()
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)
},
},
},
})
}