easygin

package module
v1.1.0 Latest Latest
Warning

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

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

README

EasyGin

EasyGin is a Go library that provides a wrapper for the Gin framework, aiming to make working with the Gin framework easier and more convenient. By offering additional features and functionalities, EasyGin helps you build web applications based on Gin more quickly.

Features

  • Automatic Parameter Parsing and Injection: EasyGin automatically parses incoming requests and injects the parameters into your controller functions, allowing you to focus on writing business logic without worrying about parsing request data.
  • Unified Error Handling: EasyGin offers a unified error handling mechanism, allowing you to handle and respond to errors in a consistent and structured way across your application.
  • Graceful Server Shutdown: EasyGin provides a graceful server shutdown mechanism, ensuring that active connections are completed before the server shuts down, preventing data loss or abrupt termination.
  • Runtime Profile Collection: EasyGin includes runtime profiling functionality, allowing you to collect performance profiles of your application during runtime for analysis and optimization.

Examples

server := easygin.NewWithEngine(gin.New())
server.GET("/test", func(ctx *gin.Context) *easygin.Response {
    resp, err := service()
    if err != nil {
        if easygin.IsRespError(err) {
            return easygin.Fail(easygin.AsRespError(err))
        }
        return easygin.Fail(easygin.NewFromError(err))
    }

    return easygin.OkData(resp)
})

server.ListenAndServe(":8080")

 

request parameters can also be added to the controller, and easygin will automatically parse and inject the data in the request

type User struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

server.GET("/test", func(ctx *gin.Context, user *User) *Response {
    ...
})

Installation

Use go mod for dependency management:

go get github.com/mangohow/easygin

Documentation

Index

Constants

View Source
const (
	UnknownErrorCode = -1
	SuccessCode      = 0
)
View Source
const (
	ContentTypeJson = "application/json"
)
View Source
const DefaultMemProfileRate = 4096

Variables

View Source
var (
	RespSuccess = RespError(&RespErrorImpl{
		Codee:    SuccessCode,
		Messagee: "success",
	})
)

Functions

func IsRespError

func IsRespError(err error) bool

func IsSuccess

func IsSuccess(err RespError) bool

func SetLogOutput

func SetLogOutput(out io.Writer)

func SetupSignalHandler

func SetupSignalHandler() context.Context

Types

type EasyGin

type EasyGin struct {
	*gin.Engine
	Server *http.Server
	// contains filtered or unexported fields
}

func New

func New() *EasyGin

func NewWithEngine

func NewWithEngine(r *gin.Engine) *EasyGin

func (*EasyGin) DELETE

func (e *EasyGin) DELETE(relativePath string, handlers ...Handler)

func (*EasyGin) GET

func (e *EasyGin) GET(relativePath string, handlers ...Handler)

func (*EasyGin) Group

func (e *EasyGin) Group(relativePath string, handlers ...Handler) *RouterGroup

func (*EasyGin) HEAD

func (e *EasyGin) HEAD(relativePath string, handlers ...Handler)

func (*EasyGin) ListenAndServe

func (e *EasyGin) ListenAndServe(addr string) error

func (*EasyGin) PATCH

func (e *EasyGin) PATCH(relativePath string, handlers ...Handler)

func (*EasyGin) POST

func (e *EasyGin) POST(relativePath string, handlers ...Handler)

func (*EasyGin) PUT

func (e *EasyGin) PUT(relativePath string, handlers ...Handler)

func (*EasyGin) SetAfterCloseHandlers

func (e *EasyGin) SetAfterCloseHandlers(handlers ...func())

SetAfterCloseHandlers register handlers which will be called after server closed

func (*EasyGin) SetMaxGraceDuration

func (e *EasyGin) SetMaxGraceDuration(max time.Duration)

func (*EasyGin) SetSignalHandler

func (e *EasyGin) SetSignalHandler(f func() context.Context)

SetSignalHandler set signal processing functions if the signal processing function is set when calling this method, the registered afterCloseHandlers will not work

type Handler

type Handler interface{}

Handler must be in one of the following forms func(ctx *gin.Context) *Response func(ctx *gin.Context, u UserType) *Response must have one or two parameter first param: must be *gin.Context second param: must be a struct or a pointer of struct return value must be *Response

type Profile

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

func (*Profile) Stop

func (p *Profile) Stop()

type RespError

type RespError interface {
	error
	Message() string
	Code() int
}

func AsRespError

func AsRespError(err error) RespError

func NewError

func NewError(code int, msg string) RespError

func NewErrorf

func NewErrorf(code int, format string, args ...interface{}) RespError

func NewFromError

func NewFromError(err error) RespError

type RespErrorImpl

type RespErrorImpl struct {
	Codee    int    `json:"code"`
	Messagee string `json:"message"`
}

RespErrorImpl e is added at the end because we need to export the field and it cannot have the same name as the function

func (*RespErrorImpl) Code

func (e *RespErrorImpl) Code() int

func (*RespErrorImpl) Error

func (e *RespErrorImpl) Error() string

func (*RespErrorImpl) Message

func (e *RespErrorImpl) Message() string

type RespValue

type RespValue struct {
	RespError
	Data interface{} `json:"data"`
}

func (*RespValue) MarshalJSON

func (r *RespValue) MarshalJSON() ([]byte, error)

type Response

type Response struct {
	R      RespValue
	Status int
}

Response controller返回值, 不要自己创建, 一定要调用函数来获取

func Error

func Error(status int) *Response

func Fail

func Fail(respErr RespError) *Response

func FailData

func FailData(respErr RespError, data interface{}) *Response

func NewResponse

func NewResponse(status int, data interface{}, respErr RespError) *Response

func Ok

func Ok() *Response

func OkCode

func OkCode(code int) *Response

func OkCodeData

func OkCodeData(code int, data interface{}) *Response

func OkData

func OkData(data interface{}) *Response

type RouterGroup

type RouterGroup struct {
	*gin.RouterGroup
}

func (*RouterGroup) DELETE

func (r *RouterGroup) DELETE(relativePath string, handlers ...Handler)

func (*RouterGroup) GET

func (r *RouterGroup) GET(relativePath string, handlers ...Handler)

func (*RouterGroup) HEAD

func (r *RouterGroup) HEAD(relativePath string, handlers ...Handler)

func (*RouterGroup) PATCH

func (r *RouterGroup) PATCH(relativePath string, handlers ...Handler)

func (*RouterGroup) POST

func (r *RouterGroup) POST(relativePath string, handlers ...Handler)

func (*RouterGroup) PUT

func (r *RouterGroup) PUT(relativePath string, handlers ...Handler)

type Stopper

type Stopper interface {
	Stop()
}

func StartProfile

func StartProfile() Stopper

Jump to

Keyboard shortcuts

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