|
| 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