100 lines
1.7 KiB
Go
100 lines
1.7 KiB
Go
package response
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"ruben/inventory2/internal/server/redirect"
|
|
)
|
|
|
|
type (
|
|
cookieRes struct {
|
|
cookie http.Cookie
|
|
res Response
|
|
}
|
|
)
|
|
|
|
var _ Response = cookieRes{}
|
|
|
|
func Cookie(c http.Cookie) Response {
|
|
return cookieRes{
|
|
cookie: c,
|
|
}
|
|
}
|
|
|
|
func (c cookieRes) String() string {
|
|
if c.res != nil {
|
|
return fmt.Sprintf(`{"cookie": %q, "nested": %s}`, &c.cookie, c.res)
|
|
}
|
|
return fmt.Sprintf(`{"cookie": %q}`, &c.cookie)
|
|
}
|
|
|
|
func (c cookieRes) wrap(res Response) Response {
|
|
c.res = res
|
|
return c
|
|
}
|
|
|
|
func (c cookieRes) Status(code int) Response {
|
|
return Status(code).wrap(c)
|
|
}
|
|
|
|
func (c cookieRes) Redirect(code redirect.Code, to string) Response {
|
|
return Redirect(code, to).wrap(c)
|
|
}
|
|
|
|
func (c cookieRes) HTML(body []byte) Response {
|
|
return HTML(body).wrap(c)
|
|
}
|
|
|
|
func (c cookieRes) JSON(body any) Response {
|
|
return JSON(body).wrap(c)
|
|
}
|
|
|
|
func (c cookieRes) Body(body io.ReadCloser) Response {
|
|
return Body(body).wrap(c)
|
|
}
|
|
|
|
func (c cookieRes) Cookie(ck http.Cookie) Response {
|
|
return Cookie(ck).wrap(c)
|
|
}
|
|
|
|
func (c cookieRes) GetStatus() (int, bool) {
|
|
if c.res == nil {
|
|
return 0, false
|
|
}
|
|
|
|
return c.res.GetStatus()
|
|
}
|
|
|
|
func (c cookieRes) GetRedirect() (code redirect.Code, to string, ok bool) {
|
|
if c.res == nil {
|
|
return 0, "", false
|
|
}
|
|
|
|
return c.res.GetRedirect()
|
|
}
|
|
|
|
func (c cookieRes) getBody() (body io.ReadCloser, ok bool, err error) {
|
|
if c.res == nil {
|
|
return nil, false, nil
|
|
}
|
|
|
|
return c.res.getBody()
|
|
}
|
|
|
|
func (c cookieRes) getCookies() []http.Cookie {
|
|
if c.res != nil {
|
|
return append(c.res.getCookies(), c.cookie)
|
|
}
|
|
|
|
return []http.Cookie{c.cookie}
|
|
}
|
|
|
|
func (c cookieRes) getContentType() (contentType string, ok bool) {
|
|
if c.res != nil {
|
|
return c.res.getContentType()
|
|
}
|
|
|
|
return "", false
|
|
}
|