forked from goffee/core
Public request in context
This commit is contained in:
parent
3d4b3651f2
commit
1cc668a0e1
5 changed files with 26 additions and 24 deletions
32
context.go
32
context.go
|
|
@ -56,8 +56,8 @@ func (c *Context) Next() {
|
||||||
func (c *Context) prepare(ctx *Context) {
|
func (c *Context) prepare(ctx *Context) {
|
||||||
// Only parse multipart form if it hasn't been parsed already
|
// Only parse multipart form if it hasn't been parsed already
|
||||||
// This prevents race conditions when multiple goroutines might access the same request
|
// This prevents race conditions when multiple goroutines might access the same request
|
||||||
if ctx.Request.httpRequest.MultipartForm == nil {
|
if ctx.Request.HttpRequest.MultipartForm == nil {
|
||||||
ctx.Request.httpRequest.ParseMultipartForm(int64(app.Config.Request.MaxUploadFileSize))
|
ctx.Request.HttpRequest.ParseMultipartForm(int64(app.Config.Request.MaxUploadFileSize))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -66,38 +66,40 @@ func (c *Context) GetPathParam(key string) interface{} {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) GetRequestParam(key string) interface{} {
|
func (c *Context) GetRequestParam(key string) interface{} {
|
||||||
return c.Request.httpRequest.FormValue(key)
|
return c.Request.HttpRequest.FormValue(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) RequestParamExists(key string) bool {
|
func (c *Context) RequestParamExists(key string) bool {
|
||||||
return c.Request.httpRequest.Form.Has(key)
|
return c.Request.HttpRequest.Form.Has(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) GetRequesForm(key string) interface{} {
|
func (c *Context) GetRequesForm(key string) interface{} {
|
||||||
c.Request.httpRequest.ParseForm()
|
c.Request.HttpRequest.ParseForm()
|
||||||
return c.Request.httpRequest.Form[key]
|
return c.Request.HttpRequest.Form[key]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) GetRequesBodyMap() map[string]interface{} {
|
func (c *Context) GetRequesBodyMap() map[string]interface{} {
|
||||||
var dat map[string]any
|
var dat map[string]any
|
||||||
body := c.Request.httpRequest.Body
|
body := c.Request.HttpRequest.Body
|
||||||
|
|
||||||
if body != nil {
|
if body != nil {
|
||||||
if content, err := io.ReadAll(body); err == nil {
|
if content, err := io.ReadAll(body); err == nil {
|
||||||
json.Unmarshal(content, &dat)
|
json.Unmarshal(content, &dat)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
defer body.Close()
|
||||||
return dat
|
return dat
|
||||||
}
|
}
|
||||||
|
|
||||||
// get raw data for file binary
|
// get raw data for file binary
|
||||||
func (c *Context) GetRawBody() ([]byte, error) {
|
func (c *Context) GetRawBody() ([]byte, error) {
|
||||||
if c.Request.httpRequest.Body == nil {
|
if c.Request.HttpRequest.Body == nil {
|
||||||
return nil, errors.New("empty body")
|
return nil, errors.New("empty body")
|
||||||
}
|
}
|
||||||
|
|
||||||
defer c.Request.httpRequest.Body.Close()
|
defer c.Request.HttpRequest.Body.Close()
|
||||||
|
|
||||||
data, err := io.ReadAll(c.Request.httpRequest.Body)
|
data, err := io.ReadAll(c.Request.HttpRequest.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -107,7 +109,7 @@ func (c *Context) GetRawBody() ([]byte, error) {
|
||||||
|
|
||||||
// get json body and bind to dest interface
|
// get json body and bind to dest interface
|
||||||
func (c *Context) GetRequesBodyStruct(dest interface{}) error {
|
func (c *Context) GetRequesBodyStruct(dest interface{}) error {
|
||||||
body := c.Request.httpRequest.Body
|
body := c.Request.HttpRequest.Body
|
||||||
if body != nil {
|
if body != nil {
|
||||||
value := reflect.ValueOf(dest)
|
value := reflect.ValueOf(dest)
|
||||||
if value.Kind() != reflect.Ptr {
|
if value.Kind() != reflect.Ptr {
|
||||||
|
|
@ -121,12 +123,12 @@ func (c *Context) GetRequesBodyStruct(dest interface{}) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) GetHeader(key string) string {
|
func (c *Context) GetHeader(key string) string {
|
||||||
return c.Request.httpRequest.Header.Get(key)
|
return c.Request.HttpRequest.Header.Get(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) GetCookie() (UserCookie, error) {
|
func (c *Context) GetCookie() (UserCookie, error) {
|
||||||
|
|
||||||
user, err := GetCookie(c.Request.httpRequest)
|
user, err := GetCookie(c.Request.HttpRequest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return user, err
|
return user, err
|
||||||
}
|
}
|
||||||
|
|
@ -142,7 +144,7 @@ func (c *Context) GetQueueClient() *asynq.Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) GetUploadedFile(name string) (*UploadedFileInfo, error) {
|
func (c *Context) GetUploadedFile(name string) (*UploadedFileInfo, error) {
|
||||||
file, fileHeader, err := c.Request.httpRequest.FormFile(name)
|
file, fileHeader, err := c.Request.HttpRequest.FormFile(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error retrieving file: %v", err)
|
return nil, fmt.Errorf("error retrieving file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -306,7 +308,7 @@ func (c *Context) CastToString(value interface{}) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Context) GetUserAgent() string {
|
func (c Context) GetUserAgent() string {
|
||||||
return c.Request.httpRequest.UserAgent()
|
return c.Request.HttpRequest.UserAgent()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) CastToInt(value interface{}) int {
|
func (c *Context) CastToInt(value interface{}) int {
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ func TestDebugAny(t *testing.T) {
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
c := &Context{
|
c := &Context{
|
||||||
Request: &Request{
|
Request: &Request{
|
||||||
httpRequest: r,
|
HttpRequest: r,
|
||||||
httpPathParams: nil,
|
httpPathParams: nil,
|
||||||
},
|
},
|
||||||
Response: &Response{
|
Response: &Response{
|
||||||
|
|
@ -538,7 +538,7 @@ func makeCTXLogTestCTX(t *testing.T, w http.ResponseWriter, r *http.Request, tmp
|
||||||
t.Helper()
|
t.Helper()
|
||||||
return &Context{
|
return &Context{
|
||||||
Request: &Request{
|
Request: &Request{
|
||||||
httpRequest: r,
|
HttpRequest: r,
|
||||||
httpPathParams: nil,
|
httpPathParams: nil,
|
||||||
},
|
},
|
||||||
Response: &Response{
|
Response: &Response{
|
||||||
|
|
|
||||||
4
core.go
4
core.go
|
|
@ -256,7 +256,7 @@ func (app *App) makeHTTPRouterHandlerFunc(h Controller, ms []Hook) httprouter.Ha
|
||||||
// Store chain state in request context
|
// Store chain state in request context
|
||||||
ctx := &Context{
|
ctx := &Context{
|
||||||
Request: &Request{
|
Request: &Request{
|
||||||
httpRequest: r.WithContext(context.WithValue(r.Context(), "goffeeChain", reqCtx)),
|
HttpRequest: r.WithContext(context.WithValue(r.Context(), "goffeeChain", reqCtx)),
|
||||||
httpPathParams: ps,
|
httpPathParams: ps,
|
||||||
},
|
},
|
||||||
Response: &Response{
|
Response: &Response{
|
||||||
|
|
@ -392,7 +392,7 @@ func UseHook(mw Hook) {
|
||||||
// Next advances to the next middleware or controller in the chain and invokes it with the given context if available.
|
// Next advances to the next middleware or controller in the chain and invokes it with the given context if available.
|
||||||
func (app *App) Next(c *Context) {
|
func (app *App) Next(c *Context) {
|
||||||
// Get request-specific chain state from context
|
// Get request-specific chain state from context
|
||||||
if reqCtx, ok := c.Request.httpRequest.Context().Value("goffeeChain").(*requestContext); ok {
|
if reqCtx, ok := c.Request.HttpRequest.Context().Value("goffeeChain").(*requestContext); ok {
|
||||||
reqCtx.chainIndex++
|
reqCtx.chainIndex++
|
||||||
if reqCtx.chainIndex < len(reqCtx.chainNodes) {
|
if reqCtx.chainIndex < len(reqCtx.chainNodes) {
|
||||||
n := reqCtx.chainNodes[reqCtx.chainIndex]
|
n := reqCtx.chainNodes[reqCtx.chainIndex]
|
||||||
|
|
|
||||||
|
|
@ -226,7 +226,7 @@ func makeCTX(t *testing.T) *Context {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
return &Context{
|
return &Context{
|
||||||
Request: &Request{
|
Request: &Request{
|
||||||
httpRequest: httptest.NewRequest(GET, LOCALHOST, nil),
|
HttpRequest: httptest.NewRequest(GET, LOCALHOST, nil),
|
||||||
httpPathParams: nil,
|
httpPathParams: nil,
|
||||||
},
|
},
|
||||||
Response: &Response{
|
Response: &Response{
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,6 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Request struct {
|
type Request struct {
|
||||||
httpRequest *http.Request
|
HttpRequest *http.Request
|
||||||
httpPathParams httprouter.Params
|
httpPathParams httprouter.Params
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue