first drafts for inventory sync page
This commit is contained in:
@@ -42,6 +42,10 @@ func (b bodyRes) Redirect(code redirect.Code, to string) Response {
|
||||
return Redirect(code, to).wrap(b)
|
||||
}
|
||||
|
||||
func (b bodyRes) HTML(body []byte) Response {
|
||||
return HTML(body).wrap(b)
|
||||
}
|
||||
|
||||
func (b bodyRes) JSON(body any) Response {
|
||||
return JSON(body).wrap(b)
|
||||
}
|
||||
@@ -82,3 +86,11 @@ func (b bodyRes) getCookies() []http.Cookie {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b bodyRes) getContentType() (contentType string, ok bool) {
|
||||
if b.res != nil {
|
||||
return b.res.getContentType()
|
||||
}
|
||||
|
||||
return "", false
|
||||
}
|
||||
|
||||
@@ -42,6 +42,10 @@ 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)
|
||||
}
|
||||
@@ -85,3 +89,11 @@ func (c cookieRes) getCookies() []http.Cookie {
|
||||
|
||||
return []http.Cookie{c.cookie}
|
||||
}
|
||||
|
||||
func (c cookieRes) getContentType() (contentType string, ok bool) {
|
||||
if c.res != nil {
|
||||
return c.res.getContentType()
|
||||
}
|
||||
|
||||
return "", false
|
||||
}
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"ruben/inventory2/internal/site/redirect"
|
||||
)
|
||||
|
||||
type (
|
||||
htmlRes struct {
|
||||
body []byte
|
||||
res Response
|
||||
}
|
||||
)
|
||||
|
||||
var _ Response = htmlRes{}
|
||||
|
||||
func HTML(body []byte) Response {
|
||||
return htmlRes{
|
||||
body: 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
|
||||
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) getStatus() (int, bool) {
|
||||
if h.res == nil {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
return h.res.getStatus()
|
||||
}
|
||||
|
||||
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) {
|
||||
return io.NopCloser(bytes.NewBuffer(h.body)), 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
|
||||
}
|
||||
@@ -44,6 +44,10 @@ func (j jsonRes) Redirect(code redirect.Code, to string) Response {
|
||||
return Redirect(code, to).wrap(j)
|
||||
}
|
||||
|
||||
func (j jsonRes) HTML(body []byte) Response {
|
||||
return HTML(body).wrap(j)
|
||||
}
|
||||
|
||||
func (j jsonRes) JSON(body any) Response {
|
||||
j.body = body
|
||||
return j
|
||||
@@ -85,3 +89,7 @@ func (j jsonRes) getCookies() []http.Cookie {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (j jsonRes) getContentType() (contentType string, ok bool) {
|
||||
return "application/json", true
|
||||
}
|
||||
|
||||
@@ -83,6 +83,10 @@ func (r redirectRes) Redirect(code redirect.Code, to string) Response {
|
||||
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)
|
||||
}
|
||||
@@ -122,3 +126,11 @@ func (r redirectRes) getCookies() []http.Cookie {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r redirectRes) getContentType() (contentType string, ok bool) {
|
||||
if r.res != nil {
|
||||
return r.res.getContentType()
|
||||
}
|
||||
|
||||
return "", false
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ type (
|
||||
Status(int) Response
|
||||
Redirect(code redirect.Code, to string) Response
|
||||
Body(io.ReadCloser) Response
|
||||
HTML([]byte) Response
|
||||
JSON(any) Response
|
||||
Cookie(http.Cookie) Response
|
||||
|
||||
@@ -19,6 +20,7 @@ type (
|
||||
getBody() (body io.ReadCloser, ok bool, err error)
|
||||
getRedirect() (code redirect.Code, to string, ok bool)
|
||||
getCookies() []http.Cookie
|
||||
getContentType() (contentType string, ok bool)
|
||||
|
||||
wrap(Response) Response
|
||||
}
|
||||
|
||||
@@ -44,6 +44,10 @@ 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)
|
||||
}
|
||||
@@ -83,3 +87,11 @@ func (s statusRes) getCookies() []http.Cookie {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s statusRes) getContentType() (contentType string, ok bool) {
|
||||
if s.res != nil {
|
||||
return s.res.getContentType()
|
||||
}
|
||||
|
||||
return "", false
|
||||
}
|
||||
|
||||
@@ -14,6 +14,10 @@ func Write(w http.ResponseWriter, r *http.Request, res Response) {
|
||||
hdrs.Add("Set-Cookie", ck.String())
|
||||
}
|
||||
|
||||
if ct, ok := res.getContentType(); ok {
|
||||
hdrs.Add("Content-Type", ct)
|
||||
}
|
||||
|
||||
if code, to, ok := res.getRedirect(); ok {
|
||||
http.Redirect(w, r, to, code.Int())
|
||||
return
|
||||
@@ -44,7 +48,7 @@ func Write(w http.ResponseWriter, r *http.Request, res Response) {
|
||||
}
|
||||
|
||||
func WriteError(w http.ResponseWriter, err error) {
|
||||
var status int
|
||||
status := http.StatusInternalServerError
|
||||
|
||||
if e, ok := GetError(err); ok {
|
||||
if status, ok = e.GetStatus(); !ok {
|
||||
|
||||
Reference in New Issue
Block a user