make the send helpers methods of the ctx to reduce verbosity

This commit is contained in:
Sky Johnson 2025-08-16 11:10:28 -05:00
parent e100f2d56b
commit 5370d14152
6 changed files with 81 additions and 81 deletions

View File

@ -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: "",

4
fs.go
View File

@ -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)
}
}

View File

@ -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

72
send.go
View File

@ -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)
}

View File

@ -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)
}
}

View File

@ -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)
}