Skip to content

Commit 49a7b64

Browse files
committed
* 新增example\session & example\bind
1 parent 53e776e commit 49a7b64

2 files changed

Lines changed: 125 additions & 0 deletions

File tree

‎example/bind/main.go‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/devfeel/dotweb"
6+
"github.com/devfeel/dotweb/framework/file"
7+
"strconv"
8+
)
9+
10+
func main() {
11+
//初始化DotServer
12+
app := dotweb.New()
13+
14+
//设置dotserver日志目录
15+
app.SetLogPath(file.GetCurrentDirectory())
16+
17+
//设置Debug开关
18+
app.SetEnabledDebug(true)
19+
20+
//设置gzip开关
21+
//app.SetEnabledGzip(true)
22+
23+
//设置路由
24+
InitRoute(app.HttpServer)
25+
26+
//启动 监控服务
27+
//pprofport := 8081
28+
//go app.StartPProfServer(pprofport)
29+
30+
// 开始服务
31+
port := 8080
32+
fmt.Println("dotweb.StartServer => " + strconv.Itoa(port))
33+
err := app.StartServer(port)
34+
fmt.Println("dotweb.StartServer error => ", err)
35+
}
36+
37+
func TestBind(ctx *dotweb.HttpContext) {
38+
type UserInfo struct {
39+
UserName string
40+
Sex int
41+
}
42+
user := new(UserInfo)
43+
errstr := "no error"
44+
if err := ctx.Bind(user); err != nil {
45+
errstr = err.Error()
46+
} else {
47+
48+
}
49+
50+
ctx.WriteString("TestBind [" + errstr + "] " + fmt.Sprint(user))
51+
}
52+
53+
func InitRoute(server *dotweb.HttpServer) {
54+
server.POST("/", TestBind)
55+
}

‎example/session/main.go‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/devfeel/dotweb"
6+
"github.com/devfeel/dotweb/framework/file"
7+
"github.com/devfeel/dotweb/session"
8+
"strconv"
9+
)
10+
11+
func main() {
12+
//初始化DotServer
13+
app := dotweb.New()
14+
15+
//设置dotserver日志目录
16+
app.SetLogPath(file.GetCurrentDirectory())
17+
18+
//设置Debug开关
19+
app.SetEnabledDebug(true)
20+
21+
//设置gzip开关
22+
//app.SetEnabledGzip(true)
23+
24+
//设置Session开关
25+
app.SetEnabledSession(true)
26+
27+
//设置Session配置
28+
//runtime mode
29+
app.SetSessionConfig(session.NewDefaultRuntimeConfig())
30+
//redis mode
31+
//app.SetSessionConfig(session.NewDefaultRedisConfig("192.168.8.175:6379", ""))
32+
33+
//设置路由
34+
InitRoute(app.HttpServer)
35+
36+
//设置HttpModule
37+
//InitModule(app)
38+
39+
//启动 监控服务
40+
//pprofport := 8081
41+
//go app.StartPProfServer(pprofport)
42+
43+
//全局容器
44+
app.AppContext.Set("gstring", "gvalue")
45+
app.AppContext.Set("gint", 1)
46+
47+
// 开始服务
48+
port := 8080
49+
fmt.Println("dotweb.StartServer => " + strconv.Itoa(port))
50+
err := app.StartServer(port)
51+
fmt.Println("dotweb.StartServer error => ", err)
52+
}
53+
54+
func TestSession(ctx *dotweb.HttpContext) {
55+
type UserInfo struct {
56+
UserName string
57+
NickName string
58+
}
59+
user := UserInfo{UserName: "test", NickName: "testName"}
60+
ctx.Session().Set("username", user)
61+
userRead := ctx.Session().Get("username").(UserInfo)
62+
63+
ctx.WriteString("welcome to dotweb - sessionid=> " + ctx.SessionID +
64+
", session-len=>" + strconv.Itoa(ctx.Session().Count()) +
65+
",username=>" + fmt.Sprintln(userRead))
66+
}
67+
68+
func InitRoute(server *dotweb.HttpServer) {
69+
server.GET("/", TestSession)
70+
}

0 commit comments

Comments
 (0)