echoext

package module
v2.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2023 License: Apache-2.0 Imports: 13 Imported by: 0

README

基于 echo 的扩展

说明

对echo(v4)进行了功能的整合扩展,主要特点:

  • 使用泛型封装了请求及返回,可以更方便的写 swag 注释,示例见 main.go
  • 添加 form-data 类型的 binder,支持文件上传的绑定,示例见 file_binder_test.go
  • 仿照 springboot 的默认错误结构重写 ErrorHandler

安装

go get -u github.com/wjhdec/echo-ext/v2

使用示例

main.go

测试

http://localhost:8181/my-test/sum?v1=1&v2=10

测试错误:

http://localhost:8181/my-test/demo-error

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CustomHttpErrorHandler

func CustomHttpErrorHandler(err error, c echo.Context)

func NewFileBinder added in v2.1.0

func NewFileBinder(baseBinder echo.Binder) echo.Binder

Types

type Author

type Author interface {
	Auth() echo.MiddlewareFunc
}

type ErrResponse

type ErrResponse struct {
	Timestamp time.Time `json:"timestamp"`
	Status    int       `json:"status"`
	Error     string    `json:"error"`
	Message   string    `json:"message"`
	Path      string    `json:"path"`
}

func NewErrResponse

func NewErrResponse(err *echo.HTTPError, c echo.Context) *ErrResponse

type HandlerEnable

type HandlerEnable interface {
	HandlerFunc() echo.HandlerFunc
	Method() string
	Path() string
	Middlewares() []echo.MiddlewareFunc
}

func NewHttpHandler

func NewHttpHandler[T any](path, method string, f func(ctx echo.Context, req T) (handler http.Handler, err error), middleware ...echo.MiddlewareFunc) HandlerEnable

NewHttpHandler 支持 httpHandler 扩展

func NewJsonHandler

func NewJsonHandler[T, R any](path, method string, f func(ctx echo.Context, req T) (resp R, err error), middleware ...echo.MiddlewareFunc) HandlerEnable

NewJsonHandler 返回json

func NewJsonStatusHandler

func NewJsonStatusHandler[T, R any](path, method string, successStatus int, f func(ctx echo.Context, req T) (resp R, err error), middleware ...echo.MiddlewareFunc) HandlerEnable

NewJsonStatusHandler 返回带状态json

func NewNoContentHandler

func NewNoContentHandler[T any](path, method string, f func(ctx echo.Context, req T) (err error), middleware ...echo.MiddlewareFunc) HandlerEnable

NewNoContentHandler 无返回

func NewStreamHandler

func NewStreamHandler[T any](path, method string, f func(ctx echo.Context, req T) (header http.Header, reader io.ReadCloser, err error), middleware ...echo.MiddlewareFunc) HandlerEnable

NewStreamHandler 返回流,用于下载等

type Options

type Options struct {
	Name     string `json:"name,omitempty"`
	Version  string `json:"version,omitempty"`
	BasePath string `json:"base_path,omitempty" yaml:"base-path,omitempty"`
	Port     int    `json:"port,omitempty"`
	TLSKey   string `json:"tls_key,omitempty" yaml:"tls-key,omitempty"`
	TLSPem   string `json:"tls_pem,omitempty" yaml:"tls-pem,omitempty"`
	// contains filtered or unexported fields
}

func (*Options) GetAuthor

func (o *Options) GetAuthor(key string) Author

func (*Options) SetAuthor

func (o *Options) SetAuthor(key string, author Author) *Options

func (*Options) SetAuthorMap

func (o *Options) SetAuthorMap(authorMap map[string]Author) *Options

func (*Options) SetName

func (o *Options) SetName(name string) *Options

func (*Options) SetVersion

func (o *Options) SetVersion(version string) *Options

type Router

type Router interface {
	AddHandler(handler ...HandlerEnable)
	Register() error
}

func NewRouter

func NewRouter(group *echo.Group) Router

type RouterFnc

type RouterFnc func(group *ServerGroup) (Router, error)

type Server

type Server struct {
	// contains filtered or unexported fields
}

func NewServer

func NewServer(opt *Options) (*Server, error)

func (*Server) AddMiddleware

func (s *Server) AddMiddleware(middleware ...echo.MiddlewareFunc)

func (*Server) AddRouterFnc

func (s *Server) AddRouterFnc(fncs ...RouterFnc) error

func (*Server) Echo

func (s *Server) Echo() *echo.Echo

func (*Server) Options

func (s *Server) Options() *Options

func (*Server) RegisterRouters

func (s *Server) RegisterRouters()

func (*Server) RootGroup

func (s *Server) RootGroup() *echo.Group

func (*Server) Run

func (s *Server) Run()

type ServerGroup

type ServerGroup struct {
	*echo.Group
	Server *Server
}

func (*ServerGroup) Author

func (s *ServerGroup) Author(key string) Author

func (*ServerGroup) SubGroup

func (s *ServerGroup) SubGroup(prefix string, funcs ...echo.MiddlewareFunc) *echo.Group

SubGroup 创建子组

Jump to

Keyboard shortcuts

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