Documentation
¶
Index ¶
- func MapValidationMessage(e validator.FieldError, value string) string
- func WriteError(w http.ResponseWriter, r *http.Request, err error)
- type APIKeyAuthConfig
- type APIKeyAuthType
- func (t APIKeyAuthType[AuthParams, AuthModel]) GetDescription() string
- func (t APIKeyAuthType[AuthParams, AuthModel]) GetFieldName() string
- func (t APIKeyAuthType[AuthParams, AuthModel]) GetFormat() string
- func (t APIKeyAuthType[AuthParams, AuthModel]) GetHandler() AuthHandlerFunc[AuthParams, AuthModel]
- func (t APIKeyAuthType[AuthParams, AuthModel]) GetIn() openapi.In
- func (t APIKeyAuthType[AuthParams, AuthModel]) GetName() string
- func (t APIKeyAuthType[AuthParams, AuthModel]) GetType() AuthType
- type Application
- type AuthHandler
- func APIKeyAuth[AuthParams, AuthModel any](handler AuthHandlerFunc[AuthParams, AuthModel], config APIKeyAuthConfig) AuthHandler[AuthParams, AuthModel]
- func BasicAuth[AuthParams, AuthModel any](handler AuthHandlerFunc[AuthParams, AuthModel], config BasicAuthConfig) AuthHandler[AuthParams, AuthModel]
- func BearerAuth[AuthParams, AuthModel any](handler AuthHandlerFunc[AuthParams, AuthModel], config BearerAuthConfig) AuthHandler[AuthParams, AuthModel]
- type AuthHandlerFunc
- type AuthRequest
- type AuthType
- type AuthenticatedJsonHandlerFunc
- type AuthenticatedMultipartHandlerFunc
- type BasicAuthConfig
- type BasicAuthType
- func (t BasicAuthType[AuthParams, AuthModel]) GetDescription() string
- func (t BasicAuthType[AuthParams, AuthModel]) GetFieldName() string
- func (t BasicAuthType[AuthParams, AuthModel]) GetFormat() string
- func (t BasicAuthType[AuthParams, AuthModel]) GetHandler() AuthHandlerFunc[AuthParams, AuthModel]
- func (t BasicAuthType[AuthParams, AuthModel]) GetIn() openapi.In
- func (t BasicAuthType[AuthParams, AuthModel]) GetName() string
- func (t BasicAuthType[AuthParams, AuthModel]) GetType() AuthType
- type BearerAuthConfig
- type BearerAuthType
- func (t BearerAuthType[AuthParams, AuthModel]) GetDescription() string
- func (t BearerAuthType[AuthParams, AuthModel]) GetFieldName() string
- func (t BearerAuthType[AuthParams, AuthModel]) GetFormat() string
- func (t BearerAuthType[AuthParams, AuthModel]) GetHandler() AuthHandlerFunc[AuthParams, AuthModel]
- func (t BearerAuthType[AuthParams, AuthModel]) GetIn() openapi.In
- func (t BearerAuthType[AuthParams, AuthModel]) GetName() string
- func (t BearerAuthType[AuthParams, AuthModel]) GetType() AuthType
- type ErrorResponse
- type HTTPError
- type Handler
- func AuthJsonHandler[RequestBody, Params, AuthParams, AuthModel, ResponseBody any](...) Handler
- func AuthMultipartHandler[Params, AuthParams, AuthModel, ResponseBody any](...) Handler
- func JsonHandler[RequestBody, Params, ResponseBody any](h JsonHandlerFunc[RequestBody, Params, ResponseBody]) Handler
- func MultipartHandler[Params any, ResponseBody any](h MultipartHandlerFunc[Params, ResponseBody]) Handler
- type JsonHandlerFunc
- type MultipartHandlerFunc
- type MultipartRequest
- type NoBody
- type NoParams
- type ParameterType
- type Request
- type Response
- type Router
- func (r *Router) DELETE(path string, handler Handler)
- func (r *Router) Extend(middleware []func(http.Handler) http.Handler)
- func (r *Router) GET(path string, handler Handler)
- func (r *Router) HEAD(path string, handler Handler)
- func (r *Router) Handle(method, path string, handler Handler)
- func (r *Router) OPTIONS(path string, handler Handler)
- func (r *Router) PATCH(path string, handler Handler)
- func (r *Router) POST(path string, handler Handler)
- func (r *Router) PUT(path string, handler Handler)
- func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (r *Router) Use(middleware func(http.Handler) http.Handler)
- type ValidationError
- type ValidationErrors
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MapValidationMessage ¶ added in v0.10.6
func MapValidationMessage(e validator.FieldError, value string) string
MapValidationMessage returns appropriate error message based on the validation tag
func WriteError ¶ added in v0.12.0
func WriteError(w http.ResponseWriter, r *http.Request, err error)
WriteError is a helper function for handling errors in HTTP handlers
Types ¶
type APIKeyAuthConfig ¶ added in v0.13.0
type APIKeyAuthType ¶ added in v0.13.0
type APIKeyAuthType[AuthParams, AuthModel any] struct { Name string FieldName string In openapi.In Description string Handler AuthHandlerFunc[AuthParams, AuthModel] }
func (APIKeyAuthType[AuthParams, AuthModel]) GetDescription ¶ added in v0.13.0
func (t APIKeyAuthType[AuthParams, AuthModel]) GetDescription() string
func (APIKeyAuthType[AuthParams, AuthModel]) GetFieldName ¶ added in v0.13.0
func (t APIKeyAuthType[AuthParams, AuthModel]) GetFieldName() string
func (APIKeyAuthType[AuthParams, AuthModel]) GetFormat ¶ added in v0.13.0
func (t APIKeyAuthType[AuthParams, AuthModel]) GetFormat() string
func (APIKeyAuthType[AuthParams, AuthModel]) GetHandler ¶ added in v0.13.0
func (t APIKeyAuthType[AuthParams, AuthModel]) GetHandler() AuthHandlerFunc[AuthParams, AuthModel]
func (APIKeyAuthType[AuthParams, AuthModel]) GetIn ¶ added in v0.13.0
func (t APIKeyAuthType[AuthParams, AuthModel]) GetIn() openapi.In
func (APIKeyAuthType[AuthParams, AuthModel]) GetName ¶ added in v0.13.0
func (t APIKeyAuthType[AuthParams, AuthModel]) GetName() string
func (APIKeyAuthType[AuthParams, AuthModel]) GetType ¶ added in v0.13.0
func (t APIKeyAuthType[AuthParams, AuthModel]) GetType() AuthType
type Application ¶
type Application struct {
// Server is the HTTP server for the application
Server *http.Server
// Router is the main Mux for the application
Router *Router
// Settings is the application Settings
Settings *settings.Config
}
Application is the main application struct that holds the Mux and other application Settings
func Default ¶
func Default(provided ...settings.Config) *Application
Default returns a new Application application with default Config
func New ¶
func New(provided ...settings.Config) *Application
New returns a new Application application
func (*Application) Start ¶ added in v0.4.0
func (a *Application) Start()
func (*Application) Stop ¶ added in v0.4.0
func (a *Application) Stop() error
type AuthHandler ¶ added in v0.13.0
type AuthHandler[AuthParams, AuthModel any] interface { GetType() AuthType GetName() string GetFieldName() string GetFormat() string GetDescription() string GetIn() openapi.In GetHandler() AuthHandlerFunc[AuthParams, AuthModel] }
func APIKeyAuth ¶ added in v0.13.0
func APIKeyAuth[AuthParams, AuthModel any]( handler AuthHandlerFunc[AuthParams, AuthModel], config APIKeyAuthConfig, ) AuthHandler[AuthParams, AuthModel]
APIKeyAuth creates an API key auth handler with configuration
func BasicAuth ¶ added in v0.13.0
func BasicAuth[AuthParams, AuthModel any]( handler AuthHandlerFunc[AuthParams, AuthModel], config BasicAuthConfig, ) AuthHandler[AuthParams, AuthModel]
BasicAuth creates a basic auth handler with configuration
func BearerAuth ¶ added in v0.13.0
func BearerAuth[AuthParams, AuthModel any]( handler AuthHandlerFunc[AuthParams, AuthModel], config BearerAuthConfig, ) AuthHandler[AuthParams, AuthModel]
BearerAuth creates a bearer auth handler with configuration
type AuthHandlerFunc ¶ added in v0.3.0
type AuthHandlerFunc[AuthParams, AuthModel any] func(ctx context.Context, req *AuthRequest[AuthParams]) (*AuthModel, error)
AuthHandlerFunc is a function that handles authentication for a route.
type AuthRequest ¶ added in v0.13.0
type AuthRequest[AuthParams any] struct { Params AuthParams }
AuthRequest is a request object that contains the parameters needed to authenticate a request. The parameters are taken from the headers, cookies, or query parameters of the request and should be specified using tags in the struct definition.
type AuthenticatedJsonHandlerFunc ¶ added in v0.6.0
type AuthenticatedJsonHandlerFunc[RequestBody, Params, AuthParams, AuthModel, ResponseBody any] struct { // contains filtered or unexported fields }
AuthenticatedJsonHandlerFunc is a function type for handling authenticated routes with Request body and params
func (AuthenticatedJsonHandlerFunc[RequestBody, Params, AuthParams, AuthModel, ResponseBody]) ServeHTTP ¶ added in v0.6.0
func (h AuthenticatedJsonHandlerFunc[RequestBody, Params, AuthParams, AuthModel, ResponseBody]) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements the http.Handler interface for AuthenticatedJsonHandlerFunc
type AuthenticatedMultipartHandlerFunc ¶ added in v0.7.0
type AuthenticatedMultipartHandlerFunc[Params, AuthParams, AuthModel, ResponseBody any] struct { // contains filtered or unexported fields }
AuthenticatedMultipartHandlerFunc is a function type for handling a MultipartRequest with params and an authenticated model
func (AuthenticatedMultipartHandlerFunc[Params, AuthParams, AuthModel, ResponseBody]) ServeHTTP ¶ added in v0.7.0
func (h AuthenticatedMultipartHandlerFunc[Params, AuthParams, AuthModel, ResponseBody]) ServeHTTP(w http.ResponseWriter, r *http.Request)
type BasicAuthConfig ¶ added in v0.13.0
type BasicAuthType ¶ added in v0.13.0
type BasicAuthType[AuthParams, AuthModel any] struct { Name string Description string Handler AuthHandlerFunc[AuthParams, AuthModel] }
func (BasicAuthType[AuthParams, AuthModel]) GetDescription ¶ added in v0.13.0
func (t BasicAuthType[AuthParams, AuthModel]) GetDescription() string
func (BasicAuthType[AuthParams, AuthModel]) GetFieldName ¶ added in v0.13.0
func (t BasicAuthType[AuthParams, AuthModel]) GetFieldName() string
func (BasicAuthType[AuthParams, AuthModel]) GetFormat ¶ added in v0.13.0
func (t BasicAuthType[AuthParams, AuthModel]) GetFormat() string
func (BasicAuthType[AuthParams, AuthModel]) GetHandler ¶ added in v0.13.0
func (t BasicAuthType[AuthParams, AuthModel]) GetHandler() AuthHandlerFunc[AuthParams, AuthModel]
func (BasicAuthType[AuthParams, AuthModel]) GetIn ¶ added in v0.13.0
func (t BasicAuthType[AuthParams, AuthModel]) GetIn() openapi.In
func (BasicAuthType[AuthParams, AuthModel]) GetName ¶ added in v0.13.0
func (t BasicAuthType[AuthParams, AuthModel]) GetName() string
func (BasicAuthType[AuthParams, AuthModel]) GetType ¶ added in v0.13.0
func (t BasicAuthType[AuthParams, AuthModel]) GetType() AuthType
type BearerAuthConfig ¶ added in v0.13.0
type BearerAuthType ¶ added in v0.13.0
type BearerAuthType[AuthParams, AuthModel any] struct { Name string Format string Description string Handler AuthHandlerFunc[AuthParams, AuthModel] }
func (BearerAuthType[AuthParams, AuthModel]) GetDescription ¶ added in v0.13.0
func (t BearerAuthType[AuthParams, AuthModel]) GetDescription() string
func (BearerAuthType[AuthParams, AuthModel]) GetFieldName ¶ added in v0.13.0
func (t BearerAuthType[AuthParams, AuthModel]) GetFieldName() string
func (BearerAuthType[AuthParams, AuthModel]) GetFormat ¶ added in v0.13.0
func (t BearerAuthType[AuthParams, AuthModel]) GetFormat() string
func (BearerAuthType[AuthParams, AuthModel]) GetHandler ¶ added in v0.13.0
func (t BearerAuthType[AuthParams, AuthModel]) GetHandler() AuthHandlerFunc[AuthParams, AuthModel]
func (BearerAuthType[AuthParams, AuthModel]) GetIn ¶ added in v0.13.0
func (t BearerAuthType[AuthParams, AuthModel]) GetIn() openapi.In
func (BearerAuthType[AuthParams, AuthModel]) GetName ¶ added in v0.13.0
func (t BearerAuthType[AuthParams, AuthModel]) GetName() string
func (BearerAuthType[AuthParams, AuthModel]) GetType ¶ added in v0.13.0
func (t BearerAuthType[AuthParams, AuthModel]) GetType() AuthType
type ErrorResponse ¶
type ErrorResponse struct {
// Timestamp of the error
Timestamp time.Time `json:"timestamp" example:"2021-01-01T12:00:00Z"`
// HTTP status code
Status int `json:"status" example:"400"`
// HTTP error type
Error string `json:"error" example:"Bad Request"`
// Path of the Request
Path string `json:"path" example:"/api/v1/users"`
// Method of the Request
Method string `json:"method" example:"GET"`
// Request ID
RequestID string `json:"requestId,omitempty" example:"123e4567-e89b-12d3-a456-426614174000" required:"false"`
// Error message
Message string `json:"message,omitempty" example:"Validation failed"`
// Validation errors
ValidationErrors []ValidationError `json:"validationErrors,omitempty" required:"false"`
}
ErrorResponse defines the structure of an error message
func NewErrorResponse ¶
func NewErrorResponse(r *http.Request, status int, message string, validationErrors ...ValidationError) *ErrorResponse
NewErrorResponse creates a new ErrorResponse instance with the given status and message
type HTTPError ¶
type HTTPError struct {
HttpStatusCode int
PublicMessage string
ValidationErrors ValidationErrors
// contains filtered or unexported fields
}
func NewHttpError ¶
func NewHttpError(httpStatusCode int, publicMessage string, err error, validationErrors ...ValidationError) *HTTPError
NewHttpError creates a new ApiError
func WrapError ¶ added in v0.8.1
func WrapError(httpStatusCode int, err error, publicMessage string, validationErrors ...ValidationError) *HTTPError
WrapError wraps an error with an HTTP status code
func (*HTTPError) HasValidationErrors ¶
HasValidationErrors checks if there are validation errors
type Handler ¶
type Handler interface {
ServeHTTP(w http.ResponseWriter, r *http.Request)
// contains filtered or unexported methods
}
Handler specifies the interface for a handler that can be registered with the Router.
func AuthJsonHandler ¶ added in v0.6.0
func AuthJsonHandler[RequestBody, Params, AuthParams, AuthModel, ResponseBody any]( handler func(ctx context.Context, req *Request[RequestBody, Params], authModel *AuthModel) (*Response[ResponseBody], error), authHandler AuthHandler[AuthParams, AuthModel], ) Handler
AuthJsonHandler handles a Request with the Request body and params.
Example usage:
Define a Request body struct:
type RequestBody struct {
Test string `json:"test" validate:"required"`
}
Define a Request params struct:
type Params struct {
Name string `header:"name" validate:"required"`
ID int `path:"id" validate:"required"`
Active bool `query:"active" validate:"required"`
Page int64 `query:"page" validate:"min=0"`
Size int64 `query:"size" validate:"min=0"`
}
Define a user struct:
type AuthModel struct {
ID int
Name string
Role string
}
Define a handler function:
func(ctx context.Context, req *simba.Request[RequestBody, Params], authModel *AuthModel) (*simba.Response[map[string]string], error) {
// Access the Request body and params fields
req.Body.Test
req.Params.Name
req.Params.ID
req.Params.Page
req.Params.Size
// Access the user fields
user.ID
user.Name
user.Role
// Return a response
return &simba.Response[map[string]string]{
Headers: map[string][]string{"My-Header": {"header-value"}},
Cookies: []*http.Cookie{{Name: "My-Cookie", Value: "cookie-value"}},
Body: map[string]string{"message": "success"},
Status: http.StatusOK,
}, nil
}
Register the handler:
Mux.POST("/test/{id}", simba.AuthJsonHandler(handler))
func AuthMultipartHandler ¶ added in v0.7.0
func AuthMultipartHandler[Params, AuthParams, AuthModel, ResponseBody any]( handler func(ctx context.Context, req *MultipartRequest[Params], authModel *AuthModel) (*Response[ResponseBody], error), authHandler AuthHandler[AuthParams, AuthModel], ) Handler
AuthMultipartHandler handles a MultipartRequest with params and an authenticated model. The MultipartRequest holds a MultipartReader and the parsed params. The reason to provide the reader is to allow the logic for processing the parts to be handled by the handler function.
Example usage:
Define a Request params struct:
type Params struct {
Name string `header:"name" validate:"required"`
ID int `path:"id" validate:"required"`
Active bool `query:"active" validate:"required"`
Page int64 `query:"page" validate:"min=0"`
Size int64 `query:"size" validate:"min=0"`
}
Define a user struct:
type AuthModel struct {
ID int
Name string
Role string
}
Define a handler function:
func(ctx context.Context, req *simba.MultipartRequest[Params], authModel *AuthModel) (*simba.Response[map[string]string], error) {
// Access the Multipart reader and params fields
req.Params.Name
req.Params.ID
req.Params.Page
req.Params.Size
req.Reader // Multipart reader
// Access the user fields
user.ID
user.Name
user.Role
// Return a response
return &simba.Response[map[string]string]{
Headers: map[string][]string{"My-Header": {"header-value"}},
Cookies: []*http.Cookie{{Name: "My-Cookie", Value: "cookie-value"}},
Body: map[string]string{"message": "success"},
Status: http.StatusOK,
}, nil
}
Register the handler:
Mux.POST("/test/{id}", simba.AuthMultipartHandler(handler))
func JsonHandler ¶ added in v0.6.0
func JsonHandler[RequestBody, Params, ResponseBody any](h JsonHandlerFunc[RequestBody, Params, ResponseBody]) Handler
JsonHandler handles a Request with the Request body and params.
Example usage:
Define a Request body struct:
type RequestBody struct {
Test string `json:"test" validate:"required"`
}
Define a Request params struct:
type Params struct {
Name string `header:"name" validate:"required"`
ID int `path:"id" validate:"required"`
Active bool `query:"active" validate:"required"`
Page int64 `query:"page" validate:"min=0"`
Size int64 `query:"size" validate:"min=0"`
}
Define a handler function:
func(ctx context.Context, req *simba.Request[RequestBody, Params]) (*simba.Response[map[string]string], error) {
// Access the Request body and params fields
req.Body.Test
req.Params.Name
req.Params.ID
req.Params.Page
req.Params.Size
// Return a response
return &simba.Response[map[string]string]{
Headers: map[string][]string{"My-Header": {"header-value"}},
Cookies: []*http.Cookie{{Name: "My-Cookie", Value: "cookie-value"}},
Body: map[string]string{"message": "success"},
Status: http.StatusOK,
}, nil
}
Register the handler:
Mux.POST("/test/{id}", simba.JsonHandler(handler))
func MultipartHandler ¶ added in v0.7.0
func MultipartHandler[Params any, ResponseBody any](h MultipartHandlerFunc[Params, ResponseBody]) Handler
MultipartHandler handles a MultipartRequest with params. // The MultipartRequest holds a MultipartReader and the parsed params. // The reason to provide the reader is to allow the logic for processing the parts to be handled by the handler function.
Example usage:
Define a Request params struct:
type Params struct {
Name string `header:"name" validate:"required"`
ID int `path:"id" validate:"required"`
Active bool `query:"active" validate:"required"`
Page int64 `query:"page" validate:"min=0"`
Size int64 `query:"size" validate:"min=0"`
}
Define a handler function:
func(ctx context.Context, req *simba.Request[RequestBody, Params]) (*simba.Response[map[string]string], error) {
// Access the Multipart reader and params fields
req.Params.Name
req.Params.ID
req.Params.Page
req.Params.Size
req.Reader // Multipart reader
// Return a response
return &simba.Response[map[string]string]{
Headers: map[string][]string{"My-Header": {"header-value"}},
Cookies: []*http.Cookie{{Name: "My-Cookie", Value: "cookie-value"}},
Body: map[string]string{"message": "success"},
Status: http.StatusOK,
}, nil
}
Register the handler:
Mux.POST("/test/{id}", simba.MultipartHandler(handler))
type JsonHandlerFunc ¶ added in v0.6.0
type JsonHandlerFunc[RequestBody, Params, ResponseBody any] func(ctx context.Context, req *Request[RequestBody, Params]) (*Response[ResponseBody], error)
JsonHandlerFunc is a function type for handling routes with Request body and params
func (JsonHandlerFunc[RequestBody, Params, ResponseBody]) ServeHTTP ¶ added in v0.6.0
func (h JsonHandlerFunc[RequestBody, Params, ResponseBody]) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements the http.Handler interface for JsonHandlerFunc
type MultipartHandlerFunc ¶ added in v0.7.0
type MultipartHandlerFunc[Params any, ResponseBody any] func(ctx context.Context, req *MultipartRequest[Params]) (*Response[ResponseBody], error)
MultipartHandlerFunc is a function type for handling routes with Request body and params
func (MultipartHandlerFunc[Params, ResponseBody]) ServeHTTP ¶ added in v0.7.0
func (h MultipartHandlerFunc[Params, ResponseBody]) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements the http.Handler interface for JsonHandlerFunc
type MultipartRequest ¶ added in v0.7.0
type ParameterType ¶
type ParameterType string
const ( ParameterTypeHeader ParameterType = "header" ParameterTypeCookie ParameterType = "cookie" ParameterTypePath ParameterType = "path" ParameterTypeQuery ParameterType = "query" ParameterTypeBody ParameterType = "body" )
func (ParameterType) String ¶ added in v0.3.0
func (p ParameterType) String() string
type Response ¶
type Response[ResponseBody any] struct { Headers http.Header Cookies []*http.Cookie Body ResponseBody Status int }
Response represents a HTTP response
type Router ¶ added in v0.2.0
Router is a simple Mux that wraps http.ServeMux and allows for middleware chaining and type information storage for routes.
func (*Router) DELETE ¶ added in v0.2.0
DELETE registers a handler for DELETE requests to the given pattern
func (*Router) HEAD ¶ added in v0.2.0
HEAD registers a handler for HEAD requests to the given pattern
func (*Router) OPTIONS ¶ added in v0.2.0
OPTIONS registers a handler for OPTIONS requests to the given pattern
func (*Router) PATCH ¶ added in v0.2.0
PATCH registers a handler for PATCH requests to the given pattern
func (*Router) POST ¶ added in v0.2.0
POST registers a handler for POST requests to the given pattern
func (*Router) ServeHTTP ¶ added in v0.2.0
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP implements the http.Handler interface for the Router type
type ValidationError ¶
type ValidationError struct {
// Parameter or field that failed validation
Parameter string `json:"parameter" example:"name"`
// Type indicates where the parameter was located (header, path, query, body)
Type ParameterType `json:"type" example:"query"`
// Error message describing the validation error
Message string `json:"message" example:"name is required"`
}
ValidationError defines the interface for a validation error
type ValidationErrors ¶
type ValidationErrors []ValidationError
ValidationErrors represents multiple validation errors
func ValidateStruct ¶ added in v0.10.4
func ValidateStruct(request any, paramType ParameterType) ValidationErrors
ValidateStruct is a helper function for validating requests using the validator package. If the request is nil, it will return nil. If the request is valid, it will return an empty slice of ValidationErrors. If the request is invalid, it will return a slice of ValidationErrors containing the validation errors for each field.
func (ValidationErrors) Error ¶
func (ve ValidationErrors) Error() string
Error implements the error interface