framework

package
v0.0.0-...-88e70e1 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2022 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Container

type Container interface {
	// Bind 绑定一个服务提供者,如果关键字凭证已经存在,会进行替换操作,相同凭证对应提供者替换为新的提供者,返回 error
	Bind(provider ServiceProvider) error
	// 判断关键字凭证是否已经绑定提供者
	IsBind(key string) bool
	// 根据关键字凭证获取一个服务
	Make(key string) (interface{}, error)
	// MustMake 根据关键字凭证获取一个服务,如果这个关键字凭证未绑定服务提供者,那么会 panic。
	// 所以在使用这个接口的时候请保证服务容器已经为这个关键字凭证绑定了服务提供者
	MustMake(key string) interface{}
	// MakeNew 根据关键字凭证获取一个服务,只是这个服务并不是单例模式的
	// 它是根据服务提供者注册的启动函数和传递的 params 参数实例化出来的
	// 这个函数在需要为不同参数启动不同实例的时候非常有用
	MakeNew(key string, params []interface{}) (interface{}, error)
}

Container 是一个服务容器,提供绑定服务和获取服务的功能

type Context

type Context struct {

	// Container
	LadeContainer
	// contains filtered or unexported fields
}

func NewContext

func NewContext(request *http.Request, response http.ResponseWriter) *Context

func (*Context) BaseContext

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

func (*Context) BindJson

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

func (*Context) BindXml

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

func (*Context) Done

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

func (*Context) Form

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

func (*Context) FormAll

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

获取Form参数的map

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)

使用request.MultipartForm

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)

func (*Context) FormInt64

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

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) GetRequest

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

func (*Context) GetResponse

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

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) Jsonp

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

Jsonp输出

func (*Context) Next

func (ctx *Context) Next() error

调用context的handler链的下一个函数

func (*Context) Param

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

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)

提取路由参数

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{}

func (*Context) QueryAll

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

使用标准库,提取url中的参数,返回map

func (*Context) QueryBool

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

func (*Context) QueryFloat32

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

func (*Context) QueryFloat64

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

func (*Context) QueryInt

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

从map中提取各类型参数,使用cast库

func (*Context) QueryInt64

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

func (*Context) QueryString

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

func (*Context) QueryStringSlice

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

func (*Context) Redirect

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

重定向

func (*Context) SetCookie

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

Cookie

func (*Context) SetHandlers

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

设置handlers

func (*Context) SetHasTimeout

func (ctx *Context) SetHasTimeout()

func (*Context) SetHeader

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

header

func (*Context) SetOkStatus

func (ctx *Context) SetOkStatus() IResponse

设置200状态

func (*Context) SetParams

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

设置路由参数map,params

func (*Context) SetStatus

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

设置状态码

func (*Context) Text

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

string

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

type Core

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

func NewCore

func NewCore() *Core

func (*Core) Delete

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

func (*Core) FindRouteByRequest

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

func (*Core) FindRouteNodeByRequest

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

寻找匹配最终节点

func (*Core) Get

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

注册各个方法的路由

func (*Core) Group

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

从core中初始化Group

func (*Core) Post

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

func (*Core) Put

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

func (*Core) ServeHTTP

func (c *Core) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Core) Use

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

设置中间件

type Group

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

func NewGroup

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

func (*Group) Delete

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

func (*Group) Get

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

func (*Group) Group

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

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)

Group设置中间件

type IGroup

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

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{}

	// 路由匹配中带的参数
	// 形如 /book/:id
	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)
}

type IResponse

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

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

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

	// html输出
	Html(template 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
}

type LadeContainer

type LadeContainer struct {
	Container // 强制要求 ladeContainer 实现 Container 接口
	// contains filtered or unexported fields
}

LadeContainer 是服务容器的具体实现

func NewLadeContainer

func NewLadeContainer() *LadeContainer

func (*LadeContainer) Bind

func (lade *LadeContainer) Bind(provider ServiceProvider) error

Bind将服务容器和关键字做了绑定

func (*LadeContainer) Make

func (lade *LadeContainer) Make(key string) (interface{}, error)

调用内部make方法

func (*LadeContainer) MakeNew

func (lade *LadeContainer) MakeNew(key string, params []interface{}) (interface{}, error)

MakeNew方式使用内部make初始化

func (*LadeContainer) MustMake

func (lade *LadeContainer) MustMake(key string) interface{}

func (*LadeContainer) NameList

func (lade *LadeContainer) NameList() []string

NameList 列出容器中所有provider的字符串凭证

func (*LadeContainer) PrintProviders

func (lade *LadeContainer) PrintProviders() []string

PrintProviders 输出服务容器中注册的关键字

type NewInstance

type NewInstance func(...interface{}) (interface{}, error)

定义所有服务容器创建服务实例的方法类型

type ServiceProvider

type ServiceProvider interface {
	// Register 在服务容器中注册了一个实例化服务的方法,是否在注册的时候就实例化这个服务,需要参考 IsDefer 接口。
	Register(Container) NewInstance
	// Boot 在调用实例化服务的时候会调用,可以把一些准备工作:基础配置,初始化参数的操作放在这个里面。
	// 如果 Boot 返回 error,整个服务实例化就会实例化失败,返回错误
	Boot(Container) error
	// IsDefer 决定是否在注册的时候实例化这个服务,如果不是注册的时候实例化,那就是在第一次 make 的时候进行实例化操作
	// false 表示不需要延迟实例化,在注册的时候就实例化。true 表示延迟实例化
	IsDefer() bool
	// Params params 定义传递给 NewInstance 的参数,可以自定义多个,建议将 container 作为第一个参数
	Params(Container) []interface{}
	// Name 代表了这个服务提供者的凭证
	Name() string
}

服务提供者需要的接口

type Tree

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

func (*Tree) AddRouter

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

添加路由节点

func (*Tree) FindHandler

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

匹配uri,查找对应的handler

Directories

Path Synopsis
doc
gin
Package gin implements a HTTP web framework called gin.
Package gin implements a HTTP web framework called gin.
provider
app
env
id
log
orm

Jump to

Keyboard shortcuts

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