From dde3586db4beb79e684888d78ce11b82ec66be34 Mon Sep 17 00:00:00 2001 From: hoteas Date: Thu, 17 Aug 2017 02:37:00 +0000 Subject: [PATCH] demo --- example/config/config.json | 18 +++++++++ example/main.go | 76 ++++++++++++++++++++++++++++++++++++++ example/tpt/index.html | 10 +++++ 3 files changed, 104 insertions(+) create mode 100644 example/config/config.json create mode 100644 example/main.go create mode 100644 example/tpt/index.html diff --git a/example/config/config.json b/example/config/config.json new file mode 100644 index 0000000..3d4ee02 --- /dev/null +++ b/example/config/config.json @@ -0,0 +1,18 @@ +{ + "cacheLongTime": 2592000, + "cacheShortTime": 7200, + "dbHost": "127.0.0.1", + "dbName": "test", + "dbPort": "3306", + "dbPwd": "root", + "dbUser": "root", + "defFile": [ + "index.html", + "index.htm" + ], + "error": {}, + "logLevel": 0, + "port": "80", + "sessionName": "HOTIME", + "tpt": "tpt" +} \ No newline at end of file diff --git a/example/main.go b/example/main.go new file mode 100644 index 0000000..c9dcf1f --- /dev/null +++ b/example/main.go @@ -0,0 +1,76 @@ +package main + +import ( + "database/sql" + "fmt" + "go.hoteas.com/hotime" + "golang.org/x/net/websocket" +) + +func main() { + + appIns := hotime.Application{} + i := 0 + + appIns.SetConnectListener(func(context hotime.Context) bool { + fmt.Println(i) + i++ + return true + }) + + //手动模式, + appIns.SetConfig() + 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{})) + + //快捷模式 + //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) { + this.Display(0, "chenggong") + }, + "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) + }, + }, + }, + }) +} diff --git a/example/tpt/index.html b/example/tpt/index.html new file mode 100644 index 0000000..9325721 --- /dev/null +++ b/example/tpt/index.html @@ -0,0 +1,10 @@ + + + + + Title + + + + + \ No newline at end of file