context

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BadRequest400

func BadRequest400[T any](r *NexResponse, message string)

BadRequest400 - response with status 400 and data of any generic type The server could not understand the request due to invalid syntax. The client SHOULD NOT repeat the request without modifications.

func Conflict409

func Conflict409[T any](r *NexResponse, message string)

Conflict409 - response with status 409 and data of any generic type This response is sent when a request conflicts with the current state of the server.

func Forbidden403

func Forbidden403[T any](r *NexResponse, message string)

Forbidden403 - response with status 403 and data of any generic type The client does not have access rights to the content; that is, it is unauthorized, so the server is refusing to give the requested resource. Unlike 401, the client's identity is known to the server.

func InternalServerError500

func InternalServerError500[T any](r *NexResponse, message string)

InternalServerError500 - response with status 500 and data of any generic type The server has encountered a situation it doesn't know how to handle.

func MethodNotAllowed405

func MethodNotAllowed405[T any](r *NexResponse, message string)

MethodNotAllowed405 - response with status 405 and data of any generic type The request method is known by the server but has been disabled and cannot be used. For example, an API may forbid DELETE-ing a resource. The two mandatory methods, GET and HEAD, must never be disabled and should not return this error code.

func NoContent204

func NoContent204[T any](r *NexResponse, message string)

NoContent204 - response with status 204 and data of any generic type There is no content to send for this request, but the headers may be useful. The user-agent may update its cached headers for this resource with the new ones.

func NotFound404

func NotFound404[T any](r *NexResponse, message string)

NotFound404 - response with status 404 and data of any generic type The server can not find the requested resource. In the browser, this means the URL is not recognized. In an API, this can also mean that the endpoint is valid but the resource itself does not exist. Servers may also send this response instead of 403 to hide the existence of a resource from an unauthorized client. This response code is probably the most famous one due to its frequent occurrence on the web.

func Ok200

func Ok200[T any](r *NexResponse, data T, message string)

Ok200 - response with status 200 and data of any generic type The request succeeded. The result meaning of "success" depends on the HTTP method: GET: The resource has been fetched and transmitted in the message body. HEAD: The representation headers are included in the response without any message body. PUT or POST: The resource describing the result of the action is transmitted in the message body. TRACE: The message body contains the request message as received by the server.

func Ok201

func Ok201[T any](r *NexResponse, data T, message string)

Created201 - response with status 201 and data of any generic type The request succeeded, and a new resource was created as a result. This is typically the response sent after POST requests, or some PUT requests.

func Ok202

func Ok202[T any](r *NexResponse, data T, message string)

Accepted202 - response with status 202 and data of any generic type The request has been received but not yet acted upon. It is non-committal, meaning that there is no way in HTTP to later send an asynchronous response indicating the outcome of processing the request. It is intended for cases where another process or server handles the request, or for batch processing.

func Unauthorized401

func Unauthorized401[T any](r *NexResponse, message string)

Unauthorized401 - response with status 401 and data of any generic type Although the HTTP standard specifies "unauthorized", semantically this response means "unauthenticated". That is, the client must authenticate itself to get the requested response.

Types

type Body

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

func NewBody

func NewBody(ctx *Context) *Body

NewBody creates a new instance of Body.

func (*Body) ParseForm

func (b *Body) ParseForm() (values url.Values, err error)

ParseForm parses the request body as form data.

func (*Body) ParseJSON

func (b *Body) ParseJSON(v interface{}) error

ParseJSON parses the request body as JSON into the provided struct.

func (*Body) ParseXML

func (b *Body) ParseXML(v interface{}) error

ParseXML parses the request body as XML into the provided struct.

func (*Body) ReadRaw

func (b *Body) ReadRaw() ([]byte, error)

ReadRaw reads the raw request body and returns it as bytes.

type Context

type Context struct {
	Request    *http.Request
	Response   http.ResponseWriter
	Params     map[string]string // for route parameters
	Res        *NexResponse
	PathParam  *PathParam
	QueryParam *QueryParam
	Form       *Form
	Body       *Body

	Data map[string]any
	// contains filtered or unexported fields
}

Context is the main structure that will be passed to handlers. It provides methods and fields to interact with the HTTP request and response.

func NewContext

func NewContext(w http.ResponseWriter, r *http.Request) *Context

NewContext creates a new instance of Context.

func (*Context) Error

func (c *Context) Error() error

Error returns the error set in the context.

func (*Context) Get

func (c *Context) Get(key string) any

Get - Get the data from context

func (*Context) GetHeader

func (c *Context) GetHeader(key string) string

GetHeader retrieves a header from the request.

func (*Context) Set

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

Set -Set the data in context

func (*Context) SetError

func (c *Context) SetError(err error)

SetError sets an error in the context.

func (*Context) SetHeader

func (c *Context) SetHeader(key, value string)

SetHeader sets a header for the response.

func (*Context) String

func (c *Context) String(status int, s string)

String writes a string response to the client.

func (*Context) Validate

func (c *Context) Validate(v interface{}) []nexval.ValidationError

Validate - validates a struct.with the Go Playground validator v10 package struct tags.

type Form

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

func NewForm

func NewForm(ctx *Context) *Form

func (*Form) Bind

func (f *Form) Bind(v interface{}) error

Bind - binds the form to the given struct.

func (*Form) BindMultiPart

func (f *Form) BindMultiPart(v interface{}) error

BindMultiPart - binds the multipart form to the given struct.

func (*Form) CheckCSRF

func (f *Form) CheckCSRF() bool

CheckCSRF - Checks if CSRF token is valid. This assumes you're adding a CSRF token in your forms.

func (*Form) Get

func (f *Form) Get(name string) string

func (*Form) GetAsBool

func (f *Form) GetAsBool(name string) (bool, error)

Boolean method

func (*Form) GetAsFloat64

func (f *Form) GetAsFloat64(name string) (float64, error)

GetAsFloat64 - returns the value of the form parameter with the given name as a float64.

func (*Form) GetAsInt

func (f *Form) GetAsInt(name string) (int, error)

Integer methods

func (*Form) GetAsInt64

func (f *Form) GetAsInt64(name string) (int64, error)

func (*Form) GetFile

func (f *Form) GetFile(name string) (multipart.File, *multipart.FileHeader, error)

GetFile - Returns the uploaded file for a given key.

func (*Form) GetMultiple

func (f *Form) GetMultiple(name string) []string

GetMultiple - Returns multiple values for a given key (useful for checkboxes, multi-selects).

func (*Form) HasField

func (f *Form) HasField(name string) bool

HasField - Checks if a field with the given name is present in the form.

type NexResponse

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

func NewNexResponse

func NewNexResponse(ctx *Context) *NexResponse

NexResponse - create new NexResponse

func (*NexResponse) HTML

func (r *NexResponse) HTML(status int, payload string)

HTML sends a html response with the given status code and payload.

func (*NexResponse) JSON

func (r *NexResponse) JSON(status int, payload interface{})

JSON sends a JSON response with the given status code and payload.

func (*NexResponse) JsonBadRequest400

func (r *NexResponse) JsonBadRequest400(message string)

BadRequest400 - response with status 400 The server could not understand the request due to invalid syntax. The client SHOULD NOT repeat the request without modifications.

func (*NexResponse) JsonConflict409

func (r *NexResponse) JsonConflict409(message string)

Conflict409 - response with status 409 This response is sent when a request conflicts with the current state of the server.

func (*NexResponse) JsonForbidden403

func (r *NexResponse) JsonForbidden403(message string)

Forbidden403 - response with status 403 The client does not have access rights to the content; that is, it is unauthorized, so the server is refusing to give the requested resource. Unlike 401, the client's identity is known to the server.

func (*NexResponse) JsonInternalServerError500

func (r *NexResponse) JsonInternalServerError500(message string)

InternalServerError500 - response with status 500 The server has encountered a situation it doesn't know how to handle.

func (*NexResponse) JsonMethodNotAllowed405

func (r *NexResponse) JsonMethodNotAllowed405(message string)

MethodNotAllowed405 - response with status 405 The request method is known by the server but has been disabled and cannot be used. For example, an API may forbid DELETE-ing a resource. The two mandatory methods, GET and HEAD, must never be disabled and should not return this error code.

func (*NexResponse) JsonNoContent204

func (r *NexResponse) JsonNoContent204(message string)

NoContent204 - response with status 204 There is no content to send for this request, but the headers may be useful. The user-agent may update its cached headers for this resource with the new ones.

func (*NexResponse) JsonNotFound404

func (r *NexResponse) JsonNotFound404(message string)

NotFound404 - response with status 404 The server can not find the requested resource. In the browser, this means the URL is not recognized. In an API, this can also mean that the endpoint is valid but the resource itself does not exist. Servers may also send this response instead of 403 to hide the existence of a resource from an unauthorized client. This response code is probably the most famous one due to its frequent occurrence on the web.

func (*NexResponse) JsonOk200

func (r *NexResponse) JsonOk200(data any, message string)

JsonOk200 - response with status 200 The request succeeded. The result meaning of "success" depends on the HTTP method: GET: The resource has been fetched and transmitted in the message body. HEAD: The representation headers are included in the response without any message body. PUT or POST: The resource describing the result of the action is transmitted in the message body. TRACE: The message body contains the request message as received by the server.

func (*NexResponse) JsonOk201

func (r *NexResponse) JsonOk201(data any, message string)

Created201 - response with status 201 The request succeeded, and a new resource was created as a result. This is typically the response sent after POST requests, or some PUT requests.

func (*NexResponse) JsonOk202

func (r *NexResponse) JsonOk202(data any, message string)

Accepted202 - response with status 202 The request has been received but not yet acted upon. It is non-committal, meaning that there is no way in HTTP to later send an asynchronous response indicating the outcome of processing the request. It is intended for cases where another process or server handles the request, or for batch processing.

func (*NexResponse) JsonUnauthorized401

func (r *NexResponse) JsonUnauthorized401(message string)

Unauthorized401 - response with status 401 Although the HTTP standard specifies "unauthorized", semantically this response means "unauthenticated". That is, the client must authenticate itself to get the requested response.

func (*NexResponse) Text

func (r *NexResponse) Text(status int, payload string)

Text sends a plain text response with the given status code and payload.

type PathParam

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

func NewPathParam

func NewPathParam(ctx *Context) *PathParam

func (*PathParam) Get

func (p *PathParam) Get(name string) string

func (*PathParam) GetAsBool

func (p *PathParam) GetAsBool(name string) (bool, error)

Boolean method

func (*PathParam) GetAsInt

func (p *PathParam) GetAsInt(name string) (int, error)

Integer related methods

func (*PathParam) GetAsInt64

func (p *PathParam) GetAsInt64(name string) (int64, error)

func (*PathParam) GetAsUUID

func (p *PathParam) GetAsUUID(name string) (uuid.UUID, error)

UUID method

type QueryParam

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

func NewQueryParam

func NewQueryParam(ctx *Context) *QueryParam

NewQueryParam creates a new instance of QueryParam.

func (*QueryParam) Get

func (q *QueryParam) Get(name string) string

Get returns the value of the query parameter with the given name.

func (*QueryParam) GetAsBool

func (q *QueryParam) GetAsBool(name string) (bool, error)

Boolean method

func (*QueryParam) GetAsInt

func (q *QueryParam) GetAsInt(name string) (int, error)

Integer related methods

func (*QueryParam) GetAsInt64

func (q *QueryParam) GetAsInt64(name string) (int64, error)

func (*QueryParam) GetAsUUID

func (q *QueryParam) GetAsUUID(name string) (uuid.UUID, error)

UUID method

Jump to

Keyboard shortcuts

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