From 7b80f2f801afd3a3992554c725c8c856dca52d1f Mon Sep 17 00:00:00 2001 From: Ehsan Noureddin Moosa Date: Tue, 9 Apr 2024 10:03:27 +0330 Subject: [PATCH] fix panic in multipart data --- example/ex-02-rest/api/service.go | 20 ++++++++++++++++++++ kit/message.go | 4 ++++ 2 files changed, 24 insertions(+) diff --git a/example/ex-02-rest/api/service.go b/example/ex-02-rest/api/service.go index e7b3d54e..eb3b26d5 100644 --- a/example/ex-02-rest/api/service.go +++ b/example/ex-02-rest/api/service.go @@ -56,6 +56,13 @@ var SampleDesc desc.ServiceDescFunc = func() *desc.Service { Selector(fasthttp.REST(http.MethodPost, "/raw_echo")). Selector(fasthttp.RPC("rawEcho")). SetHandler(RawEchoHandler), + ). + AddContract( + desc.NewContract(). + SetInput(kit.MultipartFormMessage{}). + SetOutput(kit.RawMessage{}). + Selector(fasthttp.REST(http.MethodPost, "/upload")). + SetHandler(UploadHandler), ) } @@ -132,3 +139,16 @@ func Redirect(ctx *kit.Context) { rc := ctx.Conn().(kit.RESTConn) //nolint:forcetypeassert rc.Redirect(http.StatusTemporaryRedirect, req.URL) } + +func UploadHandler(ctx *kit.Context) { + //nolint:forcetypeassert + req := ctx.In().GetMsg().(kit.MultipartFormMessage) + + frm := req.GetForm() + fmt.Println(frm.File) + + ctx.In().Reply(). + SetHdr("Content-Type", "application/json"). + SetMsg(kit.RawMessage{}). + Send() +} diff --git a/kit/message.go b/kit/message.go index 2098f85d..6fff8943 100644 --- a/kit/message.go +++ b/kit/message.go @@ -50,6 +50,10 @@ func CreateMessageFactory(in Message) MessageFactoryFunc { return func() Message { return RawMessage{} } + case reflect.Indirect(reflect.ValueOf(in)).Type() == reflect.TypeOf(MultipartFormMessage{}): + return func() Message { + return MultipartFormMessage{} + } } var ff MessageFactoryFunc