context

package
v1.1.13 Latest Latest
Warning

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

Go to latest
Published: May 29, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UserBindKey = "bind_user"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	Writer  http.ResponseWriter
	Request *http.Request
	// contains filtered or unexported fields
}

Context 封装请求上下文

func New

New 创建新的上下文

func (*Context) BadRequest

func (c *Context) BadRequest(message string)

BadRequest 返回400错误

func (*Context) Bind

func (c *Context) Bind(obj interface{}) error

Bind 绑定请求数据到结构体

func (*Context) BindUser

func (c *Context) BindUser(val interface{})

BindUser 绑定用户信息

func (*Context) Error

func (c *Context) Error(code int, message string)

Error 返回错误响应

func (*Context) File

func (c *Context) File(filepath string)

File 返回文件下载

func (*Context) FindBool

func (c *Context) FindBool(key string) bool

FindBool 查找布尔参数

func (*Context) FindDefaultBool

func (c *Context) FindDefaultBool(key string, defaultValue bool) bool

FindDefaultBool 查找布尔参数,支持默认值

func (*Context) FindDefaultInt

func (c *Context) FindDefaultInt(key string, defaultValue int) int

FindDefaultInt 查找整数参数,支持默认值

func (*Context) FindDefaultString

func (c *Context) FindDefaultString(key string, defaultValue string) string

FindDefaultString 查找字符串参数,支持默认值

func (*Context) FindInt

func (c *Context) FindInt(key string) int

FindInt 查找整数参数

func (*Context) FindString

func (c *Context) FindString(key string) string

FindString 查找字符串参数

func (*Context) Forbidden

func (c *Context) Forbidden(message string)

Forbidden 返回403错误

func (*Context) Get

func (c *Context) Get(key string) (interface{}, bool)

Get 从上下文中获取值

func (*Context) GetBindUser

func (c *Context) GetBindUser(recipient interface{}) error

GetBindUser 获取绑定的用户信息

func (*Context) GetString

func (c *Context) GetString(key string) string

GetString 获取字符串值

func (*Context) HTML

func (c *Context) HTML(code int, html string)

HTML 返回HTML响应

func (*Context) InternalServerError

func (c *Context) InternalServerError(message string)

InternalServerError 返回500错误

func (*Context) JSON

func (c *Context) JSON(code int, data interface{})

JSON 返回JSON响应

func (*Context) NotFound

func (c *Context) NotFound(message string)

NotFound 返回404错误

func (*Context) Redirect

func (c *Context) Redirect(code int, location string)

Redirect 重定向

func (*Context) Set

func (c *Context) Set(key string, value interface{})

Set 在上下文中存储值

func (*Context) String

func (c *Context) String(code int, format string, values ...interface{})

String 返回文本响应

func (*Context) Success

func (c *Context) Success(data interface{})

Success 返回成功响应

func (*Context) Unauthorized

func (c *Context) Unauthorized(message string)

Unauthorized 返回401错误

func (*Context) Valid

func (c *Context) Valid(obj interface{}) error

Valid 验证结构体

func (*Context) ValidField

func (c *Context) ValidField(rules Rules) error

ValidField 验证单个字段

type Response

type Response struct {
	Code    int         `json:"code"`              // 状态码
	Message string      `json:"message,omitempty"` // 消息
	Data    interface{} `json:"data,omitempty"`    // 数据
}

Response 标准响应结构

type ResponseType

type ResponseType int

ResponseType 定义响应类型

const (
	// ResponseTypeJSON JSON响应类型
	ResponseTypeJSON ResponseType = iota
	// ResponseTypeXML XML响应类型
	ResponseTypeXML
	// ResponseTypeHTML HTML响应类型
	ResponseTypeHTML
	// ResponseTypeText 文本响应类型
	ResponseTypeText
)

type Rules

type Rules map[string][]string

Rules 定义验证规则

Jump to

Keyboard shortcuts

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