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,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)
}