mirror of
https://github.com/weloe/token-go.git
synced 2025-10-07 08:20:56 +08:00
feat: add web context and add go net/http web context
This commit is contained in:
30
ctx/go-http-context/request-storage.go
Normal file
30
ctx/go-http-context/request-storage.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package go_http_context
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type HttpReqStorage struct {
|
||||
source context.Context
|
||||
}
|
||||
|
||||
func NewReqStorage(req *http.Request) *HttpReqStorage {
|
||||
return &HttpReqStorage{source: req.Context()}
|
||||
}
|
||||
|
||||
func (r HttpReqStorage) Source() interface{} {
|
||||
return r.source
|
||||
}
|
||||
|
||||
func (r HttpReqStorage) Get(key string) interface{} {
|
||||
return r.source.Value(key)
|
||||
}
|
||||
|
||||
func (r HttpReqStorage) Set(key string, value string) {
|
||||
r.source = context.WithValue(r.source, key, value)
|
||||
}
|
||||
|
||||
func (r HttpReqStorage) Delete(key string) {
|
||||
r.source = context.WithValue(r.source, key, nil)
|
||||
}
|
Reference in New Issue
Block a user