98 lines
1.6 KiB
Go
98 lines
1.6 KiB
Go
package response
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
|
|
"ruben/inventory2/internal/site/redirect"
|
|
)
|
|
|
|
type (
|
|
statusRes struct {
|
|
code int
|
|
res Response
|
|
}
|
|
)
|
|
|
|
var _ Response = statusRes{}
|
|
|
|
func Status(code int) Response {
|
|
return statusRes{
|
|
code: code,
|
|
}
|
|
}
|
|
|
|
func (s statusRes) String() string {
|
|
if s.res != nil {
|
|
return fmt.Sprintf(`{"status": %d, "nested": %s}`, s.code, s.res)
|
|
}
|
|
return fmt.Sprintf(`{"status": %d}`, s.code)
|
|
}
|
|
|
|
func (s statusRes) wrap(res Response) Response {
|
|
s.res = res
|
|
return s
|
|
}
|
|
|
|
func (s statusRes) Status(code int) Response {
|
|
s.code = code
|
|
return s
|
|
}
|
|
|
|
func (s statusRes) Redirect(code redirect.Code, to string) Response {
|
|
return Redirect(code, to).wrap(s)
|
|
}
|
|
|
|
func (s statusRes) HTML(body []byte) Response {
|
|
return HTML(body).wrap(s)
|
|
}
|
|
|
|
func (s statusRes) JSON(body any) Response {
|
|
return JSON(body).wrap(s)
|
|
}
|
|
|
|
func (s statusRes) Body(body io.ReadCloser) Response {
|
|
return Body(body).wrap(s)
|
|
}
|
|
|
|
func (s statusRes) Cookie(ck http.Cookie) Response {
|
|
return Cookie(ck).wrap(s)
|
|
}
|
|
|
|
func (s statusRes) getStatus() (int, bool) {
|
|
return s.code, true
|
|
}
|
|
|
|
func (s statusRes) getRedirect() (code redirect.Code, to string, ok bool) {
|
|
if s.res == nil {
|
|
return 0, "", false
|
|
}
|
|
|
|
return s.res.getRedirect()
|
|
}
|
|
|
|
func (s statusRes) getBody() (body io.ReadCloser, ok bool, err error) {
|
|
if s.res == nil {
|
|
return nil, false, nil
|
|
}
|
|
|
|
return s.res.getBody()
|
|
}
|
|
|
|
func (s statusRes) getCookies() []http.Cookie {
|
|
if s.res != nil {
|
|
return s.res.getCookies()
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s statusRes) getContentType() (contentType string, ok bool) {
|
|
if s.res != nil {
|
|
return s.res.getContentType()
|
|
}
|
|
|
|
return "", false
|
|
}
|