Documentation
¶
Index ¶
- func MapMany[T any, R any](x []T, m func(e T) R) []R
- func MapManyE[T any, R any](x []T, m func(e T) (R, error)) ([]R, error)
- func MethodNotAllowed(c Context)
- type Context
- type Engine
- type HandlerFunc
- type PathRegistry
- type ResponseStatusError
- func NewBadRequestError(message string) *ResponseStatusError
- func NewConflictError(message string) *ResponseStatusError
- func NewForbiddenError(message string) *ResponseStatusError
- func NewNotFoundError(message string) *ResponseStatusError
- func NewResponseStatusError(statusCode int, message string) *ResponseStatusError
- func NewUnauthorizedError(message string) *ResponseStatusError
- type Router
- type RouterGroup
- type Validatable
- type Validator
- func (v *Validator) Require(condition bool, message string) *Validator
- func (v *Validator) RequireEnum(s string, message string, values ...string) *Validator
- func (v *Validator) RequireMatchesRegex(s string, regex *regexp.Regexp, message string) *Validator
- func (v *Validator) RequireMaxLength(s string, max int, message string) *Validator
- func (v *Validator) RequireMinLength(s string, min int, message string) *Validator
- func (v *Validator) RequireNotEmpty(s string, message string) *Validator
- func (v *Validator) RequireSliceEnum(s []string, message string, values ...string) *Validator
- func (v *Validator) RequireSliceMinLength(s []string, min int, message string) *Validator
- func (v *Validator) RequireSliceNotEmpty(s []string, message string) *Validator
- func (v *Validator) RequireStringLengthBetween(s string, min int, max int, message string) *Validator
- func (v *Validator) RequireStringMaxLength(s string, max int, message string) *Validatordeprecated
- func (v *Validator) RequireStringMinLength(s string, min int, message string) *Validatordeprecated
- func (v *Validator) RequireStringNotEmpty(s string, message string) *Validatordeprecated
- func (v *Validator) RequireStringSliceEnum(s []string, message string, values ...string) *Validatordeprecated
- func (v *Validator) RequireStringSliceMinLength(s []string, min int, message string) *Validatordeprecated
- func (v *Validator) RequireStringSliceNotEmpty(s []string, message string) *Validatordeprecated
- func (v *Validator) V(fun func(*Validator)) *Validator
- func (v *Validator) Validate() error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MethodNotAllowed ¶
func MethodNotAllowed(c Context)
Types ¶
type Context ¶
type Context interface {
// Get gets a value from the context.
Get(key string) (any, bool)
// MustGet tries to get a value from the context. If the value cannot be found the request is aborted.
MustGet(key string) any
// Set sets a context value.
Set(key string, value any)
// Query gets a raw query value
Query(key string) string
// QueryArray gets an array query value
QueryArray(key string) []string
// IntQuery gets a query value as int
IntQuery(key string) (int, error)
// BoolQuery gets a query value as bool
BoolQuery(key string) (bool, error)
// Iso8601DateQuery gets a query value as ISO 8601 Date
Iso8601DateQuery(key string) (*time.Time, error)
// Iso8601DateTimeQuery gets a query values as ISO 8601 DateTime
Iso8601DateTimeQuery(key string) (*time.Time, error)
// StringQuery gets a query value as string. This method performs unescaping.
StringQuery(key string) (string, error)
// DefaultQuery gets a query value. If the value cannot be found a default value is returned.
DefaultQuery(key string, defaultValue string) string
// DefaultIntQuery gets a query value as int. If the value cannot be found a default value is returned.
DefaultIntQuery(key string, defaultValue int) (int, error)
// DefaultBoolQuery gets a query value as bool. If the value cannot be found a default value is returned.
DefaultBoolQuery(key string, defaultValue bool) (bool, error)
// DefaultStringQuery gets a query value as string. If the value cannot be found a default value is returned.
DefaultStringQuery(key string, defaultValue string) (string, error)
// GetHeader gets a request header
GetHeader(key string) string
// Param gets a request param (aka path parameter)
Param(key string) string
// GetRawData gets the raw request body
GetRawData() ([]byte, error)
// MayBindJSON tries to bind the request body from JSON to the given object.
MayBindJSON(obj any) bool
// MayBindJSONV tries to bind the request body from JSON to the given object. It will then invoke the provided validator function.
MayBindJSONV(obj any, validator func() error) bool
// MustBindJSON tries to bind the request body from JSON to the given object. If that fails the request is aborted with 400.
MustBindJSON(obj any) bool
// MustBindJSONV tries to bind the request body from JSON to the given object. If that fails the request is aborted with 400.
// If it succeeds the provided validator function is invoked.
MustBindJSONV(obj any, validator func() error) bool
// Request gets the original http request
Request() *http.Request
// Writer gets the http response writer
Writer() http.ResponseWriter
//ClientIP implements one best effort algorithm to return the real client IP.
//It calls c.RemoteIP() under the hood, to check if the remote IP is a trusted proxy or not.
//If it is it will then try to parse the headers defined in Engine.RemoteIPHeaders (defaulting to [X-Forwarded-For, X-Real-Ip]).
//If the headers are not syntactically valid OR the remote IP does not correspond to a trusted proxy,
//the remote IP (coming from Request.RemoteAddr) is returned.
ClientIP() string
//RemoteIP parses the IP from Request.RemoteAddr, normalizes and returns the IP (without the port).
RemoteIP() string
// Status sets the response status code.
Status(code int) Context
// String sets the response status code and writes a string response.
String(code int, format string, values ...any) Context
// SetHeader sets a response header.
SetHeader(key string, value string)
// SetContentType sets the response content type.
SetContentType(value string)
// Cookie returns the named cookie provided in the request or false if not found.
// If multiple cookies match the given name, only one cookie will be returned.
Cookie(name string) (string, bool)
// SetCookie sets a cookie.
SetCookie(name string, value string, maxAge int, path string, domain string, secure bool, httpOnly bool)
// Stream writes a stream response.
Stream(step func(w io.Writer) bool) bool
// SSEvent writes a server sent event.
SSEvent(name string, message any)
// Data sets the response status code and writes the given data as is.
Data(code int, contentType string, data []byte)
// RespondOk sets status 200, marshals obj to JSON
RespondOk(obj any)
// RespondNoContent sets status 204, no response body
RespondNoContent()
// RespondCreated sets status 201, marshals obj to JSON
RespondCreated(obj any)
// RespondForbidden sets status 403, marshals obj to JSON
RespondForbidden(obj any)
// RespondForbiddenE sets status 403, writes error as error response (JSON)
RespondForbiddenE(err error)
RespondUnauthorized(obj any)
RespondUnauthorizedE(err error)
// RespondBadRequest sets status 400, marshals obj to JSON
RespondBadRequest(obj any)
// RespondBadRequestE sets status 400, writes error as error response (JSON)
RespondBadRequestE(err error)
// RespondNotFound sets status 404, marshals obj to JSON
RespondNotFound(obj any)
// RespondNotFoundE sets status 404, writes error as error response (JSON)
RespondNotFoundE(err error)
// RespondConflict sets status 409, marshals obj to JSON
RespondConflict(obj any)
// RespondConflictE sets status 409, writes error as error response (JSON)
RespondConflictE(err error)
// RespondInternalServerError sets status 500, marshals obj to JSON
RespondInternalServerError(obj any)
// RespondInternalServerErrorE sets status 500, writes error as error response (JSON)
RespondInternalServerErrorE(err error)
// RespondMissingRequestBody sets status 400, writes error response (JSON)
RespondMissingRequestBody()
// Abort prevents pending handlers being called. This will not stop the current handler.
Abort()
// AbortWithError prevents pending handlers being called. This will not stop the current handler.
// The response status code is set to the given value.
// Writes error as error response (JSON).
AbortWithError(code int, err error)
// Next should only be used in middlewares. It executes pending handlers in the chain inside the current handler.
Next()
// HandleError inspects the given error and writes an appropriate response.
HandleError(err error)
// Deadline returns that there is no deadline (ok==false) when c.Request has no Context.
Deadline() (deadline time.Time, ok bool)
// Done returns nil (chan which will wait forever) when c.Request has no Context.
Done() <-chan struct{}
// Err returns nil when c.Request has no Context.
Err() error
// Value returns the value associated with this context for key, or nil if no value is associated with key.
// Successive calls to Value with the same key returns the same result.
Value(key any) any
}
type Engine ¶
type Engine interface {
RouterGroup
NoMethod(handlers ...HandlerFunc)
NoRoute(handlers ...HandlerFunc)
// ExpandMethods expands each non-configured method for each path to return 405 Method not allowed
ExpandMethods()
Run(addr ...string) error
EnableDebugMode()
ServeHTTP(w http.ResponseWriter, req *http.Request)
}
type HandlerFunc ¶
type HandlerFunc func(c Context)
type PathRegistry ¶
type PathRegistry struct {
// contains filtered or unexported fields
}
func NewPathRegistry ¶
func NewPathRegistry() *PathRegistry
func (*PathRegistry) Add ¶
func (p *PathRegistry) Add(path string, methods ...string)
func (*PathRegistry) Paths ¶
func (p *PathRegistry) Paths() []string
type ResponseStatusError ¶
func NewBadRequestError ¶
func NewBadRequestError(message string) *ResponseStatusError
func NewConflictError ¶
func NewConflictError(message string) *ResponseStatusError
func NewForbiddenError ¶
func NewForbiddenError(message string) *ResponseStatusError
func NewNotFoundError ¶
func NewNotFoundError(message string) *ResponseStatusError
func NewResponseStatusError ¶
func NewResponseStatusError(statusCode int, message string) *ResponseStatusError
func NewUnauthorizedError ¶
func NewUnauthorizedError(message string) *ResponseStatusError
func (*ResponseStatusError) Error ¶
func (e *ResponseStatusError) Error() string
type Router ¶
type Router interface {
Use(middleware ...HandlerFunc) Router
Any(relativePath string, handlers ...HandlerFunc) Router
GET(relativePath string, handlers ...HandlerFunc) Router
POST(relativePath string, handlers ...HandlerFunc) Router
PUT(relativePath string, handlers ...HandlerFunc) Router
DELETE(relativePath string, handlers ...HandlerFunc) Router
PATCH(relativePath string, handlers ...HandlerFunc) Router
OPTIONS(relativePath string, handlers ...HandlerFunc) Router
HEAD(relativePath string, handlers ...HandlerFunc) Router
}
type RouterGroup ¶
type RouterGroup interface {
Router
Group(relativePath string, handlers ...HandlerFunc) RouterGroup
}
type Validatable ¶
type Validatable interface {
Validate() error
}
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
func NewValidator ¶
func NewValidator() *Validator
func ValidateSub ¶
func ValidateSub[T Validatable](v *Validator, key string, items []T) *Validator
ValidateSub performs validation on a sub item.
func (*Validator) RequireEnum ¶
RequireEnum requires a value to be found in a given enum
func (*Validator) RequireMatchesRegex ¶
RequireMatchesRegex requires a value to match a given regular expression
func (*Validator) RequireMaxLength ¶ added in v0.2.0
RequireMaxLength requires a value to have a given maximum length
func (*Validator) RequireMinLength ¶ added in v0.2.0
RequireMinLength requires a value to have a given minimum length
func (*Validator) RequireNotEmpty ¶ added in v0.2.0
RequireNotEmpty requires a string not to be empty
func (*Validator) RequireSliceEnum ¶ added in v0.2.0
RequireSliceEnum requires the given slice to only contain elements from values...
func (*Validator) RequireSliceMinLength ¶ added in v0.2.0
RequireSliceMinLength requires the given slice to have at least min elements
func (*Validator) RequireSliceNotEmpty ¶ added in v0.2.0
RequireSliceNotEmpty requires the given slice not to be empty
func (*Validator) RequireStringLengthBetween ¶
func (v *Validator) RequireStringLengthBetween(s string, min int, max int, message string) *Validator
RequireStringLengthBetween requires a string TODO
func (*Validator) RequireStringNotEmpty
deprecated
func (*Validator) RequireStringSliceNotEmpty
deprecated
Click to show internal directories.
Click to hide internal directories.