gohandler

package module
v0.0.0-...-47fd5e2 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2022 License: MIT Imports: 4 Imported by: 0

README ¶

🚧 gohandler

This is simplified request handler package. Main purpose of this package isolating services from controller. Best feature of this package is you can handle request separately from the service part.

💈 Full Example

// request.go
type Request struct {
	gohandler.BaseRequest
}

// overrided method
func (r Request) GetSchema() interface{} {
	return r.RequestSchema.(*CustomRequest)
}

type CustomRequest struct {
	Username string  `json:"username" validate:"required"`
}

func NewRequest() gohandler.IRequest {
	return Request{
		gohandler.BaseRequest{
			RequestSchema: new(CustomRequest),
		},
	}
}

// controller.go
type MyCustomHandler struct {
}

func (receiver MyCustomHandler) LocalBinding(ctx *fiber.Ctx) error {
	fmt.Println("you can bind parameters to context")
	return nil
}

func Get(ctx *fiber.Ctx) error {
	h := gohandler.New(ctx)
	h.WithHandler(MyCustomHandler{})   //optional
	h.WithRequest(myCustomRequest.New()) //optional

  // it will call your method, will bind ctx as parameters
	return h.Handle(myIsolatedService.New(), "Get") 
}

🧭 Quick Note

You can bind your struct to Fiber's Locals on LocalBinding method. For example;

func (h Handler) LocalBinding(ctx *fiber.Ctx) error {
	id := ctx.Params("id") // get id from param
  
	myStruct, _ := repository.New().FindByID(id) // find row from db
  
	ctx.Locals("myStruct", &myStruct) // bind your data to locals
  
	return nil
}

📫  Have a question? Want to chat? Ran into a problem?

Website yahyahindioglu.com
LinkedIn Account

License

This project is available under the MIT license.

Documentation ¶

Index ¶

Constants ¶

View Source
const Parameters = "parameters"

Variables ¶

This section is empty.

Functions ¶

This section is empty.

Types ¶

type BaseHandler ¶

type BaseHandler struct {
	Request IRequest
	C       *fiber.Ctx
	Handler IHandler
}

func (BaseHandler) CallServiceMethod ¶

func (h BaseHandler) CallServiceMethod(service interface{}, method string) error

func (BaseHandler) GetValueOfC ¶

func (h BaseHandler) GetValueOfC() []reflect.Value

func (BaseHandler) Handle ¶

func (h BaseHandler) Handle(service interface{}, method string) error

func (BaseHandler) WithHandler ¶

func (h BaseHandler) WithHandler(handler IHandler) IBaseHandler

func (BaseHandler) WithRequest ¶

func (h BaseHandler) WithRequest(request IRequest) BaseHandler

type BaseRequest ¶

type BaseRequest struct {
	RequestSchema interface{}
}

func (BaseRequest) AfterValidate ¶

func (receiver BaseRequest) AfterValidate(ctx *fiber.Ctx) error

func (BaseRequest) BeforeValidate ¶

func (receiver BaseRequest) BeforeValidate(ctx *fiber.Ctx) error

func (BaseRequest) BindRequest ¶

func (receiver BaseRequest) BindRequest(ctx *fiber.Ctx)

func (BaseRequest) GetSchema ¶

func (receiver BaseRequest) GetSchema() interface{}

func (BaseRequest) Validate ¶

func (receiver BaseRequest) Validate() ([]*RequestError, error)

func (BaseRequest) Validation ¶

func (receiver BaseRequest) Validation(ctx *fiber.Ctx) *ValidationError

type Handler ¶

type Handler struct {
}

func (Handler) LocalBinding ¶

func (h Handler) LocalBinding(ctx *fiber.Ctx) error

type IBaseHandler ¶

type IBaseHandler interface {
	Handle(service interface{}, method string) error
	WithRequest(request IRequest) BaseHandler
	GetValueOfC() []reflect.Value
	CallServiceMethod(service interface{}, method string) error
	WithHandler(handler IHandler) IBaseHandler
}

func New ¶

func New(ctx *fiber.Ctx) IBaseHandler

type IHandler ¶

type IHandler interface {
	LocalBinding(ctx *fiber.Ctx) error
}

type IRequest ¶

type IRequest interface {
	Validation(ctx *fiber.Ctx) *ValidationError
	BeforeValidate(ctx *fiber.Ctx) error
	Validate() ([]*RequestError, error)
	AfterValidate(ctx *fiber.Ctx) error
	GetSchema() interface{}
	BindRequest(ctx *fiber.Ctx)
}

type RequestError ¶

type RequestError struct {
	FailedField string
	Tag         string
	Value       string
	FieldValue  interface{}
}

type ValidationError ¶

type ValidationError struct {
	StatusCode int
	Data       []*RequestError
	Err        error
}

func BuildException ¶

func BuildException(code int, err error, data ...*RequestError) *ValidationError

func UnprocessableEntityException ¶

func UnprocessableEntityException(errorList []*RequestError) *ValidationError

Directories ¶

Path Synopsis
examples

Jump to

Keyboard shortcuts

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