ginstarter

package
v0.1.14 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2024 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StatusCodeSuccess            = http.StatusOK
	StatusCodeServiceUnavailable = http.StatusServiceUnavailable
	StatusCodeExceededLimit      = http.StatusTooManyRequests
	StatusCodeTimeout            = http.StatusGatewayTimeout
	StatusCodeException          = http.StatusInternalServerError
	StatusCodeNotFound           = http.StatusNotFound
	StatusCodeForbidden          = http.StatusForbidden

	StatusCodeMethodNotAllowed     = http.StatusMethodNotAllowed
	StatusCodeMediaTypeNotAllowed  = http.StatusUnsupportedMediaType
	StatusCodeUploadLimitExceeded  = http.StatusRequestEntityTooLarge
	StatusCodeUnauthorized         = http.StatusUnauthorized
	StatusCodeBadRequestParameters = http.StatusBadRequest
)

Variables

This section is empty.

Functions

func NewCommonResp

func NewCommonResp() *commonResp

NewCommonResp 创建一个普通响应

func NewRespRest

func NewRespRest() *restResp

NewRespRest 创建一个Rest响应体

func RawGinEngine added in v0.1.1

func RawGinEngine() *gin.Engine

RawGinEngine 获取原始的gin引擎实例

Types

type BadHttpCodeResolver added in v0.1.4

type BadHttpCodeResolver func(httpStatusCode int, errMsg string) Response

type BasicAuthAccount

type BasicAuthAccount struct {
	Username string
	Password string
}

type BizErrorCode

type BizErrorCode int

type BizErrorMessage

type BizErrorMessage string

type GinConfig added in v0.1.14

type GinConfig struct {

	// 模块组件在启动时执行初始化
	InitFunc func(instance *gin.Engine)

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

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

	// 默认情况系统会将捕获的异常详细发给PanicResolver处理,如果不想将细节暴露向外
	// 方案 1. 启用隐藏异常细节功能,系统将在触发panic重要错误时不再调用PanicResolver处理,并统一响应500错误
	// 方案 2. 如果不想禁用异常时调用PanicResolver, 可以在初始化时手动设置自定义PanicResolver处理器
	// * panic 将被分为框架内部错误和框架未知错误 框架内部错误是非敏感错误,不受该参数控制,每次都会触发PanicResolver,例如验证框架错误
	HidePanicErrorDetails bool
	// 全局异常响应处理器 如果不指定则使用默认方式
	PanicResolver PanicResolver

	// 禁用异常http响应码Resolver
	DisableBadHttpCodeResolver bool
	// 禁用系统内置的忽略异常响应码
	DisableDefaultIgnoreHttpCode bool
	// 启用异常http响应码Resolver 指定不处理特定的异常响应码
	IgnoreHttpCode []int
	// 启用异常http响应码Resolver 如果不指定则使用默认方式
	BadHttpCodeResolver BadHttpCodeResolver

	// 自定义全局中间件 作用于所有请求 按照顺序执行
	GlobalMiddlewares []Middleware

	// 响应数据的结构体解码器 默认为JSON方式解码
	// 在使用NewRespRest响应结构体数据时解码为[]byte数据的解码器
	// 如果自实现Response接口将不使用解码器
	ResponseDataStructDecoder ResponseDataStructDecoder

	// 尝试启用TraceId响应
	// https://github.com/acexy/golang-toolkit/blob/main/sys/threadlocal.go
	// 如果工作环境开启EnableLocalTraceId ,将自动响应TranceId头
	EnableGoroutineTraceIdResponse bool

	// ========== gin config
	DebugModule        bool
	MaxMultipartMemory int64

	// 关闭包裹405错误展示,使用404代替
	DisableMethodNotAllowedError bool

	// 禁用尝试获取转发真实IP
	DisableForwardedByClientIP bool
}

type GinStarter

type GinStarter struct {

	// GinConfig 配置
	Config GinConfig
	// 懒加载函数,用于在实际执行时动态获取配置 该权重高于Config的直接配置
	LazyConfig func() GinConfig
	// 自定义Gin模块的组件属性
	GinSetting *parent.Setting
}

func (*GinStarter) Setting

func (g *GinStarter) Setting() *parent.Setting

func (*GinStarter) Start

func (g *GinStarter) Start() (interface{}, error)

func (*GinStarter) Stop

func (g *GinStarter) Stop(maxWaitTime time.Duration) (gracefully, stopped bool, err error)

type HandlerWrapper

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

HandlerWrapper 定义内部Handler

type Middleware added in v0.1.3

type Middleware func(request *Request) (Response, bool)

func BasicAuthMiddleware added in v0.1.3

func BasicAuthMiddleware(account *BasicAuthAccount, match ...func(request *Request) bool) Middleware

BasicAuthMiddleware 基础权限校验中间件 match 满足指定条件才执行

func MediaTypeMiddleware added in v0.1.4

func MediaTypeMiddleware(contentType []string, match ...func(request *Request) bool) Middleware

MediaTypeMiddleware 类型校验中间件

type PanicResolver added in v0.1.4

type PanicResolver func(err error) string

type Request

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

func (*Request) BindBodyForm

func (r *Request) BindBodyForm(object any) error

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

func (*Request) BindBodyJson

func (r *Request) BindBodyJson(object any) error

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

func (*Request) BindPathParams

func (r *Request) BindPathParams(object any) error

BindPathParams /:id 绑定结构体用于接收UriPath参数 结构体标签格式 `uri:""`

func (*Request) BindQueryParams

func (r *Request) BindQueryParams(object any) error

BindQueryParams 绑定结构体用于接收Query参数

func (*Request) GetCookie

func (r *Request) GetCookie(name string) (string, error)

GetCookie 获取Cookie name对应的参数值

func (*Request) GetFormArray added in v0.1.11

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

GetFormArray 获取Form表单的值

func (*Request) GetFormFile

func (r *Request) GetFormFile(name string) (*multipart.FileHeader, error)

GetFormFile 获取上传文件内容

func (*Request) GetFormMap added in v0.1.11

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

GetFormMap 获取Form表单的值

func (*Request) GetFormValue added in v0.1.11

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

GetFormValue 获取Form表单的值

func (*Request) GetHeader

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

GetHeader 获取Head name对应的参数值

func (*Request) GetPathParam

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

GetPathParam 获取path路径参数 /:id

func (*Request) GetPathParams

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

GetPathParams 获取path路径参数 /:id 多个参数

func (*Request) GetQueryParam

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

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

func (*Request) GetQueryParamArray

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

GetQueryParamArray 获取 uri Query参数值 /?a=b&a=d 返回切片数据

func (*Request) GetQueryParamMap

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

GetQueryParamMap 获取 uri Query参数值 /?name[a]=1&name[b]=2 返回map类型数据

func (*Request) GetQueryParams

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

GetQueryParams 获取 uri Query参数值 /?a=b&c=d 返回map类型数据 如果目标没有传递将, map中将不包含指定的参数名

func (*Request) GetRawBodyData

func (r *Request) GetRawBodyData() ([]byte, error)

GetRawBodyData 将请求body以字节数据返回

func (*Request) GetValue added in v0.1.9

func (r *Request) GetValue(key string) (interface{}, bool)

GetValue 从gin上下文获取数据

func (*Request) Host added in v0.1.2

func (r *Request) Host() string

Host 获取Host信息

func (*Request) HttpMethod

func (r *Request) HttpMethod() string

HttpMethod 获取请求方法

func (*Request) MustBindBodyForm

func (r *Request) MustBindBodyForm(object any)

MustBindBodyForm 将请求body表单数据绑定到from结构体中 任何错误将触发Panic流程中断

func (*Request) MustBindBodyJson

func (r *Request) MustBindBodyJson(object any)

MustBindBodyJson 将请求body数据绑定到json结构体中 任何错误将触发Panic流程中断

func (*Request) MustBindPathParams

func (r *Request) MustBindPathParams(object any)

MustBindPathParams /:id 绑定结构体用于接收UriPath参数 结构体标签格式 `uri:""` 任何错误将触发Panic流程中断

func (*Request) MustBindQueryParams

func (r *Request) MustBindQueryParams(object any)

MustBindQueryParams 绑定结构体用于接收Query参数以及POST表单符合条件的数据 任何错误将触发Panic流程中断

func (*Request) MustGetCookie

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

MustGetCookie 获取Cookie name对应的参数值 任何错误将触发Panic流程中断

func (*Request) MustGetFormArray added in v0.1.11

func (r *Request) MustGetFormArray(name string) []string

MustGetFormArray 获取Form表单的值 任何错误将触发Panic流程中断

func (*Request) MustGetFormFile

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

MustGetFormFile 获取上传文件内容 任何错误将触发Panic流程中断

func (*Request) MustGetFormMap added in v0.1.11

func (r *Request) MustGetFormMap(name string) map[string]string

MustGetFormMap 获取Form表单的值 任何错误将触发Panic流程中断

func (*Request) MustGetFormValue added in v0.1.11

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

MustGetFormValue 获取Form表单的值 任何错误将触发Panic流程中断

func (*Request) MustGetQueryParam

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

MustGetQueryParam 获取 uri Query参数值 /?a=b&c=d 如果没有发送指定参数将触发异常中断流程

func (*Request) MustGetQueryParamArray

func (r *Request) MustGetQueryParamArray(name string) []string

MustGetQueryParamArray 获取 uri Query参数值 /?a=b&a=d 返回切片数据 如果参数未设置将触发异常中断流程

func (*Request) MustGetQueryParamMap

func (r *Request) MustGetQueryParamMap(name string) map[string]string

MustGetQueryParamMap 获取 uri Query参数值 /?name[a]=1&name[b]=2 返回map类型数据 如果参数未设置将触发异常中断流程

func (*Request) MustGetQueryParams

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

MustGetQueryParams 获取 uri Query参数值 /?a=b&c=d 返回map类型数据 任何一个指定的参数没有传递将触发异常中断流程

func (*Request) MustGetRawBodyData

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

MustGetRawBodyData 将请求body以字节数据返回 任何错误将触发Panic流程中断

func (*Request) MustGetRawBodyString added in v0.1.10

func (r *Request) MustGetRawBodyString() string

MustGetRawBodyString 将请求body以字符串返回 任何错误将触发Panic流程中断

func (*Request) MustSaveUploadedFile

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

MustSaveUploadedFile 保存上传的文件内容 name: form name dirPath: 保存的路径 (文件夹) filename: 保存的文件名 若不指定则为源文件名 任何错误将触发Panic流程中断

func (*Request) Proto added in v0.1.2

func (r *Request) Proto() string

Proto 获取请求协议

func (*Request) RawGinContext

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

RawGinContext 获取原始Gin上下文

func (*Request) RequestFullPath added in v0.1.3

func (r *Request) RequestFullPath() string

RequestFullPath 获取请求完整路径

func (*Request) RequestIP

func (r *Request) RequestIP() string

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

func (*Request) RequestPath added in v0.1.3

func (r *Request) RequestPath() string

RequestPath 获取请求路径

func (*Request) RouterFullPath added in v0.1.3

func (r *Request) RouterFullPath() string

RouterFullPath 当前请求的注册路由路径

func (*Request) SaveUploadedFile

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

SaveUploadedFile 保存上传的文件内容 name: form name dirPath: 保存的路径 (文件夹) filename: 保存的文件名 若不指定则为源文件名

func (*Request) SetValue added in v0.1.9

func (r *Request) SetValue(key string, value interface{})

SetValue 向gin上下文绑定数据

type Response

type Response interface {

	// Data 响应的Body数据
	Data() *ResponseData
}

Response 标准响应 用户可以通过自定义实现该接口定义自己的响应结构体 也可以使用NewRespRest来创建自定义响应结构体

func RespAbortWithHttpStatusCode

func RespAbortWithHttpStatusCode(statusCode int) Response

RespAbortWithHttpStatusCode 设置响应状态码并设置忽略执行后续handler

func RespHttpStatusCode

func RespHttpStatusCode(statusCode int) Response

RespHttpStatusCode 设置响应状态码

func RespJson

func RespJson(data any, httpStatusCode ...int) Response

RespJson 响应Json数据

func RespRedirect

func RespRedirect(url string, httpStatusCode ...int) Response

RespRedirect 响应重定向

func RespRestBadParameters added in v0.1.9

func RespRestBadParameters(statusMessage ...string) Response

RespRestBadParameters 响应标准格式的Rest参数错误

func RespRestBizError

func RespRestBizError(bizErrorCode BizErrorCode, bizErrorMessage BizErrorMessage) Response

RespRestBizError 响应标准格式的Rest业务错误

func RespRestException

func RespRestException(statusMessage ...string) Response

RespRestException 响应标准格式的Rest系统异常错误

func RespRestRaw added in v0.1.10

func RespRestRaw(dataRest *RestRespStruct) Response

RespRestRaw 响应标准格式的Rest原始数据

func RespRestStatusError

func RespRestStatusError(statusCode StatusCode, statusMessage ...StatusMessage) Response

RespRestStatusError 响应标准格式的Rest状态错误

func RespRestSuccess

func RespRestSuccess(data ...any) Response

RespRestSuccess 响应标准格式的Rest成功数据

func RespRestUnAuthorized added in v0.1.9

func RespRestUnAuthorized(statusMessage ...string) Response

RespRestUnAuthorized 响应标准格式的Rest未授权错误

func RespTextPlain

func RespTextPlain(data string, httpStatusCode ...int) Response

RespTextPlain 响应Json数据

func RespToml

func RespToml(data any, httpStatusCode ...int) Response

RespToml 响应Toml数据

func RespXml

func RespXml(data any, httpStatusCode ...int) Response

RespXml 响应Xml数据

func RespYaml

func RespYaml(data any, httpStatusCode ...int) Response

RespYaml 响应Yaml数据

type ResponseCookie

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

ResponseCookie 响应Cookie

func NewCookie

func NewCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool) *ResponseCookie

type ResponseData

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

ResponseData 标准响应数据内容

func NewEmptyResponseData added in v0.1.2

func NewEmptyResponseData() *ResponseData

func NewResponseData

func NewResponseData(contentType string, body []byte) *ResponseData

func NewResponseDataWithStatusCode added in v0.1.4

func NewResponseDataWithStatusCode(contentType string, body []byte, statusCode int) *ResponseData

func (*ResponseData) AddCookie

func (r *ResponseData) AddCookie(cookie *ResponseCookie) *ResponseData

func (*ResponseData) AddCookies

func (r *ResponseData) AddCookies(cookies []*ResponseCookie) *ResponseData

func (*ResponseData) AddHeader

func (r *ResponseData) AddHeader(name, value string) *ResponseData

func (*ResponseData) AddHeaders

func (r *ResponseData) AddHeaders(headers []*ResponseHeader) *ResponseData

func (*ResponseData) SetContentType

func (r *ResponseData) SetContentType(contentType string) *ResponseData

func (*ResponseData) SetData

func (r *ResponseData) SetData(data []byte) *ResponseData

func (*ResponseData) SetStatusCode

func (r *ResponseData) SetStatusCode(statusCode int) *ResponseData

type ResponseDataStructDecoder

type ResponseDataStructDecoder interface {
	// Decode 解析响应数据
	Decode(response any) ([]byte, error)
}

ResponseDataStructDecoder 针对Response.Data() 响应的时结构体数据时的解码为[]byte功能 默认为JSON解码器 用户可以自定义实现该接口 实现自定义解码器

type ResponseHeader

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

ResponseHeader 响应头

func NewHeader

func NewHeader(name, value string) *ResponseHeader

type RestRespStatusStruct

type RestRespStatusStruct struct {

	// 标识请求系统状态 200 标识网络请求层面的成功 见StatusCode
	StatusCode    StatusCode    `json:"statusCode"`
	StatusMessage StatusMessage `json:"statusMessage"`

	// 业务错误码 仅当StatusCode为200时进入业务错误判断
	BizErrorCode    *BizErrorCode    `json:"bizErrorCode"`
	BizErrorMessage *BizErrorMessage `json:"bizErrorMessage"`

	// 系统响应时间戳
	Timestamp int64 `json:"timestamp"`
}

RestRespStatusStruct 框架默认的Rest请求状态结构

type RestRespStruct

type RestRespStruct struct {

	// 请求状态描述
	Status *RestRespStatusStruct `json:"status"`

	// 仅当StatusCode为200 无业务错误码BizErrorCode 响应成功数据
	Data any `json:"data"`
}

RestRespStruct 框架默认的Rest请求结构

func NewRestBadParameters added in v0.1.12

func NewRestBadParameters(statusMessage ...string) *RestRespStruct

NewRestBadParameters 响应标准参数错误Rest结构体

func NewRestBizError added in v0.1.10

func NewRestBizError(bizErrorCode BizErrorCode, bizErrorMessage BizErrorMessage) *RestRespStruct

NewRestBizError 响应标准业务错误Rest结构体

func NewRestException added in v0.1.10

func NewRestException(statusMessage ...string) *RestRespStruct

NewRestException 响应标准异常Rest结构体

func NewRestStatusError added in v0.1.10

func NewRestStatusError(statusCode StatusCode, statusMessage ...StatusMessage) *RestRespStruct

NewRestStatusError 响应标准错误Rest结构体

func NewRestSuccess added in v0.1.10

func NewRestSuccess(data ...interface{}) *RestRespStruct

NewRestSuccess 响应标准成功Rest结构体

func NewRestUnauthorized added in v0.1.10

func NewRestUnauthorized(statusMessage ...string) *RestRespStruct

NewRestUnauthorized 响应标准未授权Rest结构体

type Router

type Router interface {
	// Info 定义路由信息
	Info() *RouterInfo

	// Handlers 注册处理器
	Handlers(router *RouterWrapper)
}

type RouterInfo

type RouterInfo struct {
	// GroupPath 路由分组路径
	GroupPath string

	// 该Router下的中间件执行器
	Middlewares []Middleware
}

type RouterWrapper

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

RouterWrapper 定义路由包装器

func (*RouterWrapper) DELETE

func (r *RouterWrapper) DELETE(path string, handler ...HandlerWrapper)

func (*RouterWrapper) DELETE1 added in v0.1.4

func (r *RouterWrapper) DELETE1(path string, contentType []string, handler ...HandlerWrapper)

func (*RouterWrapper) GET

func (r *RouterWrapper) GET(path string, handler ...HandlerWrapper)

func (*RouterWrapper) HEAD

func (r *RouterWrapper) HEAD(path string, handler ...HandlerWrapper)

func (*RouterWrapper) MATCH

func (r *RouterWrapper) MATCH(method []string, path string, handler ...HandlerWrapper)

func (*RouterWrapper) MATCH1 added in v0.1.4

func (r *RouterWrapper) MATCH1(method []string, path string, contentType []string, handler ...HandlerWrapper)

func (*RouterWrapper) OPTIONS

func (r *RouterWrapper) OPTIONS(path string, handler ...HandlerWrapper)

func (*RouterWrapper) PATCH

func (r *RouterWrapper) PATCH(path string, handler ...HandlerWrapper)

func (*RouterWrapper) PATCH1 added in v0.1.4

func (r *RouterWrapper) PATCH1(path string, contentType []string, handler ...HandlerWrapper)

func (*RouterWrapper) POST

func (r *RouterWrapper) POST(path string, handler ...HandlerWrapper)

func (*RouterWrapper) POST1 added in v0.1.4

func (r *RouterWrapper) POST1(path string, contentType []string, handler ...HandlerWrapper)

func (*RouterWrapper) PUT

func (r *RouterWrapper) PUT(path string, handler ...HandlerWrapper)

func (*RouterWrapper) PUT1 added in v0.1.4

func (r *RouterWrapper) PUT1(path string, contentType []string, handler ...HandlerWrapper)

func (*RouterWrapper) TRACE

func (r *RouterWrapper) TRACE(path string, handler ...HandlerWrapper)

func (*RouterWrapper) TRACE1 added in v0.1.4

func (r *RouterWrapper) TRACE1(path string, contentType []string, handler ...HandlerWrapper)

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