context

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2023 License: MIT Imports: 21 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var InvalidMessage string // key type
View Source
var InvalidStatusCode int // token无效时的状态码
View Source
var SessionEnable = false

SessionEnable 开启Session

View Source
var SessionTimeout = 1200

SessionTimeout 自动过期时间,单位:秒

Functions

func ClearSession added in v0.8.0

func ClearSession()

ClearSession 移除过期的Session对象

func InitJwt added in v0.9.0

func InitJwt()

InitJwt jwt初始化

func NewRouteRegexp added in v0.8.0

func NewRouteRegexp(tpl string, typ RegexpType, options RouteRegexpOptions) *routeRegexp

NewRouteRegexp 构建支持正则的路由

Types

type HttpContext

type HttpContext struct {
	Request          *HttpRequest                                   // Request
	Response         *HttpResponse                                  // Response
	Header           collections.ReadonlyDictionary[string, string] // 头部信息
	Cookie           *HttpCookies                                   // Cookies信息
	Session          *HttpSession                                   // Session信息
	Route            *HttpRoute                                     // 路由信息
	URI              *HttpURL                                       // URL信息
	Data             *HttpData                                      // 用于传递值
	Method           string                                         // 客户端提交时的Method
	ContentLength    int64                                          // 客户端提交时的内容长度
	ContentType      string                                         // 客户端提交时的内容类型
	Exception        any                                            // 是否发生异常
	Jwt              *HttpJwt                                       // jwt验证
	Close            bool
	TransferEncoding []string
}

func NewHttpContext

func NewHttpContext(httpRoute *HttpRoute, w http.ResponseWriter, r *http.Request) *HttpContext

NewHttpContext 初始化上下文

func (*HttpContext) IsActionResult

func (httpContext *HttpContext) IsActionResult() bool

IsActionResult 是否为ActionResult类型

func (*HttpContext) ParseParams added in v0.8.0

func (httpContext *HttpContext) ParseParams() []reflect.Value

ParseParams 转换成Handle函数需要的参数

type HttpCookies added in v0.8.0

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

func (*HttpCookies) Get added in v0.8.0

func (r *HttpCookies) Get(name string) *http.Cookie

Get 获取Cookie

func (*HttpCookies) GetValue added in v0.8.0

func (r *HttpCookies) GetValue(name string) string

GetValue 获取Cookie

func (*HttpCookies) Remove added in v0.8.0

func (r *HttpCookies) Remove(name string)

Remove 删除Cookie

func (*HttpCookies) SetCookie added in v0.8.0

func (r *HttpCookies) SetCookie(cookie *http.Cookie)

SetCookie 设置Cookie

func (*HttpCookies) SetSuretyValue added in v0.8.0

func (r *HttpCookies) SetSuretyValue(name string, val string)

SetSuretyValue 设置Cookie安全值,将不允许脚本读取该值(HttpOnly)

func (*HttpCookies) SetValue added in v0.8.0

func (r *HttpCookies) SetValue(name string, val string)

SetValue 设置Cookie

type HttpData added in v0.9.0

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

func (*HttpData) Get added in v0.9.0

func (receiver *HttpData) Get(key string) any

Get 获取值

func (*HttpData) Set added in v0.9.0

func (receiver *HttpData) Set(key string, val any)

Set 设置值

type HttpJwt added in v0.9.0

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

func (*HttpJwt) Build added in v0.9.0

func (receiver *HttpJwt) Build(claims map[string]any) (string, error)

Build 生成jwt token,并写入head

func (*HttpJwt) Clear added in v0.9.0

func (receiver *HttpJwt) Clear()

清除token

func (*HttpJwt) GetClaims added in v0.9.0

func (receiver *HttpJwt) GetClaims() jwt.MapClaims

GetClaims 读取前端提交过来的Claims

func (*HttpJwt) GetToken added in v0.9.0

func (receiver *HttpJwt) GetToken() string

GetToken 获取前端提交过来的Token

func (*HttpJwt) Valid added in v0.9.0

func (receiver *HttpJwt) Valid() bool

Valid 验证前端提交过来的token是否正确

type HttpRequest

type HttpRequest struct {
	Body       io.ReadCloser
	BodyString string
	BodyBytes  []byte
	Form       map[string]any
	Query      map[string]any
	Params     []reflect.Value // 转换成Handle函数需要的参数
	R          *http.Request
}

func (*HttpRequest) ParseForm

func (r *HttpRequest) ParseForm()

ParseForm 解析来自form的值

func (*HttpRequest) ParseQuery

func (r *HttpRequest) ParseQuery()

ParseQuery 解析来自url的值

type HttpResponse

type HttpResponse struct {
	W         http.ResponseWriter
	Body      []any  // Action执行的结果(Action返回值)
	BodyBytes []byte // 自定义输出结果
	// contains filtered or unexported fields
}

func (*HttpResponse) AddHeader

func (receiver *HttpResponse) AddHeader(key, value string)

AddHeader 添加头部

func (*HttpResponse) DelHeader

func (receiver *HttpResponse) DelHeader(key string)

DelHeader 删除头部

func (*HttpResponse) GetHttpCode added in v0.9.0

func (receiver *HttpResponse) GetHttpCode() int

GetHttpCode 获取响应的HttpCode

func (*HttpResponse) GetStatusCode added in v0.9.0

func (receiver *HttpResponse) GetStatusCode() int

GetStatusCode 获取statusCode

func (*HttpResponse) GetStatusMessage added in v0.9.0

func (receiver *HttpResponse) GetStatusMessage() string

GetStatusMessage 获取statusMessage

func (*HttpResponse) Reject added in v0.9.0

func (receiver *HttpResponse) Reject(httpCode int, content string)

Reject 拒绝服务

func (*HttpResponse) SetHeader

func (receiver *HttpResponse) SetHeader(key, value string)

SetHeader 覆盖头部

func (*HttpResponse) SetHttpCode added in v0.9.0

func (receiver *HttpResponse) SetHttpCode(httpCode int)

SetHttpCode 将响应状态写入http流

func (*HttpResponse) SetMessage added in v0.9.0

func (receiver *HttpResponse) SetMessage(statusCode int, statusMessage string)

SetMessage 设计响应提示信息

func (*HttpResponse) SetStatusCode added in v0.9.0

func (receiver *HttpResponse) SetStatusCode(statusCode int)

SetStatusCode 设置StatusCode

func (*HttpResponse) SetValues added in v0.9.0

func (receiver *HttpResponse) SetValues(callValues ...reflect.Value)

SetValues 设置Body值

func (*HttpResponse) Write

func (receiver *HttpResponse) Write(content []byte)

Write 将响应内容写入http流

func (*HttpResponse) WriteJson added in v0.9.0

func (receiver *HttpResponse) WriteJson(content any)

WriteJson 将响应内容转成json后写入http流

func (*HttpResponse) WriteString added in v0.9.0

func (receiver *HttpResponse) WriteString(content string)

WriteString 将响应内容写入http流

type HttpRoute

type HttpRoute struct {
	RouteUrl            string                         // 路由地址
	Controller          reflect.Type                   // 控制器类型
	ControllerName      string                         // 控制器名称
	Action              any                            // action类型
	ActionName          string                         // action名称
	RequestParamType    collections.List[reflect.Type] // 入参
	ResponseBodyType    collections.List[reflect.Type] // 出参
	Method              collections.List[string]       // method
	ParamNames          collections.List[string]       // 入参变量名称(显示指定)
	RequestParamIsModel bool                           // 是否为DTO结构
	ResponseBodyIsModel bool                           // 是否为DTO结构
	AutoBindHeaderName  string                         // 自动绑定header的字段名称
	IsImplActionFilter  bool                           // 是否实现了IActionFilter
	IsGoBasicType       bool                           // 返回值只有一个时,是否为基础类型
	HttpMiddleware      IMiddleware                    // 中间件入口(每个路由的管道都不一样)
	HandleMiddleware    IMiddleware                    // handle中间件
	RouteRegexp         *routeRegexp                   // 正则路由
	Handler             http.Handler                   // api处理函数
	Filters             []IFilter                      // 过滤器(对单个路由的执行单元)
}

HttpRoute 路由表

func (*HttpRoute) FormToParams added in v0.8.0

func (receiver *HttpRoute) FormToParams(mapVal map[string]any) []reflect.Value

FormToParams 将map转成入参值

func (*HttpRoute) JsonToParams added in v0.8.0

func (receiver *HttpRoute) JsonToParams(request *HttpRequest) []reflect.Value

JsonToParams json入参转成param

type HttpSession added in v0.8.0

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

func InitSession added in v0.8.0

func InitSession(w http.ResponseWriter, r *http.Request) *HttpSession

InitSession 初始化httpSession

func (*HttpSession) Clear added in v0.8.0

func (r *HttpSession) Clear()

Clear 清空Session

func (*HttpSession) GetValue added in v0.8.0

func (r *HttpSession) GetValue(name string) any

GetValue 获取Session

func (*HttpSession) Remove added in v0.8.0

func (r *HttpSession) Remove(name string)

Remove 删除Session

func (*HttpSession) SetValue added in v0.8.0

func (r *HttpSession) SetValue(name string, val any)

SetValue 设置Session

type HttpURL

type HttpURL struct {
	Url             string // 请求地址 http(s)://xxx.xxx.xxx/xxx
	Path            string // 请求地址
	RemoteAddr      string // 客户端IP端口
	X_Forwarded_For string // 客户端IP端口
	X_Real_Ip       string // 客户端IP端口
	Host            string // 请求的Host主机头
	Proto           string // http协议
	RequestURI      string
	QueryString     string
	Query           map[string]any

	R *http.Request
}

func (*HttpURL) GetRealIp added in v0.9.0

func (receiver *HttpURL) GetRealIp() string

GetRealIp 获取真实IP

func (*HttpURL) ParseQuery added in v0.9.0

func (r *HttpURL) ParseQuery()

type IFilter added in v0.9.0

type IFilter interface {
	// OnActionExecuting 页面执行前执行
	OnActionExecuting(httpContext *HttpContext)
	// OnActionExecuted 页面执行后执行
	OnActionExecuted(httpContext *HttpContext)
}

IFilter 过滤器

type IMiddleware added in v0.3.0

type IMiddleware interface {
	Invoke(httpContext *HttpContext)
}

IMiddleware 中间件

type RegexpType added in v0.8.0

type RegexpType int
const (
	RegexpTypePath RegexpType = iota
	RegexpTypeHost
	RegexpTypePrefix
	RegexpTypeQuery
)

type RouteRegexpOptions added in v0.8.0

type RouteRegexpOptions struct {
	StrictSlash    bool
	UseEncodedPath bool
}

Jump to

Keyboard shortcuts

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