hotime/example/main.go

104 lines
2.7 KiB
Go
Raw Normal View History

2017-08-17 02:37:00 +00:00
package main
import (
2017-09-05 03:09:13 +00:00
"database/sql"
"fmt"
2017-08-23 01:40:54 +00:00
_ "github.com/go-sql-driver/mysql"
2017-08-17 02:37:00 +00:00
"go.hoteas.com/hotime"
2018-04-04 18:44:00 +00:00
"go.hoteas.com/hotime/cache"
2017-09-05 03:09:13 +00:00
"golang.org/x/net/websocket"
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
i := 0
2017-08-17 02:37:00 +00:00
2017-09-05 03:09:13 +00:00
appIns.SetConnectListener(func(context *hotime.Context) bool {
fmt.Println(i)
i++
//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-04 18:44:00 +00:00
ca:=hotime.CacheIns(&cache.CacheRedis{Host:appIns.Config.GetString("redisHost"),Pwd:appIns.Config.GetString("redisPwd"),Time:appIns.Config.GetCeilInt64("cacheLongTime")})
ca.Cache("x",hotime.Map{"1":"2132"})
fmt.Println(ca.Cache("x").ToMap())
fmt.Println(ca.Cache("x",nil).Data)
fmt.Println(ca.Cache("x",nil).Data)
fmt.Println(ca.Cache("x").Data)
fmt.Println(ca.Cache("x").Data)
2017-09-05 03:09:13 +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
})
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
//})
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)
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
}