change attribute Context to context.Context for ghttp.Request

This commit is contained in:
John
2020-03-10 20:45:22 +08:00
parent 645cecdffb
commit f3bd2b67f7
2 changed files with 7 additions and 6 deletions

View File

@ -7,8 +7,8 @@
package ghttp
import (
"context"
"fmt"
"github.com/gogf/gf/container/gmap"
"github.com/gogf/gf/os/gres"
"github.com/gogf/gf/os/gview"
"net/http"
@ -32,7 +32,7 @@ type Request struct {
LeaveTime int64 // Request ending time in microseconds.
Middleware *Middleware // The middleware manager.
StaticFile *StaticFile // Static file object when static file serving.
Context *gmap.StrAnyMap // Custom context map for internal usage purpose.
Context context.Context // Custom context map for internal usage purpose.
handlers []*handlerParsedItem // All matched handlers containing handler, hook and middleware for this request .
hasHookHandler bool // A bool marking whether there's hook handler in the handlers for performance purpose.
hasServeHandler bool // A bool marking whether there's serving handler in the handlers for performance purpose.
@ -68,7 +68,7 @@ func newRequest(s *Server, r *http.Request, w http.ResponseWriter) *Request {
Request: r,
Response: newResponse(s, w),
EnterTime: gtime.TimestampMilli(),
Context: gmap.NewStrAnyMap(),
Context: r.Context(),
}
request.Cookie = GetCookie(request)
request.Session = s.sessionManager.New(request.GetSessionId())

View File

@ -7,6 +7,7 @@
package ghttp_test
import (
"context"
"fmt"
"testing"
"time"
@ -21,15 +22,15 @@ func Test_Context(t *testing.T) {
s := g.Server(p)
s.Group("/", func(group *ghttp.RouterGroup) {
group.Middleware(func(r *ghttp.Request) {
r.Context.Set("traceid", 123)
r.Context = context.WithValue(r.Context, "traceid", 123)
r.Middleware.Next()
})
group.GET("/", func(r *ghttp.Request) {
r.Response.Write(r.Context.Get("traceid"))
r.Response.Write(r.Context.Value("traceid"))
})
})
s.SetPort(p)
//s.SetDumpRouterMap(false)
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()