diff options
author | Wolfy-J <[email protected]> | 2018-06-05 22:48:27 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2018-06-05 22:48:27 +0300 |
commit | 6adaf713b47c9a3ab3a516e21d2d4ecf7f2075d6 (patch) | |
tree | 6bcf1bfea1e2f87a3ae7065612c0df43c90c1cdc /http/response.go | |
parent | 3112f9b58c73773cea972fd79f04d33f8f7d7edd (diff) |
breaking the tests
Diffstat (limited to 'http/response.go')
-rw-r--r-- | http/response.go | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/http/response.go b/http/response.go new file mode 100644 index 00000000..2736c4ab --- /dev/null +++ b/http/response.go @@ -0,0 +1,42 @@ +package http + +import ( + "encoding/json" + "github.com/spiral/roadrunner" + "net/http" +) + +// Response handles PSR7 response logic. +type Response struct { + // Status contains response status. + Status int `json:"status"` + + // Headers contains list of response headers. + Headers map[string][]string `json:"headers"` + + // associated body payload. + body []byte +} + +// NewResponse creates new response based on given roadrunner payload. +func NewResponse(p *roadrunner.Payload) (*Response, error) { + r := &Response{body: p.Body} + if err := json.Unmarshal(p.Context, r); err != nil { + return nil, err + } + + return r, nil +} + +// Write writes response headers, status and body into ResponseWriter. +func (r *Response) Write(w http.ResponseWriter) { + for k, v := range r.Headers { + for _, h := range v { + w.Header().Add(k, h) + + } + } + + w.WriteHeader(r.Status) + w.Write(r.body) +} |