gs

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2022 License: Apache-2.0 Imports: 31 Imported by: 36

README

gs

Documentation

Overview

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

Index

Constants

View Source
const (
	Unrefreshed = refreshState(iota) // 未刷新
	RefreshInit                      // 准备刷新
	Refreshing                       // 正在刷新
	Refreshed                        // 已刷新
)
View Source
const (
	Deleted   = beanStatus(-1)   // 已删除
	Default   = beanStatus(iota) // 未处理
	Resolving                    // 正在决议
	Resolved                     // 已决议
	Creating                     // 正在创建
	Created                      // 已创建
	Wired                        // 注入完成
)
View Source
const (
	Version = "go-spring@v1.1.3"
	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 BeanID added in v1.1.1

func BeanID(typ interface{}, name string) string

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 File

func File(path string, file string) *web.Mapper

File 定义单个文件资源

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 GrpcServer

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

GrpcServer 参考 App.GrpcServer 的解释。

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 HttpDelete added in v1.1.3

func HttpDelete(path string, h http.HandlerFunc) *web.Mapper

HttpDelete 参考 App.HttpDelete 的解释。

func HttpGet added in v1.1.3

func HttpGet(path string, h http.HandlerFunc) *web.Mapper

HttpGet 参考 App.HttpGet 的解释。

func HttpPost added in v1.1.3

func HttpPost(path string, h http.HandlerFunc) *web.Mapper

HttpPost 参考 App.HttpPost 的解释。

func HttpPut added in v1.1.3

func HttpPut(path string, h http.HandlerFunc) *web.Mapper

HttpPut 参考 App.HttpPut 的解释。

func LoadCmdArgs added in v1.1.1

func LoadCmdArgs(args []string, p *conf.Properties) error

LoadCmdArgs 加载以 -D key=value 或者 -D key[=true] 形式传入的命令行参数。

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(msg ...string)

ShutDown 停止程序。

func Static

func Static(prefix string, dir string) *web.Mapper

Static 定义一组文件资源

func StaticFS

func StaticFS(prefix string, fs http.FileSystem) *web.Mapper

StaticFS 定义一组文件资源

func Web

func Web(enable bool) *startup

Types

type App

type App struct {
	Events  []AppEvent  `autowire:"${application-event.collection:=*?}"`
	Runners []AppRunner `autowire:"${command-line-runner.collection:=*?}"`
	// contains filtered or unexported fields
}

App 应用

func NewApp

func NewApp() *App

NewApp application 的构造函数

func (*App) Accept

func (app *App) Accept(b *BeanDefinition) *BeanDefinition

Accept 参考 Container.Accept 的解释。

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

func (app *App) File(path string, file string) *web.Mapper

File 定义单个文件资源

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)

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) HttpDelete added in v1.1.3

func (app *App) HttpDelete(path string, h http.HandlerFunc) *web.Mapper

HttpDelete 注册 DELETE 方法处理函数。

func (*App) HttpGet added in v1.1.3

func (app *App) HttpGet(path string, h http.HandlerFunc) *web.Mapper

HttpGet 注册 GET 方法处理函数。

func (*App) HttpPost added in v1.1.3

func (app *App) HttpPost(path string, h http.HandlerFunc) *web.Mapper

HttpPost 注册 POST 方法处理函数。

func (*App) HttpPut added in v1.1.3

func (app *App) HttpPut(path string, h http.HandlerFunc) *web.Mapper

HttpPut 注册 PUT 方法处理函数。

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(msg ...string)

ShutDown 关闭执行器

func (*App) Static

func (app *App) Static(prefix string, dir string) *web.Mapper

Static 定义一组文件资源

func (*App) StaticFS

func (app *App) StaticFS(prefix string, fs http.FileSystem) *web.Mapper

StaticFS 定义一组文件资源

type AppEvent

type AppEvent interface {
	OnAppStart(ctx Context)        // 应用启动的事件
	OnAppStop(ctx context.Context) // 应用停止的事件
}

AppEvent 应用运行过程中的事件

type AppRunner

type AppRunner interface {
	Run(ctx Context)
}

AppRunner 命令行启动器接口

type BeanDefinition

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

BeanDefinition bean 元数据。

func Accept

func Accept(b *BeanDefinition) *BeanDefinition

Accept 参考 Container.Accept 的解释。

func GrpcClient

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

GrpcClient 参考 App.GrpcClient 的解释。

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 ...util.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 float32) *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(ctx Context) error
}

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
	Properties() *dync.Properties
	Property(key string, value interface{})
	Object(i interface{}) *BeanDefinition
	Provide(ctor interface{}, args ...arg.Arg) *BeanDefinition
	Refresh() error
	Close()
}

func New

func New() Container

New 创建 IoC 容器。

type Context

type Context interface {
	Context() context.Context
	Keys() []string
	Has(key string) bool
	Prop(key string, opts ...conf.GetOption) string
	Resolve(s string) (string, error)
	Bind(i interface{}, opts ...conf.BindOption) error
	Get(i interface{}, selectors ...util.BeanSelector) error
	Wire(objOrCtor interface{}, ctorArgs ...arg.Arg) (interface{}, error)
	Invoke(fn interface{}, args ...arg.Arg) ([]interface{}, error)
	Go(fn func(ctx context.Context))
}

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

type ContextAware added in v1.1.1

type ContextAware struct {
	GSContext Context `autowire:""`
}

ContextAware injects the Context into a struct as the field GSContext.

type GrpcServers

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

func (*GrpcServers) Add

func (s *GrpcServers) Add(serviceName string, server *grpc.Server)

func (*GrpcServers) ForEach

func (s *GrpcServers) ForEach(fn func(string, *grpc.Server))

type Resource

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

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

type ResourceLocator

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

ResourceLocator 查找名字为 filename 的资源。

type WebStarter

type WebStarter struct {
	Containers []web.Server `autowire:""`
	Filters    []web.Filter `autowire:"${web.server.filters:=*?}"`
	Router     web.Router   `autowire:""`
}

WebStarter Web 服务器启动器

func (*WebStarter) OnAppStart

func (starter *WebStarter) OnAppStart(ctx Context)

OnAppStart 应用程序启动事件。

func (*WebStarter) OnAppStop

func (starter *WebStarter) OnAppStop(ctx context.Context)

OnAppStop 应用程序结束事件。

Directories

Path Synopsis
Package arg 用于实现函数参数绑定。
Package arg 用于实现函数参数绑定。
Package cond provides many conditions used when registering bean.
Package cond provides many conditions used when registering bean.

Jump to

Keyboard shortcuts

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