app

package
v0.0.0-...-4028f8f Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2024 License: MIT Imports: 15 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add[T interface{}](scope ServiceScope, creator serverCreator[T])

func AddCommand

func AddCommand(cmd *cobra.Command)

AddCommand Add a command to the application.

func AddCommandFunc

func AddCommandFunc(use string, short string, run func())

AddCommandFunc Add a command function to the application.

func AddSingleton

func AddSingleton[T interface{}](creator serverCreator[T])

func AddSingletonDefault

func AddSingletonDefault[T interface{}]()

AddSingletonDefault 添加 Singleton 无参构造对象

func AddTransient

func AddTransient[T interface{}](creator serverCreator[T])

func AddTransientDefault

func AddTransientDefault[T interface{}]()

AddTransientDefault 添加Transient无参构造对象

func AddValue

func AddValue(value interface{})

func Configure

func Configure(action ConfigureFunc)

Configure is a function that will be called before the application run.

func ConfigureOptions

func ConfigureOptions[T any](f func(container *Container, v *T))

ConfigureOptions 配置 Options,不支持两个类型相互依赖

func Get

func Get[T interface{}]() *T

func GetArray

func GetArray[T interface{}]() []T

func GetByType

func GetByType[T interface{}](serviceType reflect.Type) *T

func GetOptional

func GetOptional[T interface{}]() (*T, bool)

func GetPtr

func GetPtr[T interface{}]() T

func GetPtrOptional

func GetPtrOptional[T interface{}]() (T, bool)

func Info

func Info(use string, short string, description string)

Info set the application info.

func NewRun

func NewRun(r RunFunc) runFunInfo

func OrderRun

func OrderRun(order int, action RunFunc)

OrderRun is a function that will be called by order when the application starts run.

func PostRun

func PostRun(action RunFunc)

PostRun is a function that will be called before the application exit.

func Run

func Run(action RunFunc)

Run is a function that will be called when the application starts run. order is 0.

func TryAdd

func TryAdd[T interface{}](scope ServiceScope, creator serverCreator[T])

func TryAddSingleton

func TryAddSingleton[T interface{}](creator serverCreator[T])

func TryAddSingletonDefault

func TryAddSingletonDefault[T interface{}]()

TryAddSingletonDefault 添加 Singleton 无参构造对象

func TryAddTransient

func TryAddTransient[T interface{}](creator serverCreator[T])

func TryAddTransientDefault

func TryAddTransientDefault[T interface{}]()

TryAddTransientDefault 添加Transient无参构造对象

func TryAddValue

func TryAddValue(value interface{})

func Version

func Version(version string)

Version set the application version.

Types

type App

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

func Build

func Build() (*App, error)

func (App) Run

func (app App) Run()

type AppBuilder

type AppBuilder struct {
	Context    *AppContext
	CmdBuilder *CommandBuilder
}

func DefaultBuilder

func DefaultBuilder() *AppBuilder

func NewAppBuilder

func NewAppBuilder() *AppBuilder

func (*AppBuilder) Build

func (a *AppBuilder) Build() (*App, error)

func (*AppBuilder) Configure

func (a *AppBuilder) Configure(action RunFunc)

func (*AppBuilder) Info

func (a *AppBuilder) Info(use string, short string, description string) *AppBuilder

func (*AppBuilder) OrderRun

func (a *AppBuilder) OrderRun(order int, action RunFunc)

func (*AppBuilder) PostRun

func (a *AppBuilder) PostRun(action RunFunc)

func (*AppBuilder) Run

func (a *AppBuilder) Run(action RunFunc)

func (*AppBuilder) Version

func (a *AppBuilder) Version(version string) *AppBuilder

type AppContext

type AppContext struct {
	PreRuns  []runFunInfo
	Runs     []runFunInfo
	PostRuns []runFunInfo

	Name        string
	Short       string
	Description string
	Version     string
}

func NewAppContext

func NewAppContext() *AppContext

func (*AppContext) OrderRun

func (a *AppContext) OrderRun(order int, action RunFunc)

func (*AppContext) PostOrderRun

func (a *AppContext) PostOrderRun(order int, action RunFunc)

func (*AppContext) PostRun

func (a *AppContext) PostRun(action RunFunc)

func (*AppContext) PreOrderRun

func (a *AppContext) PreOrderRun(order int, action RunFunc)

func (*AppContext) PreRun

func (a *AppContext) PreRun(action RunFunc)

func (*AppContext) Run

func (a *AppContext) Run(action RunFunc)

type AppEnvironment

type AppEnvironment string
const (
	Development AppEnvironment = "development"
	Production  AppEnvironment = "production"
	Staging     AppEnvironment = "staging"
)

type AppOptions

type AppOptions struct {
	Environment AppEnvironment
	LogLevel    zerolog.Level
}

type CommandBuilder

type CommandBuilder struct {
	RootCmd *cobra.Command
}

func NewCommandBuilder

func NewCommandBuilder() *CommandBuilder

func (*CommandBuilder) AddCommand

func (b *CommandBuilder) AddCommand(cmd *cobra.Command)

func (*CommandBuilder) AddRun

func (b *CommandBuilder) AddRun(use string, short string, run func())

func (*CommandBuilder) Build

func (cb *CommandBuilder) Build(ac *AppContext) (*cobra.Command, error)

type ConfigureFunc

type ConfigureFunc = func() error

type Container

type Container struct {
	Services []*serviceItem
	// contains filtered or unexported fields
}

func AddService

func AddService(descriptor *ServiceDescriptor) *Container

func CreateContainer

func CreateContainer() *Container

func GetContainer

func GetContainer() *Container

func TryAddService

func TryAddService(descriptor *ServiceDescriptor) *Container

func (*Container) Add

func (c *Container) Add(descriptor *ServiceDescriptor)

func (*Container) Configure

func (c *Container) Configure(t reflect.Type, initFunc InitFunc)

Configure 配置服务,不支持两个类型相互依赖

func (*Container) Get

func (c *Container) Get(serviceType reflect.Type) interface{}

func (*Container) GetArray

func (c *Container) GetArray(baseType reflect.Type) []interface{}

GetArray 有问题,只能transient有效,可使用 options 模式来替代,通过 ConfigureOption 来配置

func (*Container) GetOptional

func (c *Container) GetOptional(serviceType reflect.Type) (interface{}, bool)

GetOptional 获取对象,如果没有,返回nil

func (*Container) TryAdd

func (c *Container) TryAdd(descriptor *ServiceDescriptor)

type InitFunc

type InitFunc func(container *Container, instance any)

type RunFunc

type RunFunc = func() error

type ServiceCreator

type ServiceCreator func(c *Container) interface{}

type ServiceDescriptor

type ServiceDescriptor struct {
	ServiceType reflect.Type
	Creator     ServiceCreator
	Scope       ServiceScope
	Value       interface{}
}

func NewServiceDescriptor

func NewServiceDescriptor(serviceType reflect.Type, creator ServiceCreator, scope ServiceScope) ServiceDescriptor

type ServiceScope

type ServiceScope int
const (
	Transient ServiceScope = 0
	Singleton ServiceScope = 1
	Scoped    ServiceScope = 2
)

type VersionInfo

type VersionInfo struct {
	Version string
	Date    string
	Commit  string
	BuiltBy string
}

func (*VersionInfo) String

func (v *VersionInfo) String() string

Jump to

Keyboard shortcuts

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