goext/ginext/preContext.go

212 lines
6.2 KiB
Go
Raw Permalink Normal View History

2023-07-18 15:23:32 +02:00
package ginext
import (
2023-11-03 16:53:41 +01:00
"bytes"
2023-07-18 15:23:32 +02:00
"context"
2023-07-24 11:47:47 +02:00
"fmt"
2023-07-18 15:23:32 +02:00
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
2023-11-03 16:53:41 +01:00
"gogs.mikescher.com/BlackForestBytes/goext/dataext"
2023-07-24 11:47:47 +02:00
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
2023-07-18 15:23:32 +02:00
"gogs.mikescher.com/BlackForestBytes/goext/langext"
2023-11-03 16:53:41 +01:00
"io"
2023-07-18 15:23:32 +02:00
"runtime/debug"
2023-08-11 16:32:34 +02:00
"time"
2023-07-18 15:23:32 +02:00
)
type PreContext struct {
2024-07-16 15:08:37 +02:00
ginCtx *gin.Context
wrapper *GinWrapper
uri any
query any
body any
rawbody *[]byte
form any
header any
timeout *time.Duration
persistantData *preContextData // must be a ptr, so that we can get the values back in out Wrap func
ignoreWrongContentType bool
2023-12-02 13:07:36 +01:00
}
type preContextData struct {
2023-12-02 13:38:17 +01:00
sessionObj SessionObject
2023-07-18 15:23:32 +02:00
}
func (pctx *PreContext) URI(uri any) *PreContext {
pctx.uri = uri
return pctx
}
func (pctx *PreContext) Query(query any) *PreContext {
pctx.query = query
return pctx
}
func (pctx *PreContext) Body(body any) *PreContext {
pctx.body = body
return pctx
}
2023-11-03 16:53:41 +01:00
func (pctx *PreContext) RawBody(rawbody *[]byte) *PreContext {
pctx.rawbody = rawbody
return pctx
}
2023-07-18 15:23:32 +02:00
func (pctx *PreContext) Form(form any) *PreContext {
pctx.form = form
return pctx
}
2023-07-24 17:42:18 +02:00
func (pctx *PreContext) Header(header any) *PreContext {
pctx.header = header
return pctx
}
2023-08-11 16:32:34 +02:00
func (pctx *PreContext) WithTimeout(to time.Duration) *PreContext {
pctx.timeout = &to
return pctx
}
2023-12-02 13:07:36 +01:00
func (pctx *PreContext) WithSession(sessionObj SessionObject) *PreContext {
2023-12-02 13:38:17 +01:00
pctx.persistantData.sessionObj = sessionObj
2023-12-02 13:07:36 +01:00
return pctx
}
2024-07-16 15:08:37 +02:00
func (pctx *PreContext) IgnoreWrongContentType() *PreContext {
pctx.ignoreWrongContentType = true
return pctx
}
2024-07-16 15:16:56 +02:00
func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) {
2023-07-18 15:23:32 +02:00
if pctx.uri != nil {
if err := pctx.ginCtx.ShouldBindUri(pctx.uri); err != nil {
2023-07-24 11:47:47 +02:00
err = exerr.Wrap(err, "Failed to read uri").
WithType(exerr.TypeBindFailURI).
Str("struct_type", fmt.Sprintf("%T", pctx.uri)).
Build()
2024-07-16 15:16:56 +02:00
return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "URI", err))
2023-07-18 15:23:32 +02:00
}
}
if pctx.query != nil {
if err := pctx.ginCtx.ShouldBindQuery(pctx.query); err != nil {
2023-07-24 11:47:47 +02:00
err = exerr.Wrap(err, "Failed to read query").
WithType(exerr.TypeBindFailQuery).
Str("struct_type", fmt.Sprintf("%T", pctx.query)).
Build()
2024-07-16 15:16:56 +02:00
return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "QUERY", err))
2023-07-18 15:23:32 +02:00
}
}
if pctx.body != nil {
if pctx.ginCtx.ContentType() == "application/json" {
if brc, ok := pctx.body.(dataext.BufferedReadCloser); ok {
// Ensures a fully reset (offset=0) buffer before parsing
err := brc.Reset()
if err != nil {
err = exerr.Wrap(err, "Failed to read (brc.reset) json-body").
WithType(exerr.TypeBindFailJSON).
Str("struct_type", fmt.Sprintf("%T", pctx.body)).
Build()
return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "JSON", err))
}
}
2023-07-18 15:23:32 +02:00
if err := pctx.ginCtx.ShouldBindJSON(pctx.body); err != nil {
2023-07-24 11:47:47 +02:00
err = exerr.Wrap(err, "Failed to read json-body").
WithType(exerr.TypeBindFailJSON).
Str("struct_type", fmt.Sprintf("%T", pctx.body)).
Build()
2024-07-16 15:22:18 +02:00
return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "JSON", err))
2023-07-18 15:23:32 +02:00
}
} else {
2024-07-16 15:08:37 +02:00
if !pctx.ignoreWrongContentType {
err := exerr.New(exerr.TypeBindFailJSON, "missing JSON body").
Str("struct_type", fmt.Sprintf("%T", pctx.body)).
Build()
2024-07-16 15:22:18 +02:00
return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "JSON", err))
2024-07-16 15:08:37 +02:00
}
2023-07-18 15:23:32 +02:00
}
}
2023-11-03 16:53:41 +01:00
if pctx.rawbody != nil {
if brc, ok := pctx.ginCtx.Request.Body.(dataext.BufferedReadCloser); ok {
v, err := brc.BufferedAll()
if err != nil {
2024-07-16 15:22:18 +02:00
return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "BODY", err))
2023-11-03 16:53:41 +01:00
}
*pctx.rawbody = v
} else {
buf := &bytes.Buffer{}
_, err := io.Copy(buf, pctx.ginCtx.Request.Body)
if err != nil {
2024-07-16 15:22:18 +02:00
return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "BODY", err))
2023-11-03 16:53:41 +01:00
}
*pctx.rawbody = buf.Bytes()
}
}
2023-07-18 15:23:32 +02:00
if pctx.form != nil {
if pctx.ginCtx.ContentType() == "multipart/form-data" {
if err := pctx.ginCtx.ShouldBindWith(pctx.form, binding.Form); err != nil {
2023-07-24 11:47:47 +02:00
err = exerr.Wrap(err, "Failed to read multipart-form").
WithType(exerr.TypeBindFailFormData).
Str("struct_type", fmt.Sprintf("%T", pctx.form)).
Build()
2024-07-16 15:16:56 +02:00
return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "FORM", err))
}
} else if pctx.ginCtx.ContentType() == "application/x-www-form-urlencoded" {
if err := pctx.ginCtx.ShouldBindWith(pctx.form, binding.Form); err != nil {
err = exerr.Wrap(err, "Failed to read urlencoded-form").
WithType(exerr.TypeBindFailFormData).
Str("struct_type", fmt.Sprintf("%T", pctx.form)).
2023-07-24 11:47:47 +02:00
Build()
2024-07-16 15:16:56 +02:00
return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "FORM", err))
2023-07-18 15:23:32 +02:00
}
} else {
2024-07-16 15:08:37 +02:00
if !pctx.ignoreWrongContentType {
err := exerr.New(exerr.TypeBindFailFormData, "missing form body").
Str("struct_type", fmt.Sprintf("%T", pctx.form)).
Build()
2024-07-16 15:16:56 +02:00
return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "FORM", err))
2024-07-16 15:08:37 +02:00
}
2023-07-18 15:23:32 +02:00
}
}
2023-07-24 17:42:18 +02:00
if pctx.header != nil {
2023-08-28 10:44:38 +02:00
if err := pctx.ginCtx.ShouldBindHeader(pctx.header); err != nil {
2023-07-24 17:42:18 +02:00
err = exerr.Wrap(err, "Failed to read header").
WithType(exerr.TypeBindFailHeader).
Str("struct_type", fmt.Sprintf("%T", pctx.query)).
Build()
2024-07-16 15:16:56 +02:00
return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "HEADER", err))
2023-07-24 17:42:18 +02:00
}
}
2023-08-11 16:32:34 +02:00
ictx, cancel := context.WithTimeout(context.Background(), langext.Coalesce(pctx.timeout, pctx.wrapper.requestTimeout))
2023-12-02 13:07:36 +01:00
2024-01-16 15:04:10 +01:00
actx := CreateAppContext(pctx.ginCtx, ictx, cancel)
2023-12-02 13:07:36 +01:00
if pctx.persistantData.sessionObj != nil {
2024-01-16 15:04:10 +01:00
err := pctx.persistantData.sessionObj.Init(pctx.ginCtx, actx)
2023-12-02 13:07:36 +01:00
if err != nil {
2024-01-16 15:04:10 +01:00
actx.Cancel()
2024-07-16 15:16:56 +02:00
return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "INIT", err))
2023-12-02 13:07:36 +01:00
}
}
2023-07-18 15:23:32 +02:00
return actx, pctx.ginCtx, nil
}
func callPanicSafe(fn WHandlerFunc, pctx PreContext) (res HTTPResponse, stackTrace string, panicObj any) {
defer func() {
if rec := recover(); rec != nil {
res = nil
stackTrace = string(debug.Stack())
panicObj = rec
}
}()
res = fn(pctx)
return res, "", nil
}