diff --git a/context_response.go b/context_response.go index d720e9a..0101545 100644 --- a/context_response.go +++ b/context_response.go @@ -56,6 +56,14 @@ func (r *ContextResponse) Json(code int, obj any) contractshttp.Response { return &JsonResponse{code, obj, r.instance} } +func (r *ContextResponse) NoContent(code ...int) contractshttp.Response { + if len(code) == 0 { + code = append(code, http.StatusNoContent) + } + + return &NoContentResponse{code[0], r.instance} +} + func (r *ContextResponse) Origin() contractshttp.ResponseOrigin { return r.origin } diff --git a/context_response_test.go b/context_response_test.go index 522b9ab..4f03d6a 100644 --- a/context_response_test.go +++ b/context_response_test.go @@ -222,6 +222,42 @@ func TestResponse(t *testing.T) { expectBody: "Goravel", expectHeader: "goravel", }, + { + name: "NoContent", + method: "GET", + url: "/no/content", + setup: func(method, url string) error { + fiber.Get("/no/content", func(ctx contractshttp.Context) contractshttp.Response { + return ctx.Response().NoContent() + }) + + var err error + req, err = http.NewRequest(method, url, nil) + if err != nil { + return err + } + return nil + }, + expectCode: http.StatusNoContent, + }, + { + name: "NoContentWithCode", + method: "GET", + url: "/no/content/with/code", + setup: func(method, url string) error { + fiber.Get("/no/content/with/code", func(ctx contractshttp.Context) contractshttp.Response { + return ctx.Response().NoContent(http.StatusCreated) + }) + + var err error + req, err = http.NewRequest(method, url, nil) + if err != nil { + return err + } + return nil + }, + expectCode: http.StatusCreated, + }, { name: "Origin", method: "GET", diff --git a/go.mod b/go.mod index da84aac..c06d938 100644 --- a/go.mod +++ b/go.mod @@ -5,9 +5,8 @@ go 1.21 require ( github.com/gofiber/fiber/v2 v2.52.4 github.com/gofiber/template/html/v2 v2.1.1 - github.com/gookit/color v1.5.4 github.com/gookit/validate v1.5.2 - github.com/goravel/framework v1.13.1-0.20240602025035-84b54a821232 + github.com/goravel/framework v1.13.1-0.20240604080703-58fde234c3ef github.com/savioxavier/termlink v1.3.0 github.com/spf13/cast v1.6.0 github.com/stretchr/testify v1.9.0 @@ -80,6 +79,7 @@ require ( github.com/google/wire v0.6.0 // indirect github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect github.com/googleapis/gax-go/v2 v2.12.2 // indirect + github.com/gookit/color v1.5.4 // indirect github.com/gookit/filter v1.2.1 // indirect github.com/gookit/goutil v0.6.15 // indirect github.com/goravel/file-rotatelogs/v2 v2.4.2 // indirect diff --git a/go.sum b/go.sum index 618ec16..b205119 100644 --- a/go.sum +++ b/go.sum @@ -386,8 +386,8 @@ github.com/gookit/validate v1.5.2 h1:i5I2OQ7WYHFRPRATGu9QarR9snnNHydvwSuHXaRWAV0 github.com/gookit/validate v1.5.2/go.mod h1:yuPy2WwDlwGRa06fFJ5XIO8QEwhRnTC2LmxmBa5SE14= github.com/goravel/file-rotatelogs/v2 v2.4.2 h1:g68AzbePXcm0V2CpUMc9j4qVzcDn7+7aoWSjZ51C0m4= github.com/goravel/file-rotatelogs/v2 v2.4.2/go.mod h1:23VuSW8cBS4ax5cmbV+5AaiLpq25b8UJ96IhbAkdo8I= -github.com/goravel/framework v1.13.1-0.20240602025035-84b54a821232 h1:bXgugUzsNE+mrX2xhdlPngCUxMlrmKQR+UWbpdhNgqA= -github.com/goravel/framework v1.13.1-0.20240602025035-84b54a821232/go.mod h1:pDVx2pvJ71YgiMnWWXeOFZzErlSsxkc9Sf4ywmhZBpQ= +github.com/goravel/framework v1.13.1-0.20240604080703-58fde234c3ef h1:FaTDqgX2ZvquEVWdv1Rm4PtUe8KINyD60AxNuS27VQI= +github.com/goravel/framework v1.13.1-0.20240604080703-58fde234c3ef/go.mod h1:pDVx2pvJ71YgiMnWWXeOFZzErlSsxkc9Sf4ywmhZBpQ= github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4= github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM= github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI= diff --git a/response.go b/response.go index 08110c8..a29a986 100644 --- a/response.go +++ b/response.go @@ -52,6 +52,15 @@ func (r *JsonResponse) Render() error { return r.instance.Status(r.code).JSON(r.obj) } +type NoContentResponse struct { + code int + instance *fiber.Ctx +} + +func (r *NoContentResponse) Render() error { + return r.instance.Status(r.code).Send(nil) +} + type RedirectResponse struct { code int location string diff --git a/route.go b/route.go index d75a78f..4353b9c 100644 --- a/route.go +++ b/route.go @@ -12,11 +12,11 @@ import ( "github.com/gofiber/fiber/v2/middleware/logger" "github.com/gofiber/fiber/v2/middleware/recover" "github.com/gofiber/template/html/v2" - "github.com/gookit/color" "github.com/goravel/framework/contracts/config" httpcontract "github.com/goravel/framework/contracts/http" "github.com/goravel/framework/contracts/route" "github.com/goravel/framework/support" + "github.com/goravel/framework/support/color" "github.com/goravel/framework/support/file" "github.com/goravel/framework/support/json" "github.com/savioxavier/termlink" @@ -144,7 +144,7 @@ func (r *Route) Run(host ...string) error { } r.outputRoutes() - color.Greenln(termlink.Link("[HTTP] Listening and serving HTTP on", host[0])) + color.Green().Println(termlink.Link("[HTTP] Listening and serving HTTP on", host[0])) return r.instance.Listen(host[0]) } @@ -179,7 +179,7 @@ func (r *Route) RunTLSWithCert(host, certFile, keyFile string) error { } r.outputRoutes() - color.Greenln(termlink.Link("[HTTPS] Listening and serving HTTPS on", host)) + color.Green().Println(termlink.Link("[HTTPS] Listening and serving HTTPS on", host)) return r.instance.ListenTLS(host, certFile, keyFile) }