framework

package
v0.0.0-...-d316e16 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

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

Context 自定义 Context

func NewContext

func NewContext(r *http.Request, w http.ResponseWriter) *Context

func (*Context) BaseContext

func (ctx *Context) BaseContext() context.Context

func (*Context) BindJson

func (ctx *Context) BindJson(obj interface{}) error

BindJson 将body文本解析到obj结构体中

func (*Context) BindJsonTest

func (ctx *Context) BindJsonTest(obj interface{}) error

func (*Context) BindXml

func (ctx *Context) BindXml(obj interface{}) error

BindXml xml body

func (*Context) ClientIp

func (ctx *Context) ClientIp() string

ClientIp 获取客户端ip

func (*Context) Cookie

func (ctx *Context) Cookie(key string) (string, bool)

Cookie 获取Cookie的key值

func (*Context) Cookies

func (ctx *Context) Cookies() map[string]string

Cookies 获取Cookies

func (*Context) Deadline

func (ctx *Context) Deadline() (deadline time.Time, ok bool)

func (*Context) Done

func (ctx *Context) Done() <-chan struct{}

func (*Context) Err

func (ctx *Context) Err() error

func (*Context) Form

func (ctx *Context) Form(key string) interface{}

Form 获取表单任意值

func (*Context) FormAll

func (ctx *Context) FormAll() map[string][]string

FormAll 获取表单数据

func (*Context) FormAllTest

func (ctx *Context) FormAllTest() map[string][]string

FormAll 提取所有表单参数

func (*Context) FormBool

func (ctx *Context) FormBool(key string, def bool) (bool, bool)

func (*Context) FormFile

func (ctx *Context) FormFile(key string) (*multipart.FileHeader, error)

FormFile 获取上传文件

func (*Context) FormFloat32

func (ctx *Context) FormFloat32(key string, def float32) (float32, bool)

func (*Context) FormFloat64

func (ctx *Context) FormFloat64(key string, def float64) (float64, bool)

func (*Context) FormInt

func (ctx *Context) FormInt(key string, def int) (int, bool)

FormInt 获取表单的int数据

func (*Context) FormInt64

func (ctx *Context) FormInt64(key string, def int64) (int64, bool)

FormInt64 获取表单的int64数据

func (*Context) FormIntTest

func (ctx *Context) FormIntTest(key string, def int) int

func (*Context) FormString

func (ctx *Context) FormString(key string, def string) (string, bool)

func (*Context) FormStringSlice

func (ctx *Context) FormStringSlice(key string, def []string) ([]string, bool)

func (*Context) FormStringTest

func (ctx *Context) FormStringTest(key string, def string) string

func (*Context) GetRawData

func (ctx *Context) GetRawData() ([]byte, error)

GetRawData 其他格式

func (*Context) GetRequest

func (ctx *Context) GetRequest() *http.Request

func (*Context) GetResponse

func (ctx *Context) GetResponse() http.ResponseWriter

func (*Context) HTML

func (ctx *Context) HTML(status int, obj interface{}, template string) error

func (*Context) HasTimeout

func (ctx *Context) HasTimeout() bool

func (*Context) Header

func (ctx *Context) Header(key string) (string, bool)

func (*Context) Headers

func (ctx *Context) Headers() map[string][]string

Headers 获取Headers

func (*Context) Host

func (ctx *Context) Host() string

Host 获取Host

func (*Context) Html

func (ctx *Context) Html(file string, obj interface{}) IResponse

Html 输出

func (*Context) Json

func (ctx *Context) Json(obj interface{}) IResponse

func (*Context) JsonTest

func (ctx *Context) JsonTest(status int, obj interface{}) error

func (*Context) Jsonp

func (ctx *Context) Jsonp(obj interface{}) IResponse

Jsonp 输出

func (*Context) Method

func (ctx *Context) Method() string

Method 获取方法名

func (*Context) Next

func (ctx *Context) Next() error

Next 执行handlers的下一个方法(中间件+控制器), 通过移动index控制请求调用链

func (*Context) Param

func (ctx *Context) Param(key string) interface{}

Param 获取路由参数

func (*Context) ParamBool

func (ctx *Context) ParamBool(key string, def bool) (bool, bool)

func (*Context) ParamFloat32

func (ctx *Context) ParamFloat32(key string, def float32) (float32, bool)

func (*Context) ParamFloat64

func (ctx *Context) ParamFloat64(key string, def float64) (float64, bool)

func (*Context) ParamInt

func (ctx *Context) ParamInt(key string, def int) (int, bool)

ParamInt 路由匹配中带的参数 形如 /book/:id

func (*Context) ParamInt64

func (ctx *Context) ParamInt64(key string, def int64) (int64, bool)

func (*Context) ParamString

func (ctx *Context) ParamString(key string, def string) (string, bool)

func (*Context) Query

func (ctx *Context) Query(key string) interface{}

Query 获取任意值Query的值

func (*Context) QueryAll

func (ctx *Context) QueryAll() map[string][]string

QueryAll 获取请求地址中所有参数

func (*Context) QueryAllTest

func (ctx *Context) QueryAllTest() map[string][]string

QueryAll 提取所有query参数 map[string][]string: http://example.com/search?tag=science&tag=technology&tag=math result:map["tag"][]string{"science", "technology", "math"}

func (*Context) QueryArray

func (ctx *Context) QueryArray(key string, def []string) []string

QueryArray 获取query的指定key值

func (*Context) QueryBool

func (ctx *Context) QueryBool(key string, def bool) (bool, bool)

QueryBool 获取query的布尔值

func (*Context) QueryFloat32

func (ctx *Context) QueryFloat32(key string, def float32) (float32, bool)

QueryFloat32 获取query的转为float32数据

func (*Context) QueryFloat64

func (ctx *Context) QueryFloat64(key string, def float64) (float64, bool)

QueryFloat64 获取query的转我float64数据

func (*Context) QueryInt

func (ctx *Context) QueryInt(key string, def int) (int, bool)

QueryInt 获取Int类型的请求参数

func (*Context) QueryInt64

func (ctx *Context) QueryInt64(key string, def int64) (int64, bool)

QueryInt64 获取请求中的参数转为int64

func (*Context) QueryIntTest

func (ctx *Context) QueryIntTest(key string, def int) int

QueryInt 获取query指定key的int值

func (*Context) QueryString

func (ctx *Context) QueryString(key string, def string) (string, bool)

QueryString 获取query的字符串数据

func (*Context) QueryStringSlice

func (ctx *Context) QueryStringSlice(key string, def []string) ([]string, bool)

QueryStringSlice 将参数转为切片

func (*Context) QueryStringTest

func (ctx *Context) QueryStringTest(key string, def string) string

QueryString 获取query指定key的string值

func (*Context) Redirect

func (ctx *Context) Redirect(path string) IResponse

Redirect 重定向

func (*Context) SetCookie

func (ctx *Context) SetCookie(key string, val string, maxAge int, path string, domain string, secure bool, httpOnly bool) IResponse

SetCookie 设置cookie

func (*Context) SetHandlers

func (ctx *Context) SetHandlers(handlers []ControllerHandler)

SetHandlers 将涉及到的中间件和控制器加入ctx中

func (*Context) SetHasTimeout

func (ctx *Context) SetHasTimeout()

func (*Context) SetHeader

func (ctx *Context) SetHeader(key string, val string) IResponse

SetHeader 设置header

func (*Context) SetOkStatus

func (ctx *Context) SetOkStatus() IResponse

SetOkStatus 设置200状态

func (*Context) SetParams

func (ctx *Context) SetParams(params map[string]string)

func (*Context) SetStatus

func (ctx *Context) SetStatus(code int) IResponse

SetStatus 设置状态码

func (*Context) Text

func (ctx *Context) Text(format string, values ...interface{}) IResponse

Text

func (*Context) TextTest

func (ctx *Context) TextTest(status int, obj string) error

func (*Context) Uri

func (ctx *Context) Uri() string

Uri 获取uri

func (*Context) Value

func (ctx *Context) Value(key interface{}) interface{}

func (*Context) WriterMux

func (ctx *Context) WriterMux() *sync.Mutex

func (*Context) Xml

func (ctx *Context) Xml(obj interface{}) IResponse

Xml 输出

type ControllerHandler

type ControllerHandler func(c *Context) error

func TimeoutHandler

func TimeoutHandler(function ControllerHandler, d time.Duration) ControllerHandler

type Core

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

Core 框架核心结构 框架的核心,负责路由匹配,server接口实现,初始路由,注册方法

func NewCore

func NewCore() *Core

NewCore 初始化对象Core

func (*Core) Delete

func (c *Core) Delete(url string, handlers ...ControllerHandler)

Delete DELETE方法路由注册

func (*Core) FindRouteByRequest

func (c *Core) FindRouteByRequest(request *http.Request) []ControllerHandler

FindRouteByRequest 匹配路由,如果没有匹配到,返回nil

func (*Core) FindRouteNodeByRequest

func (c *Core) FindRouteNodeByRequest(request *http.Request) *node

FindRouteNodeByRequest 匹配路由,如果没有匹配到,返回nil

func (*Core) Get

func (c *Core) Get(url string, handlers ...ControllerHandler)

Get GET方法路由注册

func (*Core) Group

func (c *Core) Group(prefix string) IGroup

func (*Core) Post

func (c *Core) Post(url string, handlers ...ControllerHandler)

Post POST方法路由注册

func (*Core) Put

func (c *Core) Put(url string, handlers ...ControllerHandler)

Put PUT方法路由注册

func (*Core) ServeHTTP

func (c *Core) ServeHTTP(response http.ResponseWriter, request *http.Request)

ServeHTTP 框架核心结构实现了Handler接口 所有请求都进入这个函数, 这个函数负责路由分发

func (*Core) Use

func (c *Core) Use(middlewares ...ControllerHandler)

Use 注册中间件

type Group

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

Group 前缀匹配的具体实现者

func NewGroup

func NewGroup(core *Core, prefix string) *Group

func (*Group) Delete

func (g *Group) Delete(uri string, handler ...ControllerHandler)

func (*Group) Get

func (g *Group) Get(uri string, handlers ...ControllerHandler)

func (*Group) Group

func (g *Group) Group(uri string) IGroup

Group 实现 Group 方法

func (*Group) Post

func (g *Group) Post(uri string, handlers ...ControllerHandler)

func (*Group) Put

func (g *Group) Put(uri string, handlers ...ControllerHandler)

func (*Group) Use

func (g *Group) Use(middlewares ...ControllerHandler)

Use 注册中间件

type IGroup

type IGroup interface {
	Get(string, ...ControllerHandler)
	Post(string, ...ControllerHandler)
	Put(string, ...ControllerHandler)
	Delete(string, ...ControllerHandler)
	Use(middlewares ...ControllerHandler)
	Group(uri string) IGroup
}

IGroup 代表前缀分组

type IRequest

type IRequest interface {
	// 请求地址 url 中带的参数
	// 形如: foo.com?a=1&b=bar&c[]=bar
	QueryInt(key string, def int) (int, bool)
	QueryInt64(key string, def int64) (int64, bool)
	QueryFloat64(key string, def float64) (float64, bool)
	QueryFloat32(key string, def float32) (float32, bool)
	QueryBool(key string, def bool) (bool, bool)
	QueryString(key string, def string) (string, bool)
	QueryStringSlice(key string, def []string) ([]string, bool)
	Query(key string) interface{}

	ParamInt(key string, def int) (int, bool)
	ParamInt64(key string, def int64) (int64, bool)
	ParamFloat64(key string, def float64) (float64, bool)
	ParamFloat32(key string, def float32) (float32, bool)
	ParamBool(key string, def bool) (bool, bool)
	ParamString(key string, def string) (string, bool)
	Param(key string) interface{}

	// form 表单中带的参数
	FormInt(key string, def int) (int, bool)
	FormInt64(key string, def int64) (int64, bool)
	FormFloat64(key string, def float64) (float64, bool)
	FormFloat32(key string, def float32) (float32, bool)
	FormBool(key string, def bool) (bool, bool)
	FormString(key string, def string) (string, bool)
	FormStringSlice(key string, def []string) ([]string, bool)
	FormFile(key string) (*multipart.FileHeader, error)
	Form(key string) interface{}

	// json body
	BindJson(obj interface{}) error

	// xml body
	BindXml(obj interface{}) error

	// 其他格式
	GetRawData() ([]byte, error)

	// 基础信息
	Uri() string
	Method() string
	Host() string
	ClientIp() string

	// header
	Headers() map[string][]string
	Header(key string) (string, bool)

	// cookie
	Cookies() map[string]string
	Cookie(key string) (string, bool)
}

IRequest 代表请求包含的方法

type IResponse

type IResponse interface {
	// Json输出
	Json(obj interface{}) IResponse

	// Jsonp输出
	Jsonp(obj interface{}) IResponse

	//xml输出
	Xml(obj interface{}) IResponse

	// html输出
	Html(file string, obj interface{}) IResponse

	// string
	Text(format string, values ...interface{}) IResponse

	// 重定向
	Redirect(path string) IResponse

	// header
	SetHeader(key string, val string) IResponse

	// Cookie
	SetCookie(key string, val string, maxAge int, path, domain string, secure, httpOnly bool) IResponse

	// 设置状态码
	SetStatus(code int) IResponse

	// 设置200状态
	SetOkStatus() IResponse
}

IResponse 代表返回方法

type Tree

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

Tree 前缀树

func NewTree

func NewTree() *Tree

func (*Tree) AddRouter

func (tree *Tree) AddRouter(uri string, handlers []ControllerHandler) error

AddRouter 增加路由节点, 路由节点有先后顺序

/book/list /book/:id (冲突) /book/:id/name /book/:student/age /:user/name(冲突) /:user/name/:age

func (*Tree) FindHandler

func (tree *Tree) FindHandler(uri string) []ControllerHandler

FindHandler 匹配uri

Directories

Path Synopsis
gin
Package gin implements a HTTP web framework called gin.
Package gin implements a HTTP web framework called gin.

Jump to

Keyboard shortcuts

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