gs

package
v1.1.0-alpha Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2021 License: Apache-2.0 Imports: 30 Imported by: 36

Documentation

Overview

Package gs 实现了 go-spring 的核心骨架,包含 IoC 容器、基于 IoC 容器的 App 以及全局 App 对象封装三个部分,可以应用于多种使用场景。

Index

Constants

View Source
const (
	HighestOrder = math.MinInt32
	LowestOrder  = math.MaxInt32
)
View Source
const (
	Default   = beanStatus(0) // 默认状态
	Resolving = beanStatus(1) // 正在决议
	Resolved  = beanStatus(2) // 已决议
	Wiring    = beanStatus(3) // 正在注入
	Wired     = beanStatus(4) // 注入完成
	Deleted   = beanStatus(5) // 已删除
)
View Source
const (
	Unrefreshed = refreshState(0) // 未刷新
	Refreshing  = refreshState(1) // 正在刷新
	Refreshed   = refreshState(2) // 已刷新
)
View Source
const DefaultBanner = `` /* 641-byte string literal not displayed */
View Source
const EnvPrefix = "GS_"

EnvPrefix 属性覆盖的环境变量需要携带该前缀。

Variables

View Source
var AppEvent = (*appEvent)(nil)

AppEvent 导出 appEvent 类型

View Source
var AppRunner = (*appRunner)(nil)

AppRunner 导出 appRunner 类型

View Source
var WebFilter = (*web.Filter)(nil)

WebFilter 导出 web.Filter 类型

View Source
var WebRouter = (*web.Router)(nil)

WebRouter 导出 web.Router 类型

Functions

func Banner(banner string)

Banner 自定义 banner 字符串。

func Consume

func Consume(fn interface{}, topics ...string)

Consume 注册 MQ 消费者。

func DeleteBinding

func DeleteBinding(path string, fn interface{}) *web.Mapper

DeleteBinding 注册 DELETE 方法处理函数。

func DeleteMapping

func DeleteMapping(path string, fn web.HandlerFunc) *web.Mapper

DeleteMapping 注册 DELETE 方法处理函数。

func GetBinding

func GetBinding(path string, fn interface{}) *web.Mapper

GetBinding 注册 GET 方法处理函数。

func GetMapping

func GetMapping(path string, fn web.HandlerFunc) *web.Mapper

GetMapping 注册 GET 方法处理函数。

func Go

func Go(fn func(ctx context.Context))

Go 创建安全可等待的 goroutine,fn 要求的 ctx 对象由 IoC 容器提供,当 IoC 容 器关闭时 ctx会 发出 Done 信号, fn 在接收到此信号后应当立即退出。

func HandleDelete

func HandleDelete(path string, h web.Handler) *web.Mapper

HandleDelete 注册 DELETE 方法处理函数。

func HandleGet

func HandleGet(path string, h web.Handler) *web.Mapper

HandleGet 注册 GET 方法处理函数。

func HandlePost

func HandlePost(path string, h web.Handler) *web.Mapper

HandlePost 注册 POST 方法处理函数。

func HandlePut

func HandlePut(path string, h web.Handler) *web.Mapper

HandlePut 注册 PUT 方法处理函数。

func HandleRequest

func HandleRequest(method uint32, path string, h web.Handler) *web.Mapper

HandleRequest 注册任意 HTTP 方法处理函数。

func OnProperty

func OnProperty(key string, fn interface{})

OnProperty 当 key 对应的属性值准备好后发送一个通知。

func PostBinding

func PostBinding(path string, fn interface{}) *web.Mapper

PostBinding 注册 POST 方法处理函数。

func PostMapping

func PostMapping(path string, fn web.HandlerFunc) *web.Mapper

PostMapping 注册 POST 方法处理函数。

func PrintBanner

func PrintBanner(banner string)

PrintBanner 打印 banner 到控制台

func Property

func Property(key string, value interface{})

Property 设置 key 对应的属性值,如果 key 对应的属性值已经存在则 Set 方法会 覆盖旧值。Set 方法除了支持 string 类型的属性值,还支持 int、uint、bool 等 其他基础数据类型的属性值。特殊情况下,Set 方法也支持 slice 、map 与基础数据 类型组合构成的属性值,其处理方式是将组合结构层层展开,可以将组合结构看成一棵树, 那么叶子结点的路径就是属性的 key,叶子结点的值就是属性的值。

func PutBinding

func PutBinding(path string, fn interface{}) *web.Mapper

PutBinding 注册 PUT 方法处理函数。

func PutMapping

func PutMapping(path string, fn web.HandlerFunc) *web.Mapper

PutMapping 注册 PUT 方法处理函数。

func RequestBinding

func RequestBinding(method uint32, path string, fn interface{}) *web.Mapper

RequestBinding 注册任意 HTTP 方法处理函数。

func RequestMapping

func RequestMapping(method uint32, path string, fn web.HandlerFunc) *web.Mapper

RequestMapping 注册任意 HTTP 方法处理函数。

func Run

func Run() error

Run 启动程序。

func Setenv

func Setenv(key string, value string)

Setenv 封装 os.Setenv 函数,如果发生 error 会 panic 。

func ShutDown

func ShutDown(err error)

ShutDown 停止程序。

Types

type App

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

App 应用

func NewApp

func NewApp() *App

NewApp application 的构造函数

func (*App) Banner

func (app *App) Banner(banner string)

Banner 自定义 banner 字符串。

func (*App) Consume

func (app *App) Consume(fn interface{}, topics ...string)

Consume 注册 MQ 消费者。

func (*App) DeleteBinding

func (app *App) DeleteBinding(path string, fn interface{}) *web.Mapper

DeleteBinding 注册 DELETE 方法处理函数

func (*App) DeleteMapping

func (app *App) DeleteMapping(path string, fn web.HandlerFunc) *web.Mapper

DeleteMapping 注册 DELETE 方法处理函数

func (*App) GetBinding

func (app *App) GetBinding(path string, fn interface{}) *web.Mapper

GetBinding 注册 GET 方法处理函数

func (*App) GetMapping

func (app *App) GetMapping(path string, fn web.HandlerFunc) *web.Mapper

GetMapping 注册 GET 方法处理函数

func (*App) Go

func (app *App) Go(fn func(ctx context.Context))

Go 创建安全可等待的 goroutine,fn 要求的 ctx 对象由 IoC 容器提供,当 IoC 容 器关闭时 ctx会 发出 Done 信号, fn 在接收到此信号后应当立即退出。

func (*App) GrpcClient

func (app *App) GrpcClient(fn interface{}, endpoint string) *BeanDefinition

GrpcClient 注册 gRPC 服务客户端,fn 是 gRPC 自动生成的客户端构造函数。

func (*App) GrpcServer

func (app *App) GrpcServer(serviceName string, fn interface{}, service interface{}) *BeanDefinition

GrpcServer 注册 gRPC 服务提供者,fn 是 gRPC 自动生成的服务注册函数, serviceName 是服务名称,必须对应 *_grpc.pg.go 文件里面 grpc.ServerDesc 的 ServiceName 字段,server 是服务提供者对象。

func (*App) HandleDelete

func (app *App) HandleDelete(path string, h web.Handler) *web.Mapper

HandleDelete 注册 DELETE 方法处理函数

func (*App) HandleGet

func (app *App) HandleGet(path string, h web.Handler) *web.Mapper

HandleGet 注册 GET 方法处理函数

func (*App) HandlePost

func (app *App) HandlePost(path string, h web.Handler) *web.Mapper

HandlePost 注册 POST 方法处理函数

func (*App) HandlePut

func (app *App) HandlePut(path string, h web.Handler) *web.Mapper

HandlePut 注册 PUT 方法处理函数

func (*App) HandleRequest

func (app *App) HandleRequest(method uint32, path string, h web.Handler) *web.Mapper

HandleRequest 注册任意 HTTP 方法处理函数

func (*App) Object

func (app *App) Object(i interface{}) *BeanDefinition

Object 注册对象形式的 bean ,需要注意的是该方法在注入开始后就不能再调用了。

func (*App) OnProperty

func (app *App) OnProperty(key string, fn interface{})

OnProperty 当 key 对应的属性值准备好后发送一个通知。

func (*App) PostBinding

func (app *App) PostBinding(path string, fn interface{}) *web.Mapper

PostBinding 注册 POST 方法处理函数

func (*App) PostMapping

func (app *App) PostMapping(path string, fn web.HandlerFunc) *web.Mapper

PostMapping 注册 POST 方法处理函数

func (*App) Property

func (app *App) Property(key string, value interface{})

Property 设置 key 对应的属性值,如果 key 对应的属性值已经存在则 Set 方法会 覆盖旧值。Set 方法除了支持 string 类型的属性值,还支持 int、uint、bool 等 其他基础数据类型的属性值。特殊情况下,Set 方法也支持 slice 、map 与基础数据 类型组合构成的属性值,其处理方式是将组合结构层层展开,可以将组合结构看成一棵树, 那么叶子结点的路径就是属性的 key,叶子结点的值就是属性的值。

func (*App) Provide

func (app *App) Provide(ctor interface{}, args ...arg.Arg) *BeanDefinition

Provide 注册构造函数形式的 bean ,需要注意的是该方法在注入开始后就不能再调用了。

func (*App) PutBinding

func (app *App) PutBinding(path string, fn interface{}) *web.Mapper

PutBinding 注册 PUT 方法处理函数

func (*App) PutMapping

func (app *App) PutMapping(path string, fn web.HandlerFunc) *web.Mapper

PutMapping 注册 PUT 方法处理函数

func (*App) RequestBinding

func (app *App) RequestBinding(method uint32, path string, fn interface{}) *web.Mapper

RequestBinding 注册任意 HTTP 方法处理函数

func (*App) RequestMapping

func (app *App) RequestMapping(method uint32, path string, fn web.HandlerFunc) *web.Mapper

RequestMapping 注册任意 HTTP 方法处理函数

func (*App) Run

func (app *App) Run() error

func (*App) ShutDown

func (app *App) ShutDown(err error)

ShutDown 关闭执行器

type AppContext

type AppContext interface {
	Go(fn func(ctx context.Context))
}

type BeanDefinition

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

BeanDefinition bean 元数据。

func GrpcClient

func GrpcClient(fn interface{}, endpoint string) *BeanDefinition

GrpcClient 注册 gRPC 服务客户端,fn 是 gRPC 自动生成的客户端构造函数。

func GrpcServer

func GrpcServer(serviceName string, fn interface{}, service interface{}) *BeanDefinition

GrpcServer 注册 gRPC 服务提供者,fn 是 gRPC 自动生成的服务注册函数, serviceName 是服务名称,必须对应 *_grpc.pg.go 文件里面 grpc.ServerDesc 的 ServiceName 字段,server 是服务提供者对象。

func NewBean

func NewBean(objOrCtor interface{}, ctorArgs ...arg.Arg) *BeanDefinition

NewBean 普通函数注册时需要使用 reflect.ValueOf(fn) 形式以避免和构造函数发生冲突。

func Object

func Object(i interface{}) *BeanDefinition

Object 注册对象形式的 bean ,需要注意的是该方法在注入开始后就不能再调用了。

func Provide

func Provide(ctor interface{}, args ...arg.Arg) *BeanDefinition

Provide 注册构造函数形式的 bean ,需要注意的是该方法在注入开始后就不能再调用了。

func (*BeanDefinition) BeanName

func (d *BeanDefinition) BeanName() string

BeanName 返回 bean 的名称。

func (*BeanDefinition) DependsOn

func (d *BeanDefinition) DependsOn(selectors ...bean.Selector) *BeanDefinition

DependsOn 设置 bean 的间接依赖项。

func (*BeanDefinition) Destroy

func (d *BeanDefinition) Destroy(fn interface{}) *BeanDefinition

Destroy 设置 bean 的销毁函数。

func (*BeanDefinition) Export

func (d *BeanDefinition) Export(exports ...interface{}) *BeanDefinition

Export 设置 bean 的导出接口。

func (*BeanDefinition) FileLine

func (d *BeanDefinition) FileLine() string

FileLine 返回 bean 的注册点。

func (*BeanDefinition) ID

func (d *BeanDefinition) ID() string

ID 返回 bean 的 ID 。

func (*BeanDefinition) Init

func (d *BeanDefinition) Init(fn interface{}) *BeanDefinition

Init 设置 bean 的初始化函数。

func (*BeanDefinition) Interface

func (d *BeanDefinition) Interface() interface{}

Interface 返回 bean 的真实值。

func (*BeanDefinition) Match

func (d *BeanDefinition) Match(typeName string, beanName string) bool

Match 测试 bean 的类型全限定名和 bean 的名称是否都匹配。

func (*BeanDefinition) Name

func (d *BeanDefinition) Name(name string) *BeanDefinition

Name 设置 bean 的名称。

func (*BeanDefinition) On

On 设置 bean 的 Condition。

func (*BeanDefinition) Order

func (d *BeanDefinition) Order(order int) *BeanDefinition

Order 设置 bean 的排序序号,值越小顺序越靠前(优先级越高)。

func (*BeanDefinition) Primary

func (d *BeanDefinition) Primary() *BeanDefinition

Primary 设置 bean 为主版本。

func (*BeanDefinition) String

func (d *BeanDefinition) String() string

func (*BeanDefinition) Type

func (d *BeanDefinition) Type() reflect.Type

Type 返回 bean 的类型。

func (*BeanDefinition) TypeName

func (d *BeanDefinition) TypeName() string

TypeName 返回 bean 的原始类型的全限定名。

func (*BeanDefinition) Value

func (d *BeanDefinition) Value() reflect.Value

Value 返回 bean 的值。

func (*BeanDefinition) Wired

func (d *BeanDefinition) Wired() bool

Wired 返回 bean 是否已经注入完成。

type Consumers

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

func (*Consumers) Add

func (c *Consumers) Add(consumer mq.Consumer)

func (*Consumers) ForEach

func (c *Consumers) ForEach(fn func(mq.Consumer))

type Container

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

Container 是 go-spring 框架的基石,实现了 Martin Fowler 在 << Inversion of Control Containers and the Dependency Injection pattern >> 一文中 提及的依赖注入的概念。但原文的依赖注入仅仅是指对象之间的依赖关系处理,而有些 IoC 容器在实现时比如 Spring 还引入了对属性 property 的处理。通常大家会用依赖注入统 述上面两种概念,但实际上使用属性绑定来描述对 property 的处理会更加合适,因此 go-spring 严格区分了这两种概念,在描述对 bean 的处理时要么单独使用依赖注入或属 性绑定,要么同时使用依赖注入和属性绑定。

func New

func New() *Container

New 创建 IoC 容器。

func (*Container) Close

func (c *Container) Close()

Close 关闭容器,此方法必须在 Refresh 之后调用。该方法会触发 ctx 的 Done 信 号,然后等待所有 goroutine 结束,最后按照被依赖先销毁的原则执行所有的销毁函数。

func (*Container) Go

func (c *Container) Go(fn func(ctx context.Context))

Go 创建安全可等待的 goroutine,fn 要求的 ctx 对象由 IoC 容器提供,当 IoC 容 器关闭时 ctx会 发出 Done 信号, fn 在接收到此信号后应当立即退出。

func (*Container) Load

func (c *Container) Load(file string) error

Load 从属性文件加载属性列表,file 可以是绝对路径,也可以是相对路径。该方法会覆 盖已有的属性值。

func (*Container) Object

func (c *Container) Object(i interface{}) *BeanDefinition

Object 注册对象形式的 bean ,需要注意的是该方法在注入开始后就不能再调用了。

func (*Container) Property

func (c *Container) Property(key string, value interface{})

Property 设置 key 对应的属性值,如果 key 对应的属性值已经存在则 Set 方法会 覆盖旧值。Set 方法除了支持 string 类型的属性值,还支持 int、uint、bool 等 其他基础数据类型的属性值。特殊情况下,Set 方法也支持 slice 、map 与基础数据 类型组合构成的属性值,其处理方式是将组合结构层层展开,可以将组合结构看成一棵树, 那么叶子结点的路径就是属性的 key,叶子结点的值就是属性的值。

func (*Container) Provide

func (c *Container) Provide(ctor interface{}, args ...arg.Arg) *BeanDefinition

Provide 注册构造函数形式的 bean ,需要注意的是该方法在注入开始后就不能再调用了。

func (*Container) Refresh

func (c *Container) Refresh() error

Refresh 刷新容器的内容,对 bean 进行有效性判断以及完成属性绑定和依赖注入。

type Environment

type Environment interface {
	Get(key string, opts ...conf.GetOption) interface{}
}

Environment 提供获取环境变量和命令行参数的方法,命令行参数优先级更高。

type Pandora

type Pandora interface {
	Go(fn func(ctx context.Context))
	Prop(key string, opts ...conf.GetOption) interface{}
	Bind(i interface{}, opts ...conf.BindOption) error
	Get(i interface{}, selectors ...bean.Selector) error
	Wire(objOrCtor interface{}, ctorArgs ...arg.Arg) (interface{}, error)
	Invoke(fn interface{}, args ...arg.Arg) ([]interface{}, error)
}

Pandora 提供了一些在 IoC 容器启动后基于反射获取和使用 property 与 bean 的接 口。因为很多人会担心在运行时大量使用反射会降低程序性能,所以命名为 Pandora,取 其诱人但危险的含义。事实上,这些在 IoC 容器启动后使用属性绑定和依赖注入的方案, 都可以转换为启动阶段的方案以提高程序的性能。 另一方面,为了统一 Container 和 App 两种启动方式下这些方法的使用方式,需要提取 出一个可共用的接口来,也就是说,无论程序是 Container 方式启动还是 App 方式启动, 都可以在需要使用这些方法的地方注入一个 Pandora 对象而不是 Container 对象或者 App 对象,从而实现使用方式的统一。

Directories

Path Synopsis
Package arg 用于实现函数参数绑定。
Package arg 用于实现函数参数绑定。
Package bean 定义了 bean 选择器和 Definition 接口。
Package bean 定义了 bean 选择器和 Definition 接口。
Package cond 提供了判断 bean 注册是否有效的条件。
Package cond 提供了判断 bean 注册是否有效的条件。
Package environ 提供了 go-spring 所有内置属性的定义,以及一种允许使用环境变量 进行属性覆盖的机制。
Package environ 提供了 go-spring 所有内置属性的定义,以及一种允许使用环境变量 进行属性覆盖的机制。

Jump to

Keyboard shortcuts

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