170 lines
3.0 KiB
Go
170 lines
3.0 KiB
Go
package response
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
|
|
"ruben/inventory2/server/redirect"
|
|
)
|
|
|
|
type (
|
|
htmlRes struct {
|
|
body []byte
|
|
reader io.Reader
|
|
res Response
|
|
}
|
|
)
|
|
|
|
var _ Response = htmlRes{}
|
|
|
|
func HTML(body []byte) Response {
|
|
return htmlRes{
|
|
body: body,
|
|
}
|
|
}
|
|
|
|
func HTMLReader(body io.Reader) Response {
|
|
return htmlRes{
|
|
reader: body,
|
|
}
|
|
}
|
|
|
|
func (h htmlRes) String() string {
|
|
if h.res != nil {
|
|
return fmt.Sprintf(`{"body": %q, "nested": %s}`, string(h.body), h.res)
|
|
}
|
|
return fmt.Sprintf(`{"body": %q}`, h.body)
|
|
}
|
|
|
|
func (h htmlRes) wrap(res Response) Response {
|
|
h.res = res
|
|
return h
|
|
}
|
|
|
|
func (h htmlRes) Status(code int) Response {
|
|
return Status(code).wrap(h)
|
|
}
|
|
|
|
func (h htmlRes) Redirect(code redirect.Code, to string) Response {
|
|
return Redirect(code, to).wrap(h)
|
|
}
|
|
|
|
func (h htmlRes) HTML(body []byte) Response {
|
|
h.body = body
|
|
h.reader = nil
|
|
return h
|
|
}
|
|
|
|
func (h htmlRes) HTMLReader(body io.Reader) Response {
|
|
h.body = nil
|
|
h.reader = body
|
|
return h
|
|
}
|
|
|
|
func (h htmlRes) JSON(body any) Response {
|
|
return JSON(body).wrap(h)
|
|
}
|
|
|
|
func (h htmlRes) Body(body io.ReadCloser) Response {
|
|
return Body(body).wrap(h)
|
|
}
|
|
|
|
func (h htmlRes) Cookie(ck http.Cookie) Response {
|
|
return Cookie(ck).wrap(h)
|
|
}
|
|
|
|
func (h htmlRes) HXPushURL(v string) Response {
|
|
return HXPushURL(v).wrap(h)
|
|
}
|
|
|
|
func (h htmlRes) HXLocation(v string) Response {
|
|
return HXLocation(v).wrap(h)
|
|
}
|
|
|
|
func (h htmlRes) HXPushUrl(v string) Response {
|
|
return HXPushUrl(v).wrap(h)
|
|
}
|
|
|
|
func (h htmlRes) HXRedirect(v string) Response {
|
|
return HXRedirect(v).wrap(h)
|
|
}
|
|
|
|
func (h htmlRes) HXRefresh(v string) Response {
|
|
return HXRefresh(v).wrap(h)
|
|
}
|
|
|
|
func (h htmlRes) HXReplaceUrl(v string) Response {
|
|
return HXReplaceUrl(v).wrap(h)
|
|
}
|
|
|
|
func (h htmlRes) HXReswap(v string) Response {
|
|
return HXReswap(v).wrap(h)
|
|
}
|
|
|
|
func (h htmlRes) HXRetarget(v string) Response {
|
|
return HXRetarget(v).wrap(h)
|
|
}
|
|
|
|
func (h htmlRes) HXReselect(v string) Response {
|
|
return HXReselect(v).wrap(h)
|
|
}
|
|
|
|
func (h htmlRes) HXTrigger(v string) Response {
|
|
return HXTrigger(v).wrap(h)
|
|
}
|
|
|
|
func (h htmlRes) HXTriggerAfterSettle(v string) Response {
|
|
return HXTriggerAfterSettle(v).wrap(h)
|
|
}
|
|
|
|
func (h htmlRes) HXTriggerAfterSwap(v string) Response {
|
|
return HXTriggerAfterSwap(v).wrap(h)
|
|
}
|
|
|
|
func (h htmlRes) GetStatus() (int, bool) {
|
|
if h.res == nil {
|
|
return 0, false
|
|
}
|
|
|
|
return h.res.GetStatus()
|
|
}
|
|
|
|
func (h htmlRes) getHeaders() [][2]string {
|
|
if h.res != nil {
|
|
return h.res.getHeaders()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (h htmlRes) GetRedirect() (code redirect.Code, to string, ok bool) {
|
|
if h.res == nil {
|
|
return 0, "", false
|
|
}
|
|
|
|
return h.res.GetRedirect()
|
|
}
|
|
|
|
func (h htmlRes) getBody() (body io.ReadCloser, ok bool, err error) {
|
|
var r io.Reader
|
|
if h.reader != nil {
|
|
r = h.reader
|
|
} else {
|
|
r = bytes.NewBuffer(h.body)
|
|
}
|
|
return io.NopCloser(r), true, nil
|
|
}
|
|
|
|
func (h htmlRes) getCookies() []http.Cookie {
|
|
if h.res != nil {
|
|
return h.res.getCookies()
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (h htmlRes) getContentType() (contentType string, ok bool) {
|
|
return "text/html", true
|
|
}
|