Documentation ¶
Index ¶
- Constants
- type CallerFunc
- type HandlerMaker
- func (h *HandlerMaker[ReqT, RespT]) Run(successStatus int)
- func (h *HandlerMaker[ReqT, RespT]) WithJSON() *HandlerMaker[ReqT, RespT]
- func (h *HandlerMaker[ReqT, RespT]) WithMultipart(maxMemory int64) *HandlerMaker[ReqT, RespT]
- func (h *HandlerMaker[ReqT, RespT]) WithQuery() *HandlerMaker[ReqT, RespT]
- func (h *HandlerMaker[ReqT, RespT]) WithVars() *HandlerMaker[ReqT, RespT]
Constants ¶
const ( ErrNotValidBodyFormat = "unable to unmarshal request body " ErrEmptyMultipartData = "empty multipart form data " )
const (
ERR_CODE_UnexpectedBody = 999
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CallerFunc ¶
type CallerFunc[ReqT any, RespT any] func(ctx context.Context, req ReqT) (RespT, tiny_errors.ErrorHandler)
A function that is called to process request.
ReqT - type of request body RespT - type of response body
type HandlerMaker ¶
func New ¶
func New[ReqT any, RespT any](w http.ResponseWriter, r *http.Request, logger logger.Logger, caller CallerFunc[ReqT, RespT]) *HandlerMaker[ReqT, RespT]
Create new handler instance
**caller** should be a function that implements type CallerFunc[ReqT, RespT]
func (*HandlerMaker[ReqT, RespT]) Run ¶
func (h *HandlerMaker[ReqT, RespT]) Run(successStatus int)
Run handler and send response with status code
func (*HandlerMaker[ReqT, RespT]) WithJSON ¶ added in v1.1.0
func (h *HandlerMaker[ReqT, RespT]) WithJSON() *HandlerMaker[ReqT, RespT]
Parsing JSON-body of request.
Request type should include fields with tags of json ¶
Example:
type YourRequest struct { FieldName string `json:"field_name"` }
func (*HandlerMaker[ReqT, RespT]) WithMultipart ¶
func (h *HandlerMaker[ReqT, RespT]) WithMultipart(maxMemory int64) *HandlerMaker[ReqT, RespT]
Parsing multipart-data from request body.
Request type should include fields with tags of mapstructure.
If field is an array of files you should set tag name as files[] and type []*multipart.FileHeader(mime/multipart.FileHeader)
If field is file and not array of files you should set tag with field name without brackets and type *multipart.FileHeader(mime/multipart.FileHeader)
Other fields should have string type(mime/multipart.Form)
File types ¶
- []*multipart.FileHeader - field with array of files. Should contain square brackets in name
- *multipart.FileHeader - field with single file. Should not contain square brackets in field name
Example
type YourRequest struct { MultipleFiles []*multipart.FileHeader `mapstructure:"multiple_files[]"` SingleFile *multipart.FileHeader `mapstructure:"single_file"` Name string `mapstructure:"name"` }
Supported nested structures. ¶
Example:
type Recipient struct { Name string `json:"name,omitempty" mapstructure:"name"` Age string `json:"age,omitempty" mapstructure:"age"` } type CreateOrder struct { Recipient Recipient `json:"recipient" mapstructure:"recipient"` Content map[string]string `json:"content" mapstructure:"content"` }
Request body(multipart-form):
{ "recipient[name]": "John", "recipient[age]": "30", "content[title]": "content title", "content[body]": "content body" }
Result:
func main() { // ... var order CreateOrder fmt.Println(order.Recipient.Name) // John fmt.Println(order.Recipient.Age) // 30 fmt.Println(order.Content["title"]) // content title fmt.Println(order.Content["body"]) // content body }
func (*HandlerMaker[ReqT, RespT]) WithQuery ¶
func (h *HandlerMaker[ReqT, RespT]) WithQuery() *HandlerMaker[ReqT, RespT]
Parsing URL-query params from request.
Request type should include fields with tags of mapstructure.
Example:
type YourRequest struct { FieldName string `mapstructure:"field_name"` }
func (*HandlerMaker[ReqT, RespT]) WithVars ¶
func (h *HandlerMaker[ReqT, RespT]) WithVars() *HandlerMaker[ReqT, RespT]
Parsing URI vars using gorilla/mux
Request type should include fields with tags of mapstructure.
Example:
type YourRequest struct { FieldName string `mapstructure:"field_name"` }