2023-07-18 15:23:32 +02:00
|
|
|
package ginext
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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-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"
|
|
|
|
"runtime/debug"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PreContext struct {
|
|
|
|
ginCtx *gin.Context
|
|
|
|
wrapper *GinWrapper
|
|
|
|
uri any
|
|
|
|
query any
|
|
|
|
body any
|
|
|
|
form any
|
2023-07-24 17:42:18 +02:00
|
|
|
header any
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-18 15:23:32 +02:00
|
|
|
func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) {
|
|
|
|
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()
|
2023-07-25 10:56:03 +02:00
|
|
|
return nil, nil, langext.Ptr(Error(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()
|
2023-07-25 10:56:03 +02:00
|
|
|
return nil, nil, langext.Ptr(Error(err))
|
2023-07-18 15:23:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if pctx.body != nil {
|
|
|
|
if pctx.ginCtx.ContentType() == "application/json" {
|
|
|
|
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()
|
2023-07-25 10:56:03 +02:00
|
|
|
return nil, nil, langext.Ptr(Error(err))
|
2023-07-18 15:23:32 +02:00
|
|
|
}
|
|
|
|
} else {
|
2023-07-24 11:47:47 +02:00
|
|
|
err := exerr.New(exerr.TypeBindFailJSON, "missing JSON body").
|
|
|
|
Str("struct_type", fmt.Sprintf("%T", pctx.body)).
|
|
|
|
Build()
|
2023-07-25 10:56:03 +02:00
|
|
|
return nil, nil, langext.Ptr(Error(err))
|
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()
|
2023-07-25 10:56:03 +02:00
|
|
|
return nil, nil, langext.Ptr(Error(err))
|
2023-07-18 15:23:32 +02:00
|
|
|
}
|
|
|
|
} else {
|
2023-07-24 11:47:47 +02:00
|
|
|
err := exerr.New(exerr.TypeBindFailFormData, "missing form body").
|
|
|
|
Str("struct_type", fmt.Sprintf("%T", pctx.form)).
|
|
|
|
Build()
|
2023-07-25 10:56:03 +02:00
|
|
|
return nil, nil, langext.Ptr(Error(err))
|
2023-07-18 15:23:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-24 17:42:18 +02:00
|
|
|
if pctx.header != nil {
|
|
|
|
if err := pctx.ginCtx.ShouldBindHeader(pctx.query); err != nil {
|
|
|
|
err = exerr.Wrap(err, "Failed to read header").
|
|
|
|
WithType(exerr.TypeBindFailHeader).
|
|
|
|
Str("struct_type", fmt.Sprintf("%T", pctx.query)).
|
|
|
|
Build()
|
2023-07-25 10:56:03 +02:00
|
|
|
return nil, nil, langext.Ptr(Error(err))
|
2023-07-24 17:42:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-18 15:23:32 +02:00
|
|
|
ictx, cancel := context.WithTimeout(context.Background(), pctx.wrapper.requestTimeout)
|
|
|
|
actx := CreateAppContext(pctx.ginCtx, ictx, cancel)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|