feat: add web context and add go net/http web context

This commit is contained in:
weloe
2023-05-02 15:06:54 +08:00
parent 35c206716d
commit c04ab085b4
14 changed files with 1024 additions and 1 deletions

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