Documentation
¶
Overview ¶
Package TigoWeb Copyright 2018 The Tigo Authors. All rights reserved.
Package TigoWeb Copyright 2018 The Tigo Authors. All rights reserved.
Package TigoWeb Copyright 2018 The Tigo Authors. All rights reserved.
Package TigoWeb Copyright 2018 The Tigo Authors. All rights reserved.
Index ¶
- Variables
- func Decrypt(src []byte, key []byte) []byte
- func Encrypt(src []byte, key []byte) string
- func InitGlobalConfig(configPath string)
- type Application
- type BaseHandler
- func (baseHandler *BaseHandler) BeforeRequest()
- func (baseHandler *BaseHandler) CheckFormBinding(obj interface{}) error
- func (baseHandler *BaseHandler) CheckJsonBinding(obj interface{}) error
- func (baseHandler *BaseHandler) CheckParamBinding(obj interface{}) error
- func (baseHandler *BaseHandler) ClearAllCookie()
- func (baseHandler *BaseHandler) ClearCookie(name string)
- func (baseHandler *BaseHandler) Connect()
- func (baseHandler *BaseHandler) Delete()
- func (baseHandler *BaseHandler) DumpHttpRequestMsg(logLevel int)
- func (baseHandler *BaseHandler) Get()
- func (baseHandler *BaseHandler) GetBody() []byte
- func (baseHandler *BaseHandler) GetCookie(name string) (value string)
- func (baseHandler *BaseHandler) GetCookieObject(name ...string) (Cookie, error)
- func (baseHandler *BaseHandler) GetCtxVal(key string) interface{}
- func (baseHandler *BaseHandler) GetHeader(name string) (value string)
- func (baseHandler *BaseHandler) GetJsonValue(key string) interface{}
- func (baseHandler *BaseHandler) GetParameter(key string) (value *ReqParams)
- func (baseHandler *BaseHandler) GetSecureCookie(name string, key ...string) (value string)
- func (baseHandler *BaseHandler) Head()
- func (baseHandler *BaseHandler) InitHandler(responseWriter http.ResponseWriter, request *http.Request)
- func (baseHandler *BaseHandler) Options()
- func (baseHandler *BaseHandler) PassJson()
- func (baseHandler *BaseHandler) Post()
- func (baseHandler *BaseHandler) Put()
- func (baseHandler *BaseHandler) Redirect(url string, expire ...time.Time)
- func (baseHandler *BaseHandler) RedirectPermanently(url string)
- func (baseHandler *BaseHandler) Render(data interface{}, templates ...string)
- func (baseHandler *BaseHandler) Response(result ...interface{})
- func (baseHandler *BaseHandler) ResponseAsHtml(result string, charset ...string)
- func (baseHandler *BaseHandler) ResponseAsJson(response interface{}, charset ...string)
- func (baseHandler *BaseHandler) ResponseAsText(result string)
- func (baseHandler *BaseHandler) SetCookie(name string, value string)
- func (baseHandler *BaseHandler) SetCookieObject(cookie Cookie)
- func (baseHandler *BaseHandler) SetCtxVal(key string, val interface{})
- func (baseHandler *BaseHandler) SetHeader(name string, value string)
- func (baseHandler *BaseHandler) SetSecureCookie(name string, value string, key ...string)
- func (baseHandler *BaseHandler) TeardownRequest()
- func (baseHandler *BaseHandler) ToJson(response interface{}) (result string)
- func (baseHandler *BaseHandler) Trace()
- type BaseResponse
- type Cookie
- type GlobalConfig
- type ReqParams
- type Response
- type UrlPattern
- type UrlPatternMidWare
Constants ¶
This section is empty.
Variables ¶
var MethodMapping = map[string]string{
"get": "Get",
"head": "Head",
"post": "Post",
"put": "Put",
"delete": "Delete",
"connect": "Connect",
"options": "Options",
"trace": "Trace",
"GET": "Get",
"HEAD": "Head",
"POST": "Post",
"PUT": "Put",
"DELETE": "Delete",
"CONNECT": "Connect",
"OPTIONS": "Options",
"TRACE": "Trace",
}
MethodMapping http请求方式的一个映射
Functions ¶
Types ¶
type Application ¶
type Application struct {
IPAddress string // IP地址
Port int // 端口
UrlPattern map[string]interface{} // url路由配置
ConfigPath string // 全局配置
}
Application web容器
type BaseHandler ¶
type BaseHandler struct {
ResponseWriter http.ResponseWriter
Request *http.Request
JsonParams map[string]interface{}
// contains filtered or unexported fields
}
BaseHandler 是Handler的基础类,开发者开发的handler继承此类
func (*BaseHandler) BeforeRequest ¶ added in v1.0.2
func (baseHandler *BaseHandler) BeforeRequest()
BeforeRequest 在每次响应HTTP请求之前执行此函数
func (*BaseHandler) CheckFormBinding ¶ added in v1.0.2
func (baseHandler *BaseHandler) CheckFormBinding(obj interface{}) error
CheckFormBinding 检查提交的form是否符合要求
func (*BaseHandler) CheckJsonBinding ¶ added in v1.0.1
func (baseHandler *BaseHandler) CheckJsonBinding(obj interface{}) error
CheckJsonBinding 检查提交的json是否符合要求
func (*BaseHandler) CheckParamBinding ¶ added in v1.0.2
func (baseHandler *BaseHandler) CheckParamBinding(obj interface{}) error
CheckParamBinding 检查提交的参数是否符合要求
func (*BaseHandler) ClearAllCookie ¶
func (baseHandler *BaseHandler) ClearAllCookie()
ClearAllCookie 清除当前path下所有的cookie
func (*BaseHandler) ClearCookie ¶
func (baseHandler *BaseHandler) ClearCookie(name string)
ClearCookie 清除当前path下的指定的cookie
func (*BaseHandler) DumpHttpRequestMsg ¶
func (baseHandler *BaseHandler) DumpHttpRequestMsg(logLevel int)
DumpHttpRequestMsg 获取http请求报文,根据logLevel值进行不同的输出
- 1: 将http报文输出到trace级别日志中
- 2: 将http报文输出到info级别日志中
- 3: 将http报文输出到warning级别日志中
- 4: 将http报文输出到error级别日志中
- others: 将http报文输出到控制台
func (*BaseHandler) GetBody ¶ added in v1.0.1
func (baseHandler *BaseHandler) GetBody() []byte
GetBody 获取HTTP报文体
func (*BaseHandler) GetCookie ¶
func (baseHandler *BaseHandler) GetCookie(name string) (value string)
GetCookie 获取cookie值,如果获取失败则返回空字符串
func (*BaseHandler) GetCookieObject ¶
func (baseHandler *BaseHandler) GetCookieObject(name ...string) (Cookie, error)
GetCookieObject 获取cookie对象,多参数输入,参数如下:
- 无参数:默认cookieName为空字符串
- 一个参数:传入的参数为cookieName
- 多个参数:传入的第一个参数为cookieName,第二个参数为加密/解密cookie所用的Key,此时认为cookie是需要进行加密/解密处理的
func (*BaseHandler) GetCtxVal ¶
func (baseHandler *BaseHandler) GetCtxVal(key string) interface{}
GetCtxVal 从上下文获取值
func (*BaseHandler) GetHeader ¶
func (baseHandler *BaseHandler) GetHeader(name string) (value string)
GetHeader 获取header
func (*BaseHandler) GetJsonValue ¶
func (baseHandler *BaseHandler) GetJsonValue(key string) interface{}
GetJsonValue 根据key获取对应的参数值,解析json数据,返回对应的value
func (*BaseHandler) GetParameter ¶
func (baseHandler *BaseHandler) GetParameter(key string) (value *ReqParams)
GetParameter 根据key获取对应的参数值
- 如果Content-Type是application/json,则直接从http的body中解析出key对应的value
- 否则,根据key直接获取value
func (*BaseHandler) GetSecureCookie ¶
func (baseHandler *BaseHandler) GetSecureCookie(name string, key ...string) (value string)
GetSecureCookie 获取加密cookie值,如果获取失败则返回空
func (*BaseHandler) InitHandler ¶
func (baseHandler *BaseHandler) InitHandler(responseWriter http.ResponseWriter, request *http.Request)
InitHandler 初始化Handler的方法
func (*BaseHandler) Redirect ¶
func (baseHandler *BaseHandler) Redirect(url string, expire ...time.Time)
Redirect 向客户端暂时重定向一个地址
func (*BaseHandler) RedirectPermanently ¶
func (baseHandler *BaseHandler) RedirectPermanently(url string)
RedirectPermanently 向客户端永久重定向一个地址
func (*BaseHandler) Render ¶
func (baseHandler *BaseHandler) Render(data interface{}, templates ...string)
Render 渲染模板,返回数据 参数解析如下:
- data:表示传入的待渲染的数据
- templates:表示模板文件的路径,接受多个模板文件
func (*BaseHandler) Response ¶
func (baseHandler *BaseHandler) Response(result ...interface{})
Response 向客户端响应一个结果
func (*BaseHandler) ResponseAsHtml ¶
func (baseHandler *BaseHandler) ResponseAsHtml(result string, charset ...string)
ResponseAsHtml 向客户端响应一个html结果,默认字符集为utf-8
func (*BaseHandler) ResponseAsJson ¶
func (baseHandler *BaseHandler) ResponseAsJson(response interface{}, charset ...string)
ResponseAsJson 向客户端响应一个Json结果,默认字符集为utf-8
func (*BaseHandler) ResponseAsText ¶
func (baseHandler *BaseHandler) ResponseAsText(result string)
ResponseAsText 向客户端响应一个Text结果
func (*BaseHandler) SetCookie ¶
func (baseHandler *BaseHandler) SetCookie(name string, value string)
SetCookie 设置cookie
func (*BaseHandler) SetCookieObject ¶
func (baseHandler *BaseHandler) SetCookieObject(cookie Cookie)
SetCookieObject 设置高级cookie选项
func (*BaseHandler) SetCtxVal ¶
func (baseHandler *BaseHandler) SetCtxVal(key string, val interface{})
SetCtxVal 在上下文中设置值
func (*BaseHandler) SetHeader ¶
func (baseHandler *BaseHandler) SetHeader(name string, value string)
SetHeader 设置header
func (*BaseHandler) SetSecureCookie ¶
func (baseHandler *BaseHandler) SetSecureCookie(name string, value string, key ...string)
SetSecureCookie 设置加密cookie
func (*BaseHandler) TeardownRequest ¶ added in v1.0.2
func (baseHandler *BaseHandler) TeardownRequest()
TeardownRequest 在每次响应HTTP请求之后执行此函数
func (*BaseHandler) ToJson ¶
func (baseHandler *BaseHandler) ToJson(response interface{}) (result string)
ToJson 将对象转化为Json字符串,转换失败则返回空字符串。 传入参数Response为一个interface,必须有成员函数Print。
type BaseResponse ¶
type BaseResponse struct {
}
BaseResponse 定义BaseResponse类,其他Json数据类继承此类,用于BaseHandler.ResponseAsJson的参数。
type Cookie ¶
type Cookie struct {
Name string
Value string
IsSecurity bool // 是否对cookie值进行加密
SecurityKey string // 加密cookie用到的key
Path string // 可选
Domain string // 可选
Expires time.Time // 可选
RawExpires string // 只有在读取Cookie时有效
// MaxAge=0 表示未指定“Max-Age”属性
// MaxAge<0 表示现在删除cookie,相当于'Max-Age:0'
// MaxAge>0 表示Max-Age属性存在并以秒为单位给出
MaxAge int
Secure bool
HttpOnly bool
Raw string
Unparsed []string // 原始文本中未解析的属性值
}
Cookie 是自定义Cookie结构体,可参看http.Cookie
func (*Cookie) ConvertFromHttpCookie ¶
ConvertFromHttpCookie 将http/Cookie转换为Cookie
func (*Cookie) GetCookieDecodeValue ¶
GetCookieDecodeValue 获取cookie解密值
- IsSecurity如果设置为false,则返回原始值
- IsSecurity如果设置为true,则返回加密后的值
如果解密失败,则抛出异常
func (*Cookie) GetCookieEncodeValue ¶
GetCookieEncodeValue 获取cookie加密值
- IsSecurity如果设置为false,则返回原始值
- IsSecurity如果设置为true,则返回加密后的值
如果加密失败,则抛出异常
func (*Cookie) SetSecurityKey ¶
SetSecurityKey 为Cookie设置SecurityKey
func (*Cookie) ToHttpCookie ¶
ToHttpCookie 转换为http/Cookie对象
type GlobalConfig ¶
type GlobalConfig struct {
IP string `json:"ip"` // IP地址
Port int `json:"port"` // 端口
Cert string `json:"cert"` // https证书路径
CertKey string `json:"cert_key"` // https密钥路径
Cookie string `json:"cookie"` // cookie加密解密的密钥
Template string `json:"template"` // 模板文件所在文件夹的路径
Log logger.LogLevel `json:"log"` // log相关属性配置
}
GlobalConfig 全局配置对象
func (*GlobalConfig) Init ¶
func (globalConfig *GlobalConfig) Init(configPath string)
Init 根据配置文件初始化全局配置变量
type ReqParams ¶
type ReqParams struct {
Value interface{}
}
ReqParams 请求参数结构体
type UrlPattern ¶
type UrlPattern struct {
UrlMapping map[string]interface{}
}
UrlPattern 是URL路由,此处存储URL映射。
func (*UrlPattern) AppendUrlPattern ¶
func (urlPattern *UrlPattern) AppendUrlPattern(uri string, v interface { Handle(http.ResponseWriter, *http.Request) })
AppendUrlPattern 向http服务挂载单个handler,注意:
- handler必须有一个Handle(http.ResponseWriter, *http.Request)函数
func (*UrlPattern) Init ¶
func (urlPattern *UrlPattern) Init()
Init 初始化url映射,遍历UrlMapping,将handler与对应的URL依次挂载到http服务上
type UrlPatternMidWare ¶
type UrlPatternMidWare struct {
Handler interface{}
// contains filtered or unexported fields
}
UrlPatternMidWare 是URL路由中间件
func (UrlPatternMidWare) Handle ¶
func (urlPatternMidWare UrlPatternMidWare) Handle(responseWriter http.ResponseWriter, request *http.Request)
Handle 封装HTTP请求的中间件,主要有以下功能:
- 1、根据反射找到挂载的handler;
- 2、调用handler的InitHandler方法;
- 3、进行HTTP请求预处理,包括判断请求方式是否合法等;
- 4、调用handler中的功能方法;
- 5、进行HTTP请求结束处理。