ginmodule

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StatusCodeSuccess            = 200
	StatusCodeServiceUnavailable = 10000 - iota
	StatusCodeExceededLimit
	StatusCodeTimeout
	StatusCodeException
	StatusCodeNotFound
	StatusCodeForbidden
	StatusCodeAuthorityUnavailable

	StatusCodeMethodNotAllowed = 9007 - iota
	StatusCodeMediaTypeNotAllowed
	StatusCodeUploadLimitExceeded
	StatusCodeUnauthorized
	StatusCodeBadRequestParameters
)

Variables

This section is empty.

Functions

func ErrorCodeHandler

func ErrorCodeHandler() gin.HandlerFunc

func Recover

func Recover() gin.HandlerFunc

Types

type BizErrorCode

type BizErrorCode int

type BizErrorMessage

type BizErrorMessage string

type GinModule

type GinModule struct {

	// 自定义Module配置
	GinModuleConfig *declaration.ModuleConfig
	GinInterceptor  func(instance *gin.Engine)

	// * 注册业务路由
	Routers []Router

	// * 注册服务监听地址 :8080 (默认)
	ListenAddress string // ip:port

	UseErrorCodeHandler bool // 使用错误包装处理器 在出现非200响应码或者异常时,将自动进行转化

	// gin config
	DebugModule                  bool
	MaxMultipartMemory           int64
	DisableMethodNotAllowedError bool
	ForwardedByClientIP          bool
}

func (*GinModule) ModuleConfig

func (g *GinModule) ModuleConfig() *declaration.ModuleConfig

func (*GinModule) Register

func (g *GinModule) Register() (interface{}, error)

func (*GinModule) Unregister

func (g *GinModule) Unregister(maxWaitSeconds uint) (gracefully bool, err error)

type GinWrapper

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

func (*GinWrapper) DELETE

func (g *GinWrapper) DELETE(path string, handler ...HandlerWrapper)

func (*GinWrapper) GET

func (g *GinWrapper) GET(path string, handler ...HandlerWrapper)

func (*GinWrapper) HEAD

func (g *GinWrapper) HEAD(path string, handler ...HandlerWrapper)

func (*GinWrapper) MATCH

func (g *GinWrapper) MATCH(method []string, path string, handler ...HandlerWrapper)

func (*GinWrapper) OPTIONS

func (g *GinWrapper) OPTIONS(path string, handler ...HandlerWrapper)

func (*GinWrapper) PATCH

func (g *GinWrapper) PATCH(path string, handler ...HandlerWrapper)

func (*GinWrapper) POST

func (g *GinWrapper) POST(path string, handler ...HandlerWrapper)

func (*GinWrapper) PUT

func (g *GinWrapper) PUT(path string, handler ...HandlerWrapper)

func (*GinWrapper) TRACE

func (g *GinWrapper) TRACE(path string, handler ...HandlerWrapper)

type HandlerWrapper

type HandlerWrapper func(request *Request) (*Response, error)

type Request

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

func (*Request) BindBodyForm

func (r *Request) BindBodyForm(object any)

BindBodyForm 将请求body表单数据绑定到from结构体中 任何异常将触发panic响应请求参数错误 `form:""`

func (*Request) BindBodyJson

func (r *Request) BindBodyJson(object any)

BindBodyJson 将请求body数据绑定到json结构体中 任何异常将触发panic响应请求参数错误 `json:""`

func (*Request) BindUriPathParams

func (r *Request) BindUriPathParams(object any)

BindUriPathParams 绑定结构体用于接收UriPath参数 任何异常将触发panic响应请求参数错误 `uri:""`

func (*Request) BindUriQueryParams

func (r *Request) BindUriQueryParams(object any)

BindUriQueryParams 绑定结构体用于接收UriQuery参数 任何异常将触发panic响应请求参数错误 `form:""`

func (*Request) FormFile

func (r *Request) FormFile(name string) *multipart.FileHeader

FormFile 获取上传文件内容 任何异常将触发panic响应请求参数错误 request name: form name

func (*Request) FullPath

func (r *Request) FullPath() string

FullPath 获取请求全路径

func (*Request) HeaderValue

func (r *Request) HeaderValue(key string) string

HeaderValue 获取Head name对应的参数值

func (*Request) HttpMethod

func (r *Request) HttpMethod() string

HttpMethod 获取请求方法

func (*Request) RawData

func (r *Request) RawData() []byte

RawData 将请求body以字节数据返回 任何异常将触发panic响应请求参数错误

func (*Request) RawDataString

func (r *Request) RawDataString() string

RawDataString 将原始请求的body以字符串数据返回 任何异常将触发panic响应请求参数错误

func (*Request) RawGinContext

func (r *Request) RawGinContext() *gin.Context

RawGinContext 获取原始Gin上下文

func (*Request) RequestIP

func (r *Request) RequestIP() string

RequestIP 尝试获取请求方客户端IP

func (*Request) SaveUploadedFile

func (r *Request) SaveUploadedFile(name string, dirPath string, filename ...string)

SaveUploadedFile 保存上传的文件内容 任何异常将触发panic响应请求参数错误 request name: form name

dirPath: 保存的路径 (文件夹)
filename: 保存的文件名 若不指定则为源文件名

func (*Request) ShouldBindBodyForm

func (r *Request) ShouldBindBodyForm(object any)

ShouldBindBodyForm 将请求body表单数据绑定到from结构体中

func (*Request) ShouldBindBodyJson

func (r *Request) ShouldBindBodyJson(object any)

ShouldBindBodyJson 将请求body数据绑定到json结构体中

func (*Request) ShouldBindUriQueryParams

func (r *Request) ShouldBindUriQueryParams(object any)

ShouldBindUriQueryParams 绑定结构体用于接收UriQuery参数

func (*Request) UriPathParam

func (r *Request) UriPathParam(name string) string

UriPathParam 获取path路径参数 /:id/

func (*Request) UriPathParams

func (r *Request) UriPathParams(names ...string) map[string]string

func (*Request) UriQueryParam

func (r *Request) UriQueryParam(name string) (string, bool)

UriQueryParam 获取 uri Query参数值 /?a=b&c=d return string: 参数值(可能是类型零值) bool: 请求方是否传递

func (*Request) UriQueryParamArray

func (r *Request) UriQueryParamArray(name string) ([]string, bool)

UriQueryParamArray 获取 uri Query参数值 /?a=b&c=d

func (*Request) UriQueryParamMap

func (r *Request) UriQueryParamMap(name string) (map[string]string, bool)

func (*Request) UriQueryParams

func (r *Request) UriQueryParams(names ...string) map[string]string

type Response

type Response struct {
	Status *Status `json:"status"`
	Data   any     `json:"data"`
}

func ResponseBizError

func ResponseBizError(bizErrorCode *BizErrorCode, bizErrorMessage *BizErrorMessage) *Response

func ResponseError

func ResponseError(statusCode StatusCode, statusMessage ...StatusMessage) *Response

ResponseError 其他StatusCode错误

func ResponseException

func ResponseException() *Response

ResponseException 系统异常响应

func ResponseSuccess

func ResponseSuccess(data ...any) *Response

type Router

type Router interface {
	RouterInfo() *RouterInfo
	RegisterHandler(ginWrapper *GinWrapper)
}

type RouterInfo

type RouterInfo struct {
	GroupPath string
	// 如果指定基于BasicAuth认证的账户,则该GroupPath下资源将需要权限认证 如果不满足验证规则,则会返回相应的httpStatus错误码,并且不会被本框架包装
	BasicAuthAccount map[string]string
}

type Status

type Status struct {
	StatusCode      StatusCode       `json:"statusCode"`
	StatusMessage   StatusMessage    `json:"statusMessage"`
	BizErrorCode    *BizErrorCode    `json:"bizErrorCode"`
	BizErrorMessage *BizErrorMessage `json:"bizErrorMessage"`
	Timestamp       int64            `json:"timestamp"`
}

type StatusCode

type StatusCode int

type StatusMessage

type StatusMessage string

func GetStatusMessage

func GetStatusMessage(statusCode StatusCode) StatusMessage

Jump to

Keyboard shortcuts

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