mirror of
https://github.com/weloe/token-go.git
synced 2025-10-06 16:07:18 +08:00
feat: add web context and add go net/http web context
This commit is contained in:
52
ctx/go-http-context/request.go
Normal file
52
ctx/go-http-context/request.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package go_http_context
|
||||
|
||||
import (
|
||||
"github.com/weloe/token-go/ctx"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var _ ctx.Request = (*HttpRequest)(nil)
|
||||
|
||||
type HttpRequest struct {
|
||||
source *http.Request
|
||||
}
|
||||
|
||||
func NewHttpRequest(r *http.Request) *HttpRequest {
|
||||
return &HttpRequest{r}
|
||||
}
|
||||
|
||||
func (d *HttpRequest) Source() interface{} {
|
||||
return d.source
|
||||
}
|
||||
|
||||
func (d *HttpRequest) Header(key string) string {
|
||||
return d.source.Header.Get(key)
|
||||
}
|
||||
|
||||
func (d *HttpRequest) PostForm(key string) string {
|
||||
return d.source.PostFormValue(key)
|
||||
}
|
||||
|
||||
func (d *HttpRequest) Query(key string) string {
|
||||
return d.source.URL.Query().Get(key)
|
||||
}
|
||||
|
||||
func (d *HttpRequest) Path() string {
|
||||
return d.source.URL.Path
|
||||
}
|
||||
|
||||
func (d *HttpRequest) Url() string {
|
||||
return d.source.URL.String()
|
||||
}
|
||||
|
||||
func (d *HttpRequest) Method() string {
|
||||
return d.source.Method
|
||||
}
|
||||
|
||||
func (d *HttpRequest) Cookie(key string) string {
|
||||
cookie, err := d.source.Cookie(key)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return cookie.Value
|
||||
}
|
Reference in New Issue
Block a user