ctx

package
v0.0.0-...-c2ffd79 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BindJSONOptions

type BindJSONOptions struct {
	// WeaklyTypedInput allows common type coercions, e.g., "10" -> 10 for int fields.
	WeaklyTypedInput bool
	// ErrorUnused when true returns an error for unexpected fields.
	ErrorUnused bool
}

BindJSONOptions customizes how BindJSON decodes JSON payloads when binding into structs. All fields are optional and default to false (strict behavior).

type Ctx

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

Ctx is the request context for goflash handlers and middleware. It wraps the http.ResponseWriter and *http.Request, exposes convenience helpers, and tracks route, status, and response state for each request.

func (*Ctx) BindJSON

func (c *Ctx) BindJSON(v any, opts ...BindJSONOptions) error

BindJSON decodes the request body JSON into v. If v is a pointer to a struct, behavior can be customized using an optional BindJSONOptions parameter. Defaults: strict decoding using encoding/json with DisallowUnknownFields, no type coercion.

func (*Ctx) Context

func (c *Ctx) Context() context.Context

Context returns the request context.Context.

func (*Ctx) Finish

func (c *Ctx) Finish()

Finish is a hook for context cleanup after request handling. No-op by default.

func (*Ctx) Get

func (c *Ctx) Get(key any, def ...any) any

Get returns a value from the request context by key. If the key is not present (or the stored value is nil), it returns the provided default when given (Get(key, def)), otherwise it returns nil.

func (*Ctx) Header

func (c *Ctx) Header(key, value string)

Header sets a header on the response.

func (*Ctx) JSON

func (c *Ctx) JSON(v any) error

JSON writes the provided value as JSON with status code (defaults to 200 if not set).

func (*Ctx) Method

func (c *Ctx) Method() string

Method returns the HTTP method for the request.

func (*Ctx) Param

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

Param returns a path parameter by name. Returns "" if not found. Note: httprouter.Params.ByName returns "" if not found, so this avoids extra allocation.

func (*Ctx) Path

func (c *Ctx) Path() string

Path returns the request URL path.

func (*Ctx) Query

func (c *Ctx) Query(key string) string

Query returns a query string parameter by key. Returns "" if not found. Note: url.Values.Get returns "" if not found, so this avoids extra allocation.

func (*Ctx) Request

func (c *Ctx) Request() *http.Request

Request returns the underlying *http.Request.

func (*Ctx) Reset

func (c *Ctx) Reset(w http.ResponseWriter, r *http.Request, ps httprouter.Params, route string)

Reset prepares the context for a new request. Used internally by the framework.

func (*Ctx) ResponseWriter

func (c *Ctx) ResponseWriter() http.ResponseWriter

ResponseWriter returns the underlying http.ResponseWriter.

func (*Ctx) Route

func (c *Ctx) Route() string

Route returns the route pattern for the current request.

func (*Ctx) Send

func (c *Ctx) Send(status int, contentType string, b []byte) (int, error)

Send writes raw bytes with the given status and content type.

func (*Ctx) Set

func (c *Ctx) Set(key, value any) *Ctx

Set stores a value in the request context using the provided key. It replaces the request with a clone that carries the new context.

Note: Prefer using a custom, unexported key type to avoid collisions.

func (*Ctx) SetJSONEscapeHTML

func (c *Ctx) SetJSONEscapeHTML(escape bool)

SetJSONEscapeHTML controls whether JSON responses escape HTML characters (default true).

func (*Ctx) SetRequest

func (c *Ctx) SetRequest(r *http.Request)

SetRequest replaces the underlying *http.Request.

func (*Ctx) SetResponseWriter

func (c *Ctx) SetResponseWriter(w http.ResponseWriter)

SetResponseWriter replaces the underlying http.ResponseWriter.

func (*Ctx) Status

func (c *Ctx) Status(code int) *Ctx

Status sets the response status code (without writing the header yet). Returns the context for chaining.

func (*Ctx) StatusCode

func (c *Ctx) StatusCode() int

StatusCode returns the status code that will be written (or 200 if not set yet).

func (*Ctx) String

func (c *Ctx) String(status int, body string) error

String writes a plain text response with the given status and body.

func (*Ctx) WroteHeader

func (c *Ctx) WroteHeader() bool

WroteHeader reports whether the response header has been written.

Jump to

Keyboard shortcuts

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