authorization enforced on webpages
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"ruben/inventory2/internal/site/redirect"
|
||||
)
|
||||
|
||||
type (
|
||||
bodyRes struct {
|
||||
body io.ReadCloser
|
||||
res Response
|
||||
}
|
||||
)
|
||||
|
||||
var _ Response = bodyRes{}
|
||||
|
||||
func Body(body io.ReadCloser) Response {
|
||||
return bodyRes{
|
||||
body: body,
|
||||
}
|
||||
}
|
||||
|
||||
func (b bodyRes) String() string {
|
||||
if b.res != nil {
|
||||
return fmt.Sprintf(`{"body": %q, "nested": %s}`, b.body, b.res)
|
||||
}
|
||||
return fmt.Sprintf(`{"body": %q}`, b.body)
|
||||
}
|
||||
|
||||
func (b bodyRes) wrap(res Response) Response {
|
||||
b.res = res
|
||||
return b
|
||||
}
|
||||
|
||||
func (b bodyRes) Status(code int) Response {
|
||||
return Status(code).wrap(b)
|
||||
}
|
||||
|
||||
func (b bodyRes) Redirect(code redirect.Code, to string) Response {
|
||||
return Redirect(code, to).wrap(b)
|
||||
}
|
||||
|
||||
func (b bodyRes) JSON(body any) Response {
|
||||
return JSON(body).wrap(b)
|
||||
}
|
||||
|
||||
func (b bodyRes) Body(body io.ReadCloser) Response {
|
||||
b.body = body
|
||||
return b
|
||||
}
|
||||
|
||||
func (b bodyRes) Cookie(ck http.Cookie) Response {
|
||||
return Cookie(ck).wrap(b)
|
||||
}
|
||||
|
||||
func (b bodyRes) getStatus() (int, bool) {
|
||||
if b.res == nil {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
return b.res.getStatus()
|
||||
}
|
||||
|
||||
func (b bodyRes) getRedirect() (code redirect.Code, to string, ok bool) {
|
||||
if b.res == nil {
|
||||
return 0, "", false
|
||||
}
|
||||
|
||||
return b.res.getRedirect()
|
||||
}
|
||||
|
||||
func (b bodyRes) getBody() (body io.ReadCloser, ok bool, err error) {
|
||||
return b.body, true, nil
|
||||
}
|
||||
|
||||
func (b bodyRes) getCookies() []http.Cookie {
|
||||
if b.res != nil {
|
||||
return b.res.getCookies()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -24,9 +24,9 @@ func Cookie(c http.Cookie) Response {
|
||||
|
||||
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, "nested": %s}`, &c.cookie, c.res)
|
||||
}
|
||||
return fmt.Sprintf(`{"cookie": %q}`, c.cookie)
|
||||
return fmt.Sprintf(`{"cookie": %q}`, &c.cookie)
|
||||
}
|
||||
|
||||
func (c cookieRes) wrap(res Response) Response {
|
||||
@@ -46,6 +46,10 @@ 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)
|
||||
}
|
||||
|
||||
@@ -48,6 +48,12 @@ func Forbidden() ErrorResponse {
|
||||
}
|
||||
}
|
||||
|
||||
func Conflict() ErrorResponse {
|
||||
return ErrorResponse{
|
||||
status: http.StatusConflict,
|
||||
}
|
||||
}
|
||||
|
||||
// builder pattern implementation
|
||||
|
||||
func (e ErrorResponse) Msg(msg string) ErrorResponse {
|
||||
@@ -55,6 +61,11 @@ func (e ErrorResponse) Msg(msg string) ErrorResponse {
|
||||
return e
|
||||
}
|
||||
|
||||
func (e ErrorResponse) Msgf(format string, args ...any) ErrorResponse {
|
||||
e.msg = fmt.Sprintf(format, args...)
|
||||
return e
|
||||
}
|
||||
|
||||
func (e ErrorResponse) Status(status int) ErrorResponse {
|
||||
e.status = status
|
||||
return e
|
||||
@@ -92,33 +103,33 @@ func (e ErrorResponse) Unwrap() error {
|
||||
|
||||
// nested response value resolution
|
||||
|
||||
func (e ErrorResponse) getStatus() (int, bool) {
|
||||
func (e ErrorResponse) GetStatus() (int, bool) {
|
||||
if e.status != 0 {
|
||||
return e.status, true
|
||||
}
|
||||
|
||||
ce, ok := getError(e.err)
|
||||
ce, ok := GetError(e.err)
|
||||
if ok {
|
||||
return ce.getStatus()
|
||||
return ce.GetStatus()
|
||||
}
|
||||
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func (e ErrorResponse) getMsg() (string, bool) {
|
||||
func (e ErrorResponse) GetMsg() (string, bool) {
|
||||
if e.msg != "" {
|
||||
return e.msg, true
|
||||
}
|
||||
|
||||
ce, ok := getError(e.err)
|
||||
ce, ok := GetError(e.err)
|
||||
if ok {
|
||||
return ce.getMsg()
|
||||
return ce.GetMsg()
|
||||
}
|
||||
|
||||
return "", false
|
||||
}
|
||||
|
||||
func getError(err error) (e ErrorResponse, ok bool) {
|
||||
func GetError(err error) (e ErrorResponse, ok bool) {
|
||||
ok = errors.As(err, &e)
|
||||
return e, ok
|
||||
}
|
||||
|
||||
@@ -49,6 +49,10 @@ func (j jsonRes) JSON(body any) Response {
|
||||
return j
|
||||
}
|
||||
|
||||
func (j jsonRes) Body(body io.ReadCloser) Response {
|
||||
return Body(body).wrap(j)
|
||||
}
|
||||
|
||||
func (j jsonRes) Cookie(ck http.Cookie) Response {
|
||||
return Cookie(ck).wrap(j)
|
||||
}
|
||||
|
||||
@@ -87,6 +87,10 @@ 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)
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ type (
|
||||
Response interface {
|
||||
Status(int) Response
|
||||
Redirect(code redirect.Code, to string) Response
|
||||
Body(io.ReadCloser) Response
|
||||
JSON(any) Response
|
||||
Cookie(http.Cookie) Response
|
||||
|
||||
|
||||
@@ -48,6 +48,10 @@ 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)
|
||||
}
|
||||
|
||||
@@ -46,8 +46,8 @@ func Write(w http.ResponseWriter, r *http.Request, res Response) {
|
||||
func WriteError(w http.ResponseWriter, err error) {
|
||||
var status int
|
||||
|
||||
if e, ok := getError(err); ok {
|
||||
if status, ok = e.getStatus(); !ok {
|
||||
if e, ok := GetError(err); ok {
|
||||
if status, ok = e.GetStatus(); !ok {
|
||||
status = http.StatusInternalServerError
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user