diff options
author | Wolfy-J <[email protected]> | 2018-07-07 19:23:15 -0700 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2018-07-07 19:23:15 -0700 |
commit | 98f7ce699b559afd74d4e9d607e64b9a8367d762 (patch) | |
tree | a1553b7ed55109a1158f92dbf0eae5520754c5f7 /service/http | |
parent | afde365ba6210569b7d48dec0c07434a7f8a1fd8 (diff) |
attributes support
Diffstat (limited to 'service/http')
-rw-r--r-- | service/http/attributes.go | 63 | ||||
-rw-r--r-- | service/http/request.go | 16 | ||||
-rw-r--r-- | service/http/service.go | 1 |
3 files changed, 74 insertions, 6 deletions
diff --git a/service/http/attributes.go b/service/http/attributes.go new file mode 100644 index 00000000..fe069adf --- /dev/null +++ b/service/http/attributes.go @@ -0,0 +1,63 @@ +package http + +import ( + "context" + "net/http" +) + +const contextKey = "psr:attributes" + +type attrs map[string]interface{} + +// InitAttributes returns request with new context and attribute bag. +func InitAttributes(r *http.Request) *http.Request { + return r.WithContext(context.WithValue(r.Context(), contextKey, attrs{})) +} + +// AllAttributes returns all context attributes. +func AllAttributes(r *http.Request) map[string]interface{} { + v := r.Context().Value(contextKey) + if v == nil { + return nil + } + + return v.(attrs) +} + +// Get gets the value from request context. It replaces any existing +// values. +func GetAttribute(r *http.Request, key string) interface{} { + v := r.Context().Value(contextKey) + if v == nil { + return "" + } + + return v.(attrs).Get(key) +} + +// Set sets the key to value. It replaces any existing +// values. Context specific. +func SetAttribute(r *http.Request, key string, value interface{}) { + v := r.Context().Value(contextKey) + v.(attrs).Set(key, value) +} + +// Get gets the value associated with the given key. +func (v attrs) Get(key string) interface{} { + if v == nil { + return "" + } + + return v[key] +} + +// Set sets the key to value. It replaces any existing +// values. +func (v attrs) Set(key string, value interface{}) { + v[key] = value +} + +// Del deletes the value associated with key. +func (v attrs) Del(key string) { + delete(v, key) +}
\ No newline at end of file diff --git a/service/http/request.go b/service/http/request.go index 9281a3f5..21566416 100644 --- a/service/http/request.go +++ b/service/http/request.go @@ -44,6 +44,9 @@ type Request struct { // Uploads contains list of uploaded files, their names, sized and associations with temporary files. Uploads *Uploads `json:"uploads"` + // Attributes can be set by chained middleware to safely pass value from Golang to PHP. See: GetAttribute, SetAttribute functions. + Attributes map[string]interface{} `json:"attributes"` + // request body can be parsedData or []byte body interface{} } @@ -51,12 +54,13 @@ type Request struct { // NewRequest creates new PSR7 compatible request using net/http request. func NewRequest(r *http.Request, cfg *UploadsConfig) (req *Request, err error) { req = &Request{ - Protocol: r.Proto, - Method: r.Method, - URI: uri(r), - Headers: r.Header, - Cookies: make(map[string]string), - RawQuery: r.URL.RawQuery, + Protocol: r.Proto, + Method: r.Method, + URI: uri(r), + Headers: r.Header, + Cookies: make(map[string]string), + RawQuery: r.URL.RawQuery, + Attributes: AllAttributes(r), } for _, c := range r.Cookies() { diff --git a/service/http/service.go b/service/http/service.go index cef019b3..460175b9 100644 --- a/service/http/service.go +++ b/service/http/service.go @@ -113,6 +113,7 @@ func (s *Service) Stop() { // middleware handles connection using set of mdws and rr PSR-7 server. func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) { + r = InitAttributes(r) for _, m := range s.mdws { if m(w, r) { return |