handler

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2024 License: MIT Imports: 10 Imported by: 1

Documentation

Index

Constants

View Source
const (
	ErrNotValidBodyFormat = "unable to unmarshal request body "
	ErrEmptyMultipartData = "empty multipart form data "
)

Variables

This section is empty.

Functions

func Default

func Default[Body any, Err any](w http.ResponseWriter, body Body, responseErr Err, status int)

func ErrorResponse

func ErrorResponse[Err any](w http.ResponseWriter, responseErr Err, status int)

func SuccessResponse

func SuccessResponse[Body any](w http.ResponseWriter, body Body, status int)

Types

type CallerFunc

type CallerFunc[ReqT any, RespT any] func(ctx context.Context, req ReqT) (RespT, tiny_errors.ErrorHandler)

type DefaultResponse

type DefaultResponse[T any, E any] struct {
	Error E `json:"error"`
	Body  T `json:"body"`
}

type HandlerMaker

type HandlerMaker[ReqT any, RespT any] struct {
	// contains filtered or unexported fields
}

func New

func New[ReqT any, RespT any](w http.ResponseWriter, r *http.Request, logger Logger, caller CallerFunc[ReqT, RespT]) *HandlerMaker[ReqT, RespT]

func (*HandlerMaker[ReqT, RespT]) Run

func (h *HandlerMaker[ReqT, RespT]) Run(successStatus int)

func (*HandlerMaker[ReqT, RespT]) WithJson

func (h *HandlerMaker[ReqT, RespT]) WithJson() *HandlerMaker[ReqT, RespT]

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]

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:"your_files[]"`
	SingleFile *multipart.FileHeader 	`mapstructure:"single_file"`
	Name string `mapstructure:"name"`
}

func (*HandlerMaker[ReqT, RespT]) WithQuery

func (h *HandlerMaker[ReqT, RespT]) WithQuery() *HandlerMaker[ReqT, RespT]

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]

Request type should include fields with tags of mapstructure

Example:

type YourRequest struct {
		FieldName string `mapstructure:"field_name"`
}

type Logger

type Logger interface {
	WithRequestInfo(r *http.Request) Logger
	With(args ...any) Logger

	Debug(msg string, args ...any)
	Info(msg string, args ...any)
	Error(msg string, args ...any)
}

Jump to

Keyboard shortcuts

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