Documentation ¶
Index ¶
- func BuildHttpResponse(code string, flag string, data any, msg string) []byte
- func GetRequestRange(r *http.Request, maxSize int64) (start, end int64, hasRange bool)
- func GetURLParam(r *http.Request, paramName string) string
- func ParseHTTPRequest(r *http.Request, obj any) error
- func RangeWrite(w http.ResponseWriter, sa io.ReadSeeker, start, end, maxSize int64, ...)
- func SendBadRequest(w http.ResponseWriter, msg string)
- func SendBusinessError(w http.ResponseWriter, err error)
- func SendBusinessErrorAndCode(w http.ResponseWriter, errCode string, msg string)
- func SendErrorResponse(w http.ResponseWriter, statusCode int, errCode string, msg string)
- func SendForbidden(w http.ResponseWriter, msg string)
- func SendServerError(w http.ResponseWriter, msg string)
- func SendSuccess(w http.ResponseWriter, data any)
- func SendSuccessResponse(w http.ResponseWriter, statusCode int, bizCode string, data any)
- func SendUnauthorized(w http.ResponseWriter, msg string)
- func WirteFile(w http.ResponseWriter, r *http.Request, path string)
- type AsRouter
- type BusinessError
- type FilterFunc
- type HTTPResponse
- type HTTPService
- func (service *HTTPService) Any(url string, fun HandlerFunc) error
- func (service *HTTPService) AsRouter(url string, router AsRouter) error
- func (service *HTTPService) BulkRouters(url string, toLowerCase bool, routers [][]any) error
- func (service *HTTPService) Delete(url string, fun HandlerFunc) error
- func (service *HTTPService) Filter(url string, fun FilterFunc) error
- func (service *HTTPService) Get(url string, fun HandlerFunc) error
- func (service *HTTPService) GetRouter() *ServiceRouter
- func (service *HTTPService) Head(url string, fun HandlerFunc) error
- func (service *HTTPService) Options(url string, fun HandlerFunc) error
- func (service *HTTPService) Patch(url string, fun HandlerFunc) error
- func (service *HTTPService) Post(url string, fun HandlerFunc) error
- func (service *HTTPService) Put(url string, fun HandlerFunc) error
- func (service *HTTPService) SetStaticDIR(path, dir string, fun FilterFunc) (err error)
- func (service *HTTPService) SetStaticFile(path, file string, fun FilterFunc) error
- func (service *HTTPService) StartHTTP(conf StartHTTPConf) error
- type HandlerEntry
- type HandlerFunc
- type RouterConfig
- type RuntimeErrorHandlerFunc
- type ServiceRouter
- func (srt *ServiceRouter) AddHandler(method, url string, handler HandlerFunc) error
- func (srt *ServiceRouter) AddURLFilter(url string, filter FilterFunc) error
- func (srt *ServiceRouter) ClearHandlersMap()
- func (srt *ServiceRouter) EnableURLParam(enable bool)
- func (srt *ServiceRouter) RemoveFilter(url string)
- func (srt *ServiceRouter) RemoveHandler(method, url string)
- func (srt *ServiceRouter) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (srt *ServiceRouter) SetDebug(isDebug bool)
- func (srt *ServiceRouter) SetDefaultFilter(globalFilter FilterFunc)
- func (srt *ServiceRouter) SetDefaultHandler(defaultHandler HandlerFunc)
- func (srt *ServiceRouter) SetRuntimeErrorHandler(h RuntimeErrorHandlerFunc)
- type StartHTTPConf
- type URLFilterEntry
- type URLMatcher
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildHttpResponse ¶
BuildHttpResponse 构建返回json
func GetRequestRange ¶
GetRequestRange 解析http分段头信息
func GetURLParam ¶ added in v0.0.14
GetURLParam 从请求上下文中获取URL参数值 paramName: 参数名 返回参数值, 如果参数不存在则返回空字符串
func RangeWrite ¶
func RangeWrite(w http.ResponseWriter, sa io.ReadSeeker, start, end, maxSize int64, hasRange bool)
RangeWrite 范围写入http, 如文件分段传输
func SendBadRequest ¶
func SendBadRequest(w http.ResponseWriter, msg string)
SendBadRequest 返回400错误, code=BAD_REQUEST
func SendBusinessError ¶ added in v0.0.8
func SendBusinessError(w http.ResponseWriter, err error)
SendBusinessError 返回业务错误 httpCode=200, code值为默认为BUSINESS_ERROR, 若error实现了CustomError接口, 则使用CustomError.ErrorCode值
func SendBusinessErrorAndCode ¶ added in v0.0.8
func SendBusinessErrorAndCode(w http.ResponseWriter, errCode string, msg string)
SendBusinessErrorAndCode 返回业务错误 httpCode=200, code=errCode参数
func SendErrorResponse ¶ added in v0.0.8
func SendErrorResponse(w http.ResponseWriter, statusCode int, errCode string, msg string)
SendErrorResponse 返回失败结果
func SendForbidden ¶ added in v0.0.8
func SendForbidden(w http.ResponseWriter, msg string)
SendForbidden 返回403错误, code=FORBIDDEN
func SendServerError ¶
func SendServerError(w http.ResponseWriter, msg string)
SendServerError 返回500错误, code=SERVER_ERROR
func SendSuccess ¶
func SendSuccess(w http.ResponseWriter, data any)
SendSuccess 返回成功结果 httpCode=200, code=OK
func SendSuccessResponse ¶ added in v0.0.8
func SendSuccessResponse(w http.ResponseWriter, statusCode int, bizCode string, data any)
SendSuccessResponse 返回成功结果
func SendUnauthorized ¶ added in v0.0.8
func SendUnauthorized(w http.ResponseWriter, msg string)
SendUnauthorized 返回401错误, code=UNAUTHORIZED
Types ¶
type BusinessError ¶ added in v0.0.8
BusinessError 业务异常
type FilterFunc ¶
type FilterFunc func(http.ResponseWriter, *http.Request) bool
FilterFunc http请求过滤器, 返回bool, true: 继续, false: 停止
type HTTPResponse ¶
type HTTPResponse struct { Code string `json:"code"` Flag string `json:"flag"` Message string `json:"message"` Data any `json:"data"` }
HTTPResponse 接口返回格式约束
func Parse2HTTPResponse ¶
func Parse2HTTPResponse(str string) *HTTPResponse
Parse2HTTPResponse json转对象
type HTTPService ¶
type HTTPService struct {
*ServiceRouter
}
HTTPService HTTP服务
func (*HTTPService) Any ¶
func (service *HTTPService) Any(url string, fun HandlerFunc) error
Any Any
func (*HTTPService) AsRouter ¶
func (service *HTTPService) AsRouter(url string, router AsRouter) error
AsRouter 批量注册路由, 指定一个前缀url
func (*HTTPService) BulkRouters ¶
func (service *HTTPService) BulkRouters(url string, toLowerCase bool, routers [][]any) error
BulkRouters 批量注册路由, 指定一个前缀url routers: 需要注册的处理函数 [{"Method(GET|POST...)", "HandlerFunc function"}, {"Method(GET|POST...)", "指定的url(可选参数)", "HandlerFunc function"}]
func (*HTTPService) Delete ¶
func (service *HTTPService) Delete(url string, fun HandlerFunc) error
Delete Delete
func (*HTTPService) Filter ¶
func (service *HTTPService) Filter(url string, fun FilterFunc) error
Filter Filter
func (*HTTPService) Get ¶
func (service *HTTPService) Get(url string, fun HandlerFunc) error
Get Get
func (*HTTPService) GetRouter ¶
func (service *HTTPService) GetRouter() *ServiceRouter
GetRouter 获取路由实例, 可作为http的Server.Handler实例
func (*HTTPService) Head ¶
func (service *HTTPService) Head(url string, fun HandlerFunc) error
Head Head
func (*HTTPService) Options ¶
func (service *HTTPService) Options(url string, fun HandlerFunc) error
Options Options
func (*HTTPService) Patch ¶
func (service *HTTPService) Patch(url string, fun HandlerFunc) error
Patch Patch
func (*HTTPService) Post ¶
func (service *HTTPService) Post(url string, fun HandlerFunc) error
Post Post
func (*HTTPService) Put ¶
func (service *HTTPService) Put(url string, fun HandlerFunc) error
Put Put
func (*HTTPService) SetStaticDIR ¶
func (service *HTTPService) SetStaticDIR(path, dir string, fun FilterFunc) (err error)
SetStaticDIR SetStaticDIR
func (*HTTPService) SetStaticFile ¶
func (service *HTTPService) SetStaticFile(path, file string, fun FilterFunc) error
SetStaticFile SetStaticFile
func (*HTTPService) StartHTTP ¶
func (service *HTTPService) StartHTTP(conf StartHTTPConf) error
StartHTTP 启动服务, 默认实现, 根据需要可重写
type HandlerEntry ¶ added in v0.0.14
type HandlerEntry struct {
// contains filtered or unexported fields
}
HandlerEntry 添加新的结构体定义
type RouterConfig ¶
type RouterConfig struct { // ToLowerCase 是否需要url转小写, 在未指定url(使用函数名字作为url的一部分)的情况下生效 ToLowerCase bool // HandlerFunc 需要注册的函数 [{"Method(GET|POST...)", "HandlerFunc function"}, {"Method(GET|POST...)", "指定的url(可选参数)", "HandlerFunc function"}] HandlerFunc [][]any }
RouterConfig 批量注册器
type RuntimeErrorHandlerFunc ¶
type RuntimeErrorHandlerFunc func(http.ResponseWriter, *http.Request, any)
RuntimeErrorHandlerFunc 未知异常处理函数
type ServiceRouter ¶
type ServiceRouter struct {
// contains filtered or unexported fields
}
ServiceRouter 实现了 http.Server 接口的 ServeHTTP 方法, 提供 URL 路由管理功能。 请求处理逻辑: 过滤器 > 路径匹配 > END
func NewServiceRouter ¶ added in v0.0.14
func NewServiceRouter() (router *ServiceRouter)
NewServiceRouter New service router
func (*ServiceRouter) AddHandler ¶
func (srt *ServiceRouter) AddHandler(method, url string, handler HandlerFunc) error
AddHandler 添加URL处理器, 如: POST /api/:*, GET /api/:id
func (*ServiceRouter) AddURLFilter ¶
func (srt *ServiceRouter) AddURLFilter(url string, filter FilterFunc) error
AddURLFilter 添加URL过滤器, 如: /api/**
func (*ServiceRouter) ClearHandlersMap ¶
func (srt *ServiceRouter) ClearHandlersMap()
ClearHandlersMap 清空所有注册的处理器
func (*ServiceRouter) EnableURLParam ¶ added in v0.0.14
func (srt *ServiceRouter) EnableURLParam(enable bool)
EnableURLParam 设置是否启用URL参数注入到请求上下文中
func (*ServiceRouter) RemoveFilter ¶
func (srt *ServiceRouter) RemoveFilter(url string)
RemoveFilter 移除指定URL的过滤器
func (*ServiceRouter) RemoveHandler ¶
func (srt *ServiceRouter) RemoveHandler(method, url string)
RemoveHandler 移除指定URL和方法的处理器
func (*ServiceRouter) ServeHTTP ¶
func (srt *ServiceRouter) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP 实现 http.Handler 接口, 处理所有 HTTP 请求
func (*ServiceRouter) SetDebug ¶
func (srt *ServiceRouter) SetDebug(isDebug bool)
SetDebug 设置是否启用调试模式, 启用后会输出详细的请求处理日志
func (*ServiceRouter) SetDefaultFilter ¶
func (srt *ServiceRouter) SetDefaultFilter(globalFilter FilterFunc)
SetDefaultFilter 设置默认的请求过滤器, 在所有特定路由过滤器之后执行
func (*ServiceRouter) SetDefaultHandler ¶
func (srt *ServiceRouter) SetDefaultHandler(defaultHandler HandlerFunc)
SetDefaultHandler 设置默认的请求处理器, 当没有匹配的路由时被调用
func (*ServiceRouter) SetRuntimeErrorHandler ¶
func (srt *ServiceRouter) SetRuntimeErrorHandler(h RuntimeErrorHandlerFunc)
SetRuntimeErrorHandler 设置运行时错误处理函数, 用于处理请求处理过程中的panic
type StartHTTPConf ¶
StartHTTPConf 启动配置
type URLFilterEntry ¶ added in v0.0.14
type URLFilterEntry struct {
// contains filtered or unexported fields
}
URLFilterEntry url过滤器结构体
type URLMatcher ¶ added in v0.0.14
type URLMatcher struct {
// contains filtered or unexported fields
}
URLMatcher URL路由匹配器
func NewURLMatcher ¶ added in v0.0.14
func NewURLMatcher(pattern string) *URLMatcher
NewURLMatcher 创建新的URL匹配器
func (*URLMatcher) GetParams ¶ added in v0.0.14
func (m *URLMatcher) GetParams(path string) map[string]string
GetParams 获取URL中的参数
func (*URLMatcher) Match ¶ added in v0.0.14
func (m *URLMatcher) Match(path string) bool
Match 检查路径是否匹配当前模式