11package main
22
33import (
4- "context"
5- "errors"
64 "fmt"
75 "github.com/devfeel/dotweb"
86 "github.com/devfeel/dotweb/framework/file"
97 "github.com/devfeel/dotweb/session"
108 "strconv"
11- "time"
129)
1310
1411func main () {
@@ -21,9 +18,13 @@ func main() {
2118 //设置gzip开关
2219 //app.HttpServer.SetEnabledGzip(true)
2320
21+ app .SetDevelopmentMode ()
22+
2423 //设置Session开关
2524 app .HttpServer .SetEnabledSession (true )
2625
26+ app .HttpServer .SetEnabledIgnoreFavicon (true )
27+
2728 //设置Session配置
2829 //runtime mode
2930 app .HttpServer .SetSessionConfig (session .NewDefaultRuntimeConfig ())
@@ -34,7 +35,7 @@ func main() {
3435 InitRoute (app .HttpServer )
3536
3637 //设置HttpModule
37- // InitModule(app)
38+ InitModule (app . HttpServer )
3839
3940 //启动 监控服务
4041 //app.SetPProfConfig(true, 8081)
@@ -52,71 +53,55 @@ func main() {
5253
5354func Index (ctx dotweb.Context ) error {
5455 ctx .Items ().Set ("count" , 2 )
55- ctx .WriteString ("index => " + ctx .Items ().GetString ("count" ))
56+ ctx .WriteString (ctx . Request (). Path () + ":Items.Count => " + ctx .Items ().GetString ("count" ))
5657 _ , err := ctx .WriteString ("\r \n " )
5758 return err
5859}
5960
60- func CtxTimeOut (ctx dotweb.Context ) error {
61- ctx .SetTimeoutContext (time .Second * 3 )
62- runCtx := context .WithValue (ctx .Context (), "test" , 1 )
63- err := sleepCtx (runCtx )
64- ctx .WriteString (time .Now (), err )
61+ func WHtml (ctx dotweb.Context ) error {
62+ ctx .WriteHtml ("this is html response!" )
6563 return nil
6664}
6765
68- func sleepCtx (runCtx context.Context ) error {
69- fmt .Println (runCtx .Value ("RequestID" ))
70- fmt .Println (runCtx .Value ("test" ))
71- c := make (chan error , 1 )
72- go func () {
73- time .Sleep (time .Second * 5 )
74- fmt .Println (time .Now (), "sleep time end" )
75- c <- errors .New ("test" )
76- }()
77- select {
78- case <- runCtx .Done ():
79- return runCtx .Err ()
80- case err := <- c :
81- return err
82- }
83- }
84-
85- func sleep (runCtx context.Context ) error {
86- fmt .Println (runCtx .Value ("RequestID" ))
87- time .Sleep (time .Second * 5 )
88- fmt .Println (time .Now (), "sleep time end" )
89- return errors .New ("test" )
90- }
91-
92-
93-
9466func InitRoute (server * dotweb.HttpServer ) {
9567 server .GET ("/" , Index )
96- server .GET ("/user" , Index ) //need login
97- server .GET ("/login" , Index )
98- server .GET ("/reg" , Index )
99- server .GET ("/ctx" , CtxTimeOut )
68+ server .GET ("/m" , Index )
69+ server .GET ("/h" , WHtml )
10070}
10171
102- func InitModule (dotserver * dotweb.DotWeb ) {
72+ func InitModule (dotserver * dotweb.HttpServer ) {
10373 dotserver .RegisterModule (& dotweb.HttpModule {
74+ Name : "test change route" ,
10475 OnBeginRequest : func (ctx dotweb.Context ) {
105- if ctx .HttpServer ().Router ().MatchPath (ctx , "/user" ) {
106- //TODO:need login
76+ if ctx .IsEnd () {
77+ return
78+ }
79+ if ctx .Request ().Path () == "/" && ctx .QueryString ("change" ) == "1" {
80+ //change route
81+ ctx .WriteString ("变更访问路由测试" )
82+ ctx .WriteString ("\r \n " )
83+ ctx .Request ().URL .Path = "/m"
84+ }
85+
86+ if ctx .Request ().Path () == "/" {
87+ ctx .Items ().Set ("count" , 1 )
88+ ctx .WriteString ("OnBeginRequest:Items.Count => " , ctx .Items ().GetString ("count" ))
89+ ctx .WriteString ("\r \n " )
10790 }
108- ctx .Items ().Set ("count" , 1 )
109- ctx .WriteString ("OnBeginRequest => " , ctx .Items ().GetString ("count" ))
110- ctx .WriteString ("\r \n " )
11191 if ctx .QueryString ("skip" ) == "1" {
11292 ctx .End ()
11393 }
11494 },
11595 OnEndRequest : func (ctx dotweb.Context ) {
116- if ctx .Items ().Exists ("count" ) {
117- ctx .WriteString ("OnEndRequest => " , ctx .Items ().GetString ("count" ))
118- } else {
119- ctx .WriteString ("OnEndRequest => " , ctx .Items ().Len ())
96+ if ctx .IsEnd () {
97+ return
98+ }
99+ if ctx .Request ().Path () == "/" {
100+ if ctx .Items ().Exists ("count" ) {
101+ ctx .WriteString ("OnEndRequest:Items.Count => " , ctx .Items ().GetString ("count" ))
102+ } else {
103+ ctx .WriteString ("OnEndRequest:Items.Len => " , ctx .Items ().Len ())
104+ }
120105 }
121106 },
122107 })
0 commit comments