in

package
v0.0.0-...-080604a Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrConfigNotFound       = errors.New("config not found")
	ErrorConfigTypeNotMatch = errors.New("config type not match")
)

Functions

func EndpointToHandleFunc

func EndpointToHandleFunc(endpoint Endpoint) echo.HandlerFunc

EndpointToHandleFunc converts an Endpoint to an echo.HandlerFunc

func GetUrlParam

func GetUrlParam[T any](ctx *Context, key string) T

GetUrlParam returns the value of the url param with the given key.

func NewLogger

func NewLogger(ctx context.Context) *logrus.Entry

NewLogger creates a new logger with a new request ID

func SetLogFormat

func SetLogFormat(fm *nested.Formatter)

SetLogFormat sets the log format This function must be called before the first call to NewLogger

func ToHandleFunc

func ToHandleFunc(fn Endpoint) echo.HandlerFunc

ToHandleFunc converts an Endpoint to an echo.HandlerFunc

Types

type Config

type Config interface {
	GetPort() int
	GetTimeout() time.Duration
	GetRootPath() string
	GetProperty(string) any
}

Config is the configuration of the application.

type Context

type Context struct {
	echo.Context
	Log Logger
	// contains filtered or unexported fields
}

Context is a wrapper for echo.Context

func NewContext

func NewContext(logger Logger, ctx echo.Context) *Context

func (*Context) GetConfig

func (c *Context) GetConfig() (Config, error)

GetConfig returns the config from the context

func (*Context) NoContent

func (c *Context) NoContent()

NoContent send a 204 status code

func (*Context) Ok

func (c *Context) Ok()

Ok send a 200 status code

func (*Context) ParseFormBody

func (c *Context) ParseFormBody(target any) error

ParseFormBody parse the form body and set the values to the target and the target must be a pointer to a struct. The struct fields must have the tag "form" with the name of the form field or the name of the struct field in lowercase. Example:

type User struct {
	Name string `form:"name"`
	Age  int    `form:"age"`
}

var user User
err := c.ParseFormBody(&user)

if err != nil {
	// handle error
}

func (*Context) ParseJsonBody

func (c *Context) ParseJsonBody(target any) error

ParseJsonBody parse the json body and set the values to the target and the target must be a pointer to a struct. The struct fields must have the tag "json" with the name of the json field or the name of the struct field in lowercase. Example:

type User struct {
	Name string `json:"name"`
	Age  int    `json:"age"`
}

var user User
err := c.ParseJsonBody(&user)

if err != nil {
	// handle error
}

func (*Context) SendStatus

func (c *Context) SendStatus(httpstatus int)

SendStatus send a status code with a default status message

func (*Context) StatusWithBody

func (c *Context) StatusWithBody(httpstatus int, response any)

StatusWithBody send a status code with a body

func (*Context) Stop

func (c *Context) Stop()

Stop break the context

func (*Context) WriteJson

func (c *Context) WriteJson(response any)

WriteJson write a json response

type Controller

type Controller struct {
	Path        string
	Middlewares []echo.MiddlewareFunc
	Endpoint    []Endpoint
}

Controller is a struct that contains a group of endpoints and a path to be used in the server

func (*Controller) Build

func (c *Controller) Build(e *echo.Echo)

Build is a method that will build all endpoints and add them to the server

type Endpoint

type Endpoint struct {
	Path        string
	Method      string
	Middlewares []echo.MiddlewareFunc
	Handler     Handler
}

Endpoint is a struct that contains a path, a method, a handler and a list of middlewares that will be used in the server

type Handler

type Handler func(ctx *Context) error

Handler is the function that will be executed when the endpoint is called.

type Logger

type Logger interface {
	Info(args ...any)
	Warn(args ...any)
	Error(args ...any)
}

Logger is the interface that wraps the basic logging methods.

type OnError

type OnError interface {
	OnError(ctx context.Context, err error)
}

OnError is the interface that wraps the basic error handling method.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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