193 lines
3.5 KiB
Go
193 lines
3.5 KiB
Go
package response
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
|
|
"ruben/inventory2/server/redirect"
|
|
)
|
|
|
|
type (
|
|
redirectRes struct {
|
|
code redirect.Code
|
|
to string
|
|
res Response
|
|
}
|
|
)
|
|
|
|
var _ Response = redirectRes{}
|
|
|
|
// convenience constructors
|
|
|
|
func MovedPermanently(to string) Response {
|
|
return redirectRes{
|
|
code: redirect.MovedPermanently,
|
|
to: to,
|
|
}
|
|
}
|
|
|
|
func Found(to string) Response {
|
|
return redirectRes{
|
|
code: redirect.Found,
|
|
to: to,
|
|
}
|
|
}
|
|
|
|
func SeeOther(to string) Response {
|
|
return redirectRes{
|
|
code: redirect.SeeOther,
|
|
to: to,
|
|
}
|
|
}
|
|
|
|
func TemporaryRedirect(to string) Response {
|
|
return redirectRes{
|
|
code: redirect.TemporaryRedirect,
|
|
to: to,
|
|
}
|
|
}
|
|
|
|
func PermanentRedirect(to string) Response {
|
|
return redirectRes{
|
|
code: redirect.PermanentRedirect,
|
|
to: to,
|
|
}
|
|
}
|
|
|
|
func Redirect(code redirect.Code, to string) Response {
|
|
return redirectRes{
|
|
code: code,
|
|
to: to,
|
|
}
|
|
}
|
|
|
|
func (r redirectRes) String() string {
|
|
if r.res != nil {
|
|
return fmt.Sprintf(`{"redirect": {"code": %d, "to": %q}, "nested": %s}`, r.code, r.to, r.res)
|
|
}
|
|
return fmt.Sprintf(`{"redirect": {"code": %d, "to": %q}}`, r.code, r.to)
|
|
}
|
|
|
|
func (r redirectRes) wrap(res Response) Response {
|
|
r.res = res
|
|
return r
|
|
}
|
|
|
|
func (r redirectRes) Status(code int) Response {
|
|
return Status(code).wrap(r)
|
|
}
|
|
|
|
func (r redirectRes) Redirect(code redirect.Code, to string) Response {
|
|
r.code = code
|
|
r.to = to
|
|
return r
|
|
}
|
|
|
|
func (r redirectRes) HTML(body []byte) Response {
|
|
return HTML(body).wrap(r)
|
|
}
|
|
|
|
func (r redirectRes) JSON(body any) Response {
|
|
return JSON(body).wrap(r)
|
|
}
|
|
|
|
func (r redirectRes) Body(body io.ReadCloser) Response {
|
|
return Body(body).wrap(r)
|
|
}
|
|
|
|
func (r redirectRes) Cookie(ck http.Cookie) Response {
|
|
return Cookie(ck).wrap(r)
|
|
}
|
|
|
|
func (r redirectRes) HXPushURL(v string) Response {
|
|
return HXPushURL(v).wrap(r)
|
|
}
|
|
|
|
func (r redirectRes) HXLocation(v string) Response {
|
|
return HXLocation(v).wrap(r)
|
|
}
|
|
|
|
func (r redirectRes) HXPushUrl(v string) Response {
|
|
return HXPushUrl(v).wrap(r)
|
|
}
|
|
|
|
func (r redirectRes) HXRedirect(v string) Response {
|
|
return HXRedirect(v).wrap(r)
|
|
}
|
|
|
|
func (r redirectRes) HXRefresh(v string) Response {
|
|
return HXRefresh(v).wrap(r)
|
|
}
|
|
|
|
func (r redirectRes) HXReplaceUrl(v string) Response {
|
|
return HXReplaceUrl(v).wrap(r)
|
|
}
|
|
|
|
func (r redirectRes) HXReswap(v string) Response {
|
|
return HXReswap(v).wrap(r)
|
|
}
|
|
|
|
func (r redirectRes) HXRetarget(v string) Response {
|
|
return HXRetarget(v).wrap(r)
|
|
}
|
|
|
|
func (r redirectRes) HXReselect(v string) Response {
|
|
return HXReselect(v).wrap(r)
|
|
}
|
|
|
|
func (r redirectRes) HXTrigger(v string) Response {
|
|
return HXTrigger(v).wrap(r)
|
|
}
|
|
|
|
func (r redirectRes) HXTriggerAfterSettle(v string) Response {
|
|
return HXTriggerAfterSettle(v).wrap(r)
|
|
}
|
|
|
|
func (r redirectRes) HXTriggerAfterSwap(v string) Response {
|
|
return HXTriggerAfterSwap(v).wrap(r)
|
|
}
|
|
|
|
func (r redirectRes) GetStatus() (int, bool) {
|
|
if r.res == nil {
|
|
return 0, false
|
|
}
|
|
|
|
return r.res.GetStatus()
|
|
}
|
|
|
|
func (r redirectRes) getHeaders() [][2]string {
|
|
if r.res != nil {
|
|
return r.res.getHeaders()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r redirectRes) GetRedirect() (code redirect.Code, to string, ok bool) {
|
|
return r.code, r.to, true
|
|
}
|
|
|
|
func (r redirectRes) getBody() (body io.ReadCloser, ok bool, err error) {
|
|
if r.res == nil {
|
|
return nil, false, nil
|
|
}
|
|
|
|
return r.res.getBody()
|
|
}
|
|
|
|
func (r redirectRes) getCookies() []http.Cookie {
|
|
if r.res != nil {
|
|
return r.res.getCookies()
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r redirectRes) getContentType() (contentType string, ok bool) {
|
|
if r.res != nil {
|
|
return r.res.getContentType()
|
|
}
|
|
|
|
return "", false
|
|
}
|