gs

package
v1.1.0-beta Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2021 License: Apache-2.0 Imports: 28 Imported by: 36

Documentation

Overview

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

Index

Constants

View Source
const (
	Unrefreshed = refreshState(iota) // 未刷新
	Refreshing                       // 正在刷新
	Refreshed                        // 已刷新
)
View Source
const (
	HighestOrder = math.MinInt32
	LowestOrder  = math.MaxInt32
)
View Source
const (
	Deleted   = beanStatus(-1)   // 已删除
	Default   = beanStatus(iota) // 未处理
	Resolving                    // 正在决议
	Resolved                     // 已决议
	Creating                     // 正在创建
	Created                      // 已创建
	Wired                        // 注入完成
)
View Source
const (
	Version = "go-spring@v1.0.5"
	Website = "https://go-spring.com/"
)
View Source
const DefaultBanner = `` /* 461-byte string literal not displayed */
View Source
const EnvPrefix = "GS_"

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

View Source
const ExcludeEnvPatterns = "EXCLUDE_ENV_PATTERNS"

ExcludeEnvPatterns 排除符合条件的环境变量。

View Source
const IncludeEnvPatterns = "INCLUDE_ENV_PATTERNS"

IncludeEnvPatterns 只加载符合条件的环境变量。

View Source
const SpringBannerVisible = "spring.banner.visible"

SpringBannerVisible 是否显示 banner。

Variables

This section is empty.

Functions

func Banner(banner string)

Banner 参考 App.Banner 的解释。

func Bootstrap

func Bootstrap() *bootstrap

Bootstrap 参考 App.Bootstrap 的解释。

func Consume

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

Consume 参考 App.Consume 的解释。

func DeleteBinding

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

DeleteBinding 参考 App.DeleteBinding 的解释。

func DeleteMapping

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

DeleteMapping 参考 App.DeleteMapping 的解释。

func GetBinding

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

GetBinding 参考 App.GetBinding 的解释。

func GetMapping

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

GetMapping 参考 App.GetMapping 的解释。

func HandleDelete

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

HandleDelete 参考 App.HandleDelete 的解释。

func HandleGet

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

HandleGet 参考 App.HandleGet 的解释。

func HandlePost

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

HandlePost 参考 App.HandlePost 的解释。

func HandlePut

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

HandlePut 参考 App.HandlePut 的解释。

func HandleRequest

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

HandleRequest 参考 App.HandleRequest 的解释。

func OnProperty

func OnProperty(key string, fn interface{})

OnProperty 参考 App.OnProperty 的解释。

func PostBinding

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

PostBinding 参考 App.PostBinding 的解释。

func PostMapping

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

PostMapping 参考 App.PostMapping 的解释。

func Property

func Property(key string, value interface{})

Property 参考 Container.Property 的解释。

func PutBinding

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

PutBinding 参考 App.PutBinding 的解释。

func PutMapping

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

PutMapping 参考 App.PutMapping 的解释。

func RequestBinding

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

RequestBinding 参考 App.RequestBinding 的解释。

func RequestMapping

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

RequestMapping 参考 App.RequestMapping 的解释。

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 {
	Events []AppEvent `autowire:""`
	// 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) Bootstrap

func (app *App) Bootstrap() *bootstrap

Bootstrap 返回 *bootstrap 对象。

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

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

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

func (*App) GrpcServer

func (app *App) GrpcServer(serviceName string, server *grpc.Server) *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 参考 Container.Object 的解释。

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 参考 Container.Property 的解释。

func (*App) Provide

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

Provide 参考 Container.Provide 的解释。

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 AppEvent

type AppEvent interface {
	OnStartApp(e Environment)      // 应用启动的事件
	OnStopApp(ctx context.Context) // 应用停止的事件
}

AppEvent 应用运行过程中的事件

type AppRunner

type AppRunner interface {
	Run(e Environment)
}

AppRunner 命令行启动器接口

type BeanDefinition

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

BeanDefinition bean 元数据。

func GrpcClient

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

GrpcClient 参考 App.GrpcClient 的解释。

func GrpcServer

func GrpcServer(serviceName string, server *grpc.Server) *BeanDefinition

GrpcServer 参考 App.GrpcServer 的解释。

func NewBean

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

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

func Object

func Object(i interface{}) *BeanDefinition

Object 参考 Container.Object 的解释。

func Provide

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

Provide 参考 Container.Provide 的解释。

func (*BeanDefinition) BeanName

func (d *BeanDefinition) BeanName() string

BeanName 返回 bean 的名称。

func (*BeanDefinition) Created

func (d *BeanDefinition) Created() bool

Created 返回是否已创建。

func (*BeanDefinition) DependsOn

func (d *BeanDefinition) DependsOn(selectors ...BeanSelector) *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 BeanDestroy

type BeanDestroy interface {
	OnDestroy()
}

type BeanInit

type BeanInit interface {
	OnInit(e Environment) error
}

type BeanSelector added in v1.2.0

type BeanSelector = internal.BeanSelector

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 interface {
	Context() context.Context
	Property(key string, value interface{})
	Object(i interface{}) *BeanDefinition
	Provide(ctor interface{}, args ...arg.Arg) *BeanDefinition
	Refresh(opts ...internal.RefreshOption) error
	Go(fn func(ctx context.Context))
	Close()
}

func New

func New() Container

New 创建 IoC 容器。

type Environment

type Environment interface {
	Context() context.Context
	HasProperty(key string) bool
	GetProperty(key string, opts ...conf.GetOption) string
	Bind(i interface{}, opts ...conf.BindOption) error
	GetBean(i interface{}, selectors ...BeanSelector) error
	Wire(objOrCtor interface{}, ctorArgs ...arg.Arg) (interface{}, error)
	Invoke(fn interface{}, args ...arg.Arg) ([]interface{}, error)
	Go(fn func(ctx context.Context))
}

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

type Resource

type Resource interface {
	io.Reader
	Name() string
}

Resource 具有名字的 io.Reader 接口称为资源。

type ResourceLocator

type ResourceLocator interface {
	Locate(filename string) ([]Resource, error)
}

ResourceLocator 查找名字为 filename 的资源。

Directories

Path Synopsis
Package arg 用于实现函数参数绑定。
Package arg 用于实现函数参数绑定。
Package cond 提供了判断 bean 注册是否有效的条件。
Package cond 提供了判断 bean 注册是否有效的条件。

Jump to

Keyboard shortcuts

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