forked from devfeel/dotweb-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
82 lines (67 loc) · 1.83 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"fmt"
"github.com/devfeel/dotweb"
"github.com/devfeel/dotweb/framework/file"
"github.com/devfeel/dotweb/session"
"strconv"
)
func main() {
//初始化DotServer
app := dotweb.New()
//设置dotserver日志目录
app.SetLogPath(file.GetCurrentDirectory())
//设置Debug开关
app.SetEnabledDebug(true)
//设置gzip开关
//app.SetEnabledGzip(true)
//设置Session开关
app.SetEnabledSession(true)
//设置Session配置
//runtime mode
app.SetSessionConfig(session.NewDefaultRuntimeConfig())
//redis mode
//app.SetSessionConfig(session.NewDefaultRedisConfig("192.168.8.175:6379", ""))
//设置路由
InitRoute(app.HttpServer)
//设置HttpModule
InitModule(app)
//启动 监控服务
//pprofport := 8081
//go app.StartPProfServer(pprofport)
//全局容器
app.AppContext.Set("gstring", "gvalue")
app.AppContext.Set("gint", 1)
// 开始服务
port := 8080
fmt.Println("dotweb.StartServer => " + strconv.Itoa(port))
err := app.StartServer(port)
fmt.Println("dotweb.StartServer error => ", err)
}
func Index(ctx *dotweb.HttpContext) {
ctx.WriteString("index => " + ctx.Items().GetString("count"))
ctx.WriteString("\r\n")
ctx.Items().Set("count", 2)
}
func InitRoute(server *dotweb.HttpServer) {
server.Router().GET("/", Index)
}
func InitModule(dotserver *dotweb.DotWeb) {
dotserver.RegisterModule(&dotweb.HttpModule{
OnBeginRequest: func(ctx *dotweb.HttpContext) {
ctx.Items().Set("count", 1)
ctx.WriteString("OnBeginRequest => ", ctx.Items().GetString("count"))
ctx.WriteString("\r\n")
if ctx.QueryString("skip") == "1" {
ctx.End()
}
},
OnEndRequest: func(ctx *dotweb.HttpContext) {
if ctx.Items().Exists("count") {
ctx.WriteString("OnEndRequest => ", ctx.Items().GetString("count"))
} else {
ctx.WriteString("OnEndRequest => ", ctx.Items().Len())
}
},
})
}