ctx

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2025 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CookieOptions

type CookieOptions struct {
	Path     string
	Domain   string
	MaxAge   int
	Secure   bool
	HttpOnly bool
	SameSite http.SameSite
	Expires  time.Time
}

CookieOptions represents the options for setting a cookie.

type Cookies

type Cookies struct {
	Request  *http.Request
	Response http.ResponseWriter
}

Cookies abstraction

func (*Cookies) GetCookie

func (co *Cookies) GetCookie(name string) (string, error)

GetCookie retrieves the value of a specific cookie.

func (*Cookies) SetCookie

func (co *Cookies) SetCookie(name, value string, options *CookieOptions)

SetCookie sets an HTTP cookie in the response using flexible options.

type Request

type Request[Req any] struct {
	Request  *http.Request
	Response http.ResponseWriter
	Cookies  Cookies
	Body     Req
}

Request is a generic context struct that holds the HTTP request and response, along with a body of type Req. It is used to pass around the HTTP request and response objects, as well as any additional data needed for processing the request.

Type Parameters:

Req - The type of the body data.

Fields:

Request  - The HTTP request object.
Response - The HTTP response writer.
Body     - The body data of type Req.

func (*Request[Req]) GetFile

func (c *Request[Req]) GetFile(fieldName string) (multipart.File, *multipart.FileHeader, error)

GetFile retrieves a file from a multipart form request.

func (*Request[Req]) GetForm

func (c *Request[Req]) GetForm(formName string) string

GetForm retrieves a form via their name

func (*Request[Req]) GetHeader

func (c *Request[Req]) GetHeader(header string) string

GetHeader retrieves the value of a specific HTTP header.

func (*Request[Req]) GetIP

func (c *Request[Req]) GetIP() string

GetIP retrieves the client's IP address from the request.

func (*Request[Req]) GetPathParam

func (c *Request[Req]) GetPathParam(pathParam string) string

GetPathParam retrieves a placeholder value from the URL path.

func (*Request[Req]) GetPathParamDefault

func (c *Request[Req]) GetPathParamDefault(pathParam, defaultValue string) string

GetPathParamDefault retrieves a path parameter value or returns a default value if not found.

func (*Request[Req]) GetQueryParam

func (c *Request[Req]) GetQueryParam(queryParam string) string

GetQueryParam retrieves the value of a specific query parameter.

func (*Request[Req]) GetQueryParamDefault

func (c *Request[Req]) GetQueryParamDefault(queryParam, defaultValue string) string

GetQueryParamDefault retrieves a query parameter value or returns a default value if not found.

func (*Request[Req]) Json

func (c *Request[Req]) Json(statusCode int, data any) error

Json writes a JSON response to the client with the specified status code.

func (*Request[Req]) Redirect

func (c *Request[Req]) Redirect(statusCode int, url string)

Redirect redirects the client to a given URL

func (*Request[Req]) Text

func (c *Request[Req]) Text(statusCode int, message string)

Text writes a plain text response to the client with the specified status code.

type Response

type Response[Res any] struct {
	Response   Res
	StatusCode int
	Error      error
}

Response is a generic struct that encapsulates a response of type Res. It includes the actual response data, an HTTP status code, and an error if one occurred.

Jump to

Keyboard shortcuts

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