context

package
v2.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context interface {
	Raw() *fasthttp.RequestCtx
	Method() string
	Scheme() string
	Host() string
	Path() string
	URI() string
	Header(name string) string
	Headers() map[string]string
	Param(name string) string
	Params() map[string]string
	Query(name string) string
	Queries() map[string]string
	Body() []byte
	BindJSON(out any) error
	Respond(statusCode int, code int, message string, data any) error
	RawResponse() ResponseBuilder
	StatusOK(message ...string) ResponseBuilder
	StatusParamError(message ...string) ResponseBuilder
	StatusServerError(message ...string) ResponseBuilder
	Container() inject.Injector
	SetContainer(inject.Injector)
	SetRouteParams(map[string]string)
	TraceID() string

	// Set stores a key-value pair in the request-scoped context.
	// Typically used by middleware to pass data (e.g. user info) to handlers or later middleware.
	Set(key string, value any)
	// Get retrieves a value by key from the request-scoped context.
	// Returns the value and whether the key was found.
	Get(key string) (any, bool)
	// MustGet retrieves a value by key, panics if the key does not exist.
	MustGet(key string) any
	// GetString is a convenience method that returns the value as a string.
	// Returns empty string if the key is missing or the value is not a string.
	GetString(key string) string
	// GetInt is a convenience method that returns the value as an int.
	// Returns 0 if the key is missing or the value is not an int.
	GetInt(key string) int

	// SSE starts a Server-Sent Events stream.
	// It sets the appropriate headers (Content-Type, Cache-Control, Connection) and
	// returns an SSEWriter that can be used to send events to the client.
	// The provided callback receives the writer; when the callback returns, the stream ends.
	SSE(fn func(w *SSEWriter)) error
}

type FWContext

type FWContext struct {
	// contains filtered or unexported fields
}

func New

func New(raw *fasthttp.RequestCtx, resp *response.Manager) *FWContext

func (*FWContext) BindJSON

func (c *FWContext) BindJSON(out any) error

func (*FWContext) Body

func (c *FWContext) Body() []byte

func (*FWContext) Container

func (c *FWContext) Container() inject.Injector

func (*FWContext) Get

func (c *FWContext) Get(key string) (any, bool)

func (*FWContext) GetInt

func (c *FWContext) GetInt(key string) int

func (*FWContext) GetString

func (c *FWContext) GetString(key string) string

func (*FWContext) Header

func (c *FWContext) Header(name string) string

func (*FWContext) Headers

func (c *FWContext) Headers() map[string]string

func (*FWContext) Host

func (c *FWContext) Host() string

func (*FWContext) Method

func (c *FWContext) Method() string

func (*FWContext) MustGet

func (c *FWContext) MustGet(key string) any

func (*FWContext) Param

func (c *FWContext) Param(name string) string

func (*FWContext) Params

func (c *FWContext) Params() map[string]string

func (*FWContext) Path

func (c *FWContext) Path() string

func (*FWContext) Queries

func (c *FWContext) Queries() map[string]string

func (*FWContext) Query

func (c *FWContext) Query(name string) string

func (*FWContext) Raw

func (c *FWContext) Raw() *fasthttp.RequestCtx

func (*FWContext) RawResponse

func (c *FWContext) RawResponse() ResponseBuilder

func (*FWContext) Respond

func (c *FWContext) Respond(statusCode int, code int, message string, data any) error

func (*FWContext) SSE

func (c *FWContext) SSE(fn func(w *SSEWriter)) error

SSE starts a Server-Sent Events stream. It sets appropriate headers and invokes fn with an SSEWriter. The stream is closed when fn returns.

Usage:

ctx.SSE(func(w *SSEWriter) {
    w.WriteText("hello")
    w.WriteJSON(map[string]any{"count": 1})
    w.WriteEvent("update", "payload data")
})

func (*FWContext) Scheme

func (c *FWContext) Scheme() string

func (*FWContext) Set

func (c *FWContext) Set(key string, value any)

func (*FWContext) SetContainer

func (c *FWContext) SetContainer(i inject.Injector)

func (*FWContext) SetRouteParams

func (c *FWContext) SetRouteParams(p map[string]string)

func (*FWContext) SetTraceIDFunc

func (c *FWContext) SetTraceIDFunc(fn func(*fasthttp.RequestCtx) string)

func (*FWContext) StatusOK

func (c *FWContext) StatusOK(message ...string) ResponseBuilder

func (*FWContext) StatusParamError

func (c *FWContext) StatusParamError(message ...string) ResponseBuilder

func (*FWContext) StatusServerError

func (c *FWContext) StatusServerError(message ...string) ResponseBuilder

func (*FWContext) TraceID

func (c *FWContext) TraceID() string

func (*FWContext) URI

func (c *FWContext) URI() string

type ResponseBuilder

type ResponseBuilder interface {
	Message(message string) ResponseBuilder
	Code(code int) ResponseBuilder
	Data(data any) error
	Send() error
}

type SSEWriter

type SSEWriter struct {
	// contains filtered or unexported fields
}

SSEWriter writes Server-Sent Events to an underlying buffered writer. Each method writes a complete SSE frame and flushes immediately.

func (*SSEWriter) WriteEvent

func (s *SSEWriter) WriteEvent(event string, data string) error

WriteEvent sends a named SSE event with text data.

event: <event>\ndata: <data>\n\n

func (*SSEWriter) WriteEventJSON

func (s *SSEWriter) WriteEventJSON(event string, v any) error

WriteEventJSON sends a named SSE event with JSON-encoded data.

event: <event>\ndata: <json>\n\n

func (*SSEWriter) WriteID

func (s *SSEWriter) WriteID(id string, event string, data string) error

WriteID sends an SSE event with id, event name, and text data.

id: <id>\nevent: <event>\ndata: <data>\n\n

func (*SSEWriter) WriteJSON

func (s *SSEWriter) WriteJSON(v any) error

WriteJSON sends a data-only SSE event with JSON-encoded content.

data: {"key":"value"}\n\n

func (*SSEWriter) WriteText

func (s *SSEWriter) WriteText(text string) error

WriteText sends a data-only SSE event with plain text content.

data: hello world\n\n

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL