http

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Errorf

func Errorf(format string, args ...any) error

Errorf 是一个可选辅助方法,便于把格式化错误快速转为 Validation/Fall 等上层调用。

func NewRoute

func NewRoute(cfg contracts.Config, validator contracts.Validation, storage contracts.Storage) (contracts.Route, error)

NewRoute 创建 HTTP 路由服务实例。

Types

type Response

type Response struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Data    any    `json:"data,omitempty"`
	// contains filtered or unexported fields
}

Response 是 GoFast 标准 JSON 响应结构。 它实现了 contracts.Response,可通过 ctx.Response() 获取并直接发送。

func NewResponse

func NewResponse(ctx contracts.Context) *Response

NewResponse 为当前请求上下文创建一个响应构建器。

func (*Response) Build

func (r *Response) Build(status int, code int, message string, data any) error

Build 构建并发送完整响应。

func (*Response) Created

func (r *Response) Created(data any, message ...string) error

Created 快速返回创建成功响应(HTTP 201, code=0)。

func (*Response) Download

func (r *Response) Download(file string, name string, disk ...string) error

Download 以附件下载方式输出文件,可自定义下载文件名。

func (*Response) Fail

func (r *Response) Fail(status int, message string, code ...int) error

Fail 快速返回失败响应(默认业务码 code=1)。

func (*Response) File

func (r *Response) File(file string, disk ...string) error

File 直接输出存储中的文件内容,默认使用 storage 默认磁盘。

func (*Response) Forbidden

func (r *Response) Forbidden(message ...string) error

Forbidden 快速返回无权限响应(HTTP 403)。

func (*Response) Json

func (r *Response) Json(status int, data any, message ...string) error

Json 快速返回任意 JSON 响应(业务码固定为 0)。

func (*Response) NotFound

func (r *Response) NotFound(message ...string) error

NotFound 快速返回资源不存在响应(HTTP 404)。

func (*Response) Paginate

func (r *Response) Paginate(list any, total int64, page int, size int, message ...string) error

Paginate 快速返回分页数据响应(HTTP 200, code=0)。

func (*Response) String

func (r *Response) String(s string) error

String 快速返回纯文本响应(HTTP 200)。

func (*Response) Success

func (r *Response) Success(data any, message ...string) error

Success 快速返回成功响应(HTTP 200, code=0)。

func (*Response) Unauthorized

func (r *Response) Unauthorized(message ...string) error

Unauthorized 快速返回未授权响应(HTTP 401)。

func (*Response) Validation

func (r *Response) Validation(err error, message ...string) error

Validation 快速返回参数验证失败响应(HTTP 422)。 默认 message 为“参数验证失败”,err.Error() 放入 data,便于前端调试/展示。

type ServiceProvider

type ServiceProvider struct{}

ServiceProvider HTTP 路由服务提供者。

func (*ServiceProvider) Boot

func (*ServiceProvider) Register

func (sp *ServiceProvider) Register(app foundation.Application)

Jump to

Keyboard shortcuts

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