forked from golang/hotime
107 lines
3.0 KiB
Go
107 lines
3.0 KiB
Go
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
_ "github.com/go-sql-driver/mysql"
|
|
"go.hoteas.com/hotime"
|
|
//"go.hoteas.com/hotime/cache"
|
|
"golang.org/x/net/websocket"
|
|
"time"
|
|
//"go.hoteas.com/hotime/cache"
|
|
)
|
|
|
|
func main() {
|
|
|
|
|
|
appIns := hotime.Application{}
|
|
|
|
|
|
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:=hotime.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)
|
|
|
|
|
|
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.Db.CacheIns=hotime.CacheIns(&hotime.CacheMemory{})
|
|
|
|
appIns.SetSession(hotime.CacheIns(&hotime.CacheMemory{}), hotime.CacheIns(&hotime.CacheDb{Db: &appIns.Db, Time: appIns.Config.GetInt64("cacheTime")}))
|
|
appIns.SetCache(hotime.CacheIns(&hotime.CacheMemory{}))
|
|
|
|
//快捷模式
|
|
//appIns.SetDefault(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.Run(hotime.Router{
|
|
"app": hotime.Proj{
|
|
"index": hotime.Ctr{
|
|
"test": func(this *hotime.Context) {
|
|
fmt.Println(this.Db.GetTag())
|
|
//x:=this.Db.Action(func(db hotime.HoTimeDB) bool {
|
|
//
|
|
// db.Insert("user",hotime.Map{"unickname":"dasdas"})
|
|
//
|
|
// return true
|
|
//})
|
|
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)
|
|
},
|
|
},
|
|
},
|
|
})
|
|
}
|