From 5370d14152b7aaa380e3fd4e95d71a7b23448d85 Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Sat, 16 Aug 2025 11:10:28 -0500 Subject: [PATCH] make the send helpers methods of the ctx to reduce verbosity --- cookies.go | 6 ++--- fs.go | 4 +-- router.go | 2 +- send.go | 72 --------------------------------------------------- sushi.go | 2 +- types.go | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 6 files changed, 81 insertions(+), 81 deletions(-) delete mode 100644 send.go diff --git a/cookies.go b/cookies.go index 1b23964..463424c 100644 --- a/cookies.go +++ b/cookies.go @@ -18,7 +18,7 @@ type CookieOptions struct { SameSite string } -func SetSecureCookie(ctx *fasthttp.RequestCtx, opts CookieOptions) { +func SetSecureCookie(ctx Ctx, opts CookieOptions) { cookie := &fasthttp.Cookie{} cookie.SetKey(opts.Name) @@ -59,11 +59,11 @@ func SetSecureCookie(ctx *fasthttp.RequestCtx, opts CookieOptions) { ctx.Response.Header.SetCookie(cookie) } -func GetCookie(ctx *fasthttp.RequestCtx, name string) string { +func GetCookie(ctx Ctx, name string) string { return string(ctx.Request.Header.Cookie(name)) } -func DeleteCookie(ctx *fasthttp.RequestCtx, name string) { +func DeleteCookie(ctx Ctx, name string) { SetSecureCookie(ctx, CookieOptions{ Name: name, Value: "", diff --git a/fs.go b/fs.go index 776b98d..e44c520 100644 --- a/fs.go +++ b/fs.go @@ -51,7 +51,7 @@ func StaticFS(fsOptions StaticOptions) Handler { fsHandler := fs.NewRequestHandler() return func(ctx Ctx, params []any) { - fsHandler(ctx) + fsHandler(ctx.RequestCtx) } } @@ -63,7 +63,7 @@ func Static(root string) Handler { // StaticFile serves a single file func StaticFile(filePath string) Handler { return func(ctx Ctx, params []any) { - fasthttp.ServeFile(ctx, filePath) + fasthttp.ServeFile(ctx.RequestCtx, filePath) } } diff --git a/router.go b/router.go index ea68302..5e86dfc 100644 --- a/router.go +++ b/router.go @@ -55,7 +55,7 @@ func (r *Router) ServeHTTP(ctx *fasthttp.RequestCtx) { return } - h(ctx, params) + h(Ctx{ctx}, params) } // Handler returns a fasthttp request handler diff --git a/send.go b/send.go deleted file mode 100644 index 5c08c2f..0000000 --- a/send.go +++ /dev/null @@ -1,72 +0,0 @@ -package sushi - -import ( - "encoding/json" - - "github.com/valyala/fasthttp" -) - -// SendHTML sends an HTML response -func SendHTML(ctx Ctx, html string) { - ctx.SetContentType("text/html; charset=utf-8") - ctx.SetBodyString(html) -} - -// SendText sends a plain text response -func SendText(ctx Ctx, text string) { - ctx.SetContentType("text/plain; charset=utf-8") - ctx.SetBodyString(text) -} - -// SendJSON sends a JSON response -func SendJSON(ctx Ctx, data any) error { - jsonData, err := json.Marshal(data) - if err != nil { - ctx.SetStatusCode(fasthttp.StatusInternalServerError) - ctx.SetBodyString("Internal Server Error") - return err - } - - ctx.SetContentType("application/json") - ctx.SetBody(jsonData) - return nil -} - -// SendStatus sends only a status code -func SendStatus(ctx Ctx, statusCode int) { - ctx.SetStatusCode(statusCode) -} - -// SendError sends an error response with status code -func SendError(ctx Ctx, statusCode int, message string) { - ctx.SetStatusCode(statusCode) - ctx.SetContentType("text/plain; charset=utf-8") - ctx.SetBodyString(message) -} - -// SendRedirect sends a redirect response -func SendRedirect(ctx Ctx, url string, statusCode ...int) { - code := fasthttp.StatusFound - if len(statusCode) > 0 { - code = statusCode[0] - } - ctx.Redirect(url, code) -} - -// SendFile serves a file -func SendFile(ctx Ctx, filePath string) { - fasthttp.ServeFile(ctx, filePath) -} - -// SendBytes sends raw bytes with optional content type -func SendBytes(ctx Ctx, data []byte, contentType ...string) { - if len(contentType) > 0 { - ctx.SetContentType(contentType[0]) - } - ctx.SetBody(data) -} - -// SendNoContent sends a 204 No Content response -func SendNoContent(ctx Ctx) { - ctx.SetStatusCode(fasthttp.StatusNoContent) -} diff --git a/sushi.go b/sushi.go index 64614f3..8094167 100644 --- a/sushi.go +++ b/sushi.go @@ -21,7 +21,7 @@ func IsHTTPS(ctx Ctx) bool { // StandardHandler adapts a standard fasthttp.RequestHandler to the router's Handler func StandardHandler(handler fasthttp.RequestHandler) Handler { return func(ctx Ctx, _ []any) { - handler(ctx) + handler(ctx.RequestCtx) } } diff --git a/types.go b/types.go index cd08b4a..cae1e0b 100644 --- a/types.go +++ b/types.go @@ -1,7 +1,79 @@ package sushi -import "github.com/valyala/fasthttp" +import ( + "encoding/json" + + "github.com/valyala/fasthttp" +) + +type Ctx struct { + *fasthttp.RequestCtx +} -type Ctx = *fasthttp.RequestCtx type Handler func(ctx Ctx, params []any) type Middleware func(ctx Ctx, params []any, next func()) + +// SendHTML sends an HTML response +func (ctx Ctx) SendHTML(html string) { + ctx.SetContentType("text/html; charset=utf-8") + ctx.SetBodyString(html) +} + +// SendText sends a plain text response +func (ctx Ctx) SendText(text string) { + ctx.SetContentType("text/plain; charset=utf-8") + ctx.SetBodyString(text) +} + +// SendJSON sends a JSON response +func (ctx Ctx) SendJSON(data any) error { + jsonData, err := json.Marshal(data) + if err != nil { + ctx.SetStatusCode(fasthttp.StatusInternalServerError) + ctx.SetBodyString("Internal Server Error") + return err + } + + ctx.SetContentType("application/json") + ctx.SetBody(jsonData) + return nil +} + +// SendStatus sends only a status code +func (ctx Ctx) SendStatus(statusCode int) { + ctx.SetStatusCode(statusCode) +} + +// SendError sends an error response with status code +func (ctx Ctx) SendError(statusCode int, message string) { + ctx.SetStatusCode(statusCode) + ctx.SetContentType("text/plain; charset=utf-8") + ctx.SetBodyString(message) +} + +// Redirect sends a redirect response +func (ctx Ctx) Redirect(url string, statusCode ...int) { + code := fasthttp.StatusFound + if len(statusCode) > 0 { + code = statusCode[0] + } + ctx.Redirect(url, code) +} + +// SendFile serves a file +func (ctx Ctx) SendFile(filePath string) { + fasthttp.ServeFile(ctx.RequestCtx, filePath) +} + +// SendBytes sends raw bytes with optional content type +func (ctx Ctx) SendBytes(data []byte, contentType ...string) { + if len(contentType) > 0 { + ctx.SetContentType(contentType[0]) + } + ctx.SetBody(data) +} + +// SendNoContent sends a 204 No Content response +func (ctx Ctx) SendNoContent() { + ctx.SetStatusCode(fasthttp.StatusNoContent) +}