gs

package
v1.2.5 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2025 License: Apache-2.0 Imports: 25 Imported by: 38

Documentation

Index

Constants

View Source
const (
	Version = "go-spring@v1.2.5"
	Website = "https://github.com/go-spring/"
)
View Source
const (
	// AllowCircularReferencesProp controls whether the container
	// allows circular dependencies between beans.
	AllowCircularReferencesProp = "spring.allow-circular-references"

	// ForceAutowireIsNullableProp forces autowired dependencies
	// to be treated as nullable (i.e. allowed to be nil).
	ForceAutowireIsNullableProp = "spring.force-autowire-is-nullable"

	// ActiveProfilesProp defines the active application profiles
	// (e.g. "dev", "test", "prod").
	ActiveProfilesProp = "spring.profiles.active"

	// EnableJobsProp enables or disables scheduled job execution.
	EnableJobsProp = "spring.app.enable-jobs"

	// EnableServersProp enables or disables all server components.
	EnableServersProp = "spring.app.enable-servers"

	// EnableSimpleHttpServerProp enables or disables the built-in
	// lightweight HTTP server.
	EnableSimpleHttpServerProp = "spring.enable.simple-http-server"

	// EnableSimplePProfServerProp enables or disables the built-in
	// lightweight pprof server.
	EnableSimplePProfServerProp = "spring.enable.simple-pprof-server"
)

Variables

View Source
var (
	// B is the global bootstrapper for initializing the application.
	B = gs_app.NewBoot()
)

Functions

func AddTester added in v1.2.3

func AddTester(t any)

AddTester registers a tester instance into the test suite. The tester will be scanned for methods prefixed with "Test", which will be automatically added to the Go test framework.

func AllowCircularReferences added in v1.2.0

func AllowCircularReferences(enable bool)

AllowCircularReferences sets whether circular references between beans are permitted during dependency injection. Default is usually false.

func As added in v1.2.0

func As[T any]() reflect.Type

As returns the reflect.Type for a given interface type T.

func Banner(banner string)

Banner sets a custom app banner.

func BindArg added in v1.2.0

func BindArg(fn any, args ...Arg) *gs_arg.BindArg

BindArg binds arguments dynamically to an option-style constructor.

func Config added in v1.2.0

func Config() *gs_conf.AppConfig

Config returns the current application configuration.

func EnableJobs added in v1.2.0

func EnableJobs(enable bool)

EnableJobs enables or disables the execution of scheduled jobs.

func EnableServers added in v1.2.0

func EnableServers(enable bool)

EnableServers enables or disables all server components in the application (e.g. HTTP servers, gRPC servers).

func EnableSimpleHttpServer added in v1.2.0

func EnableSimpleHttpServer(enable bool)

EnableSimpleHttpServer enables or disables the built-in lightweight HTTP server provided by the framework.

func EnableSimplePProfServer added in v1.2.0

func EnableSimplePProfServer(enable bool)

EnableSimplePProfServer enables or disables the built-in lightweight pprof server for performance profiling.

func Exiting added in v1.2.0

func Exiting() bool

Exiting returns true if the application is shutting down.

func ForceAutowireIsNullable added in v1.2.0

func ForceAutowireIsNullable(enable bool)

ForceAutowireIsNullable forces autowired dependencies to be treated as optional (nullable). This allows injection of nil when no candidate bean is available. Default is usually false.

func Group added in v1.2.3

func Group[T any, R any](tag string, fn func(c T) (R, error), d func(R) error)

Group registers a set of beans based on a configuration property map. Each map entry spawns a bean constructed via fn and optionally destroyed via d.

func Job added in v1.2.0

func Job(fn func(ctx context.Context) error) *gs.RegisteredBean

Job registers a function as a job bean.

func Module added in v1.2.3

func Module(conditions []ConditionOnProperty, fn func(p conf.Properties) error)

Module registers a configuration module that is conditionally activated based on property values.

func Object

func Object(i any) *gs.RegisteredBean

Object registers a bean definition for an existing object instance.

func Property

func Property(key string, val string)

Property sets a system property.

func Provide

func Provide(ctor any, args ...Arg) *gs.RegisteredBean

Provide registers a bean definition using the provided constructor function.

func RefreshProperties added in v1.2.0

func RefreshProperties() error

RefreshProperties reloads application properties from all sources.

func RegisterExpressFunc added in v1.2.0

func RegisterExpressFunc(name string, fn any)

RegisterExpressFunc registers a custom expression function that can be used inside conditional expressions.

func Root added in v1.2.5

func Root(b *gs.RegisteredBean)

Root registers a root bean in the application context.

func Run

func Run(fn ...func() error)

Run starts the application with a custom run function.

func RunAsync added in v1.2.2

func RunAsync() (func(), error)

RunAsync starts the application asynchronously and returns a stop function to gracefully shut it down.

func Runner added in v1.2.0

func Runner(fn func() error) *gs.RegisteredBean

Runner registers a function as a runner bean.

func SetActiveProfiles added in v1.2.0

func SetActiveProfiles(profiles string)

SetActiveProfiles sets the active application profiles (e.g. "dev", "prod"). This influences which configuration files and conditional beans are loaded.

func ShutDown

func ShutDown()

ShutDown gracefully stops the application.

func TestMain added in v1.2.3

func TestMain(m *testing.M)

TestMain is the custom entry point for the Go test framework. It injects test methods defined in registered testers into the internal 'tests' slice of testing.M, then starts the app and tests.

Types

type AppStarter added in v1.2.0

type AppStarter struct{}

AppStarter is a wrapper to manage the lifecycle of a Spring application. It handles initialization, running, graceful shutdown, and logging.

func Web

func Web(enable bool) *AppStarter

Web enables or disables the built-in HTTP server.

func (*AppStarter) Run added in v1.2.0

func (s *AppStarter) Run(fn ...func() error)

Run starts the application, optionally runs a user-defined callback, and waits for termination signals (e.g., SIGTERM, Ctrl+C) to trigger graceful shutdown.

func (*AppStarter) RunAsync added in v1.2.2

func (s *AppStarter) RunAsync() (func(), error)

RunAsync starts the application asynchronously and returns a function that can be used to trigger shutdown from outside.

type Arg added in v1.2.0

type Arg = gs.Arg

Arg represents an argument used when binding constructor parameters.

func IndexArg added in v1.2.0

func IndexArg(n int, arg Arg) Arg

IndexArg targets a specific constructor parameter by index and provides the given Arg as its value.

func TagArg added in v1.2.0

func TagArg(tag string) Arg

TagArg creates an argument that injects a property or bean identified by the specified struct-tag expression.

func ValueArg added in v1.2.0

func ValueArg(v any) Arg

ValueArg creates an argument with a fixed value.

type BeanMock added in v1.2.0

type BeanMock[T any] struct {
	// contains filtered or unexported fields
}

BeanMock represents a mock bean for testing.

func MockFor added in v1.2.3

func MockFor[T any](name ...string) BeanMock[T]

MockFor creates a BeanMock for the given type and optional bean name. It allows you to specify which bean in the IoC container should be mocked.

func (BeanMock[T]) With added in v1.2.3

func (m BeanMock[T]) With(obj T)

With registers a mock instance into the IoC container, replacing the original bean defined by the selector. This allows tests to use mocked dependencies.

type BeanSelector added in v1.2.0

type BeanSelector = gs.BeanSelector

BeanSelector is an alias for gs.BeanSelector used to locate beans within the ioc context.

func BeanSelectorFor added in v1.2.0

func BeanSelectorFor[T any](name ...string) BeanSelector

BeanSelectorFor creates a BeanSelector for the specified type T and optional bean name.

type Condition added in v1.2.0

type Condition = gs.Condition

Condition represents a logical predicate that decides whether a bean or module should be activated.

func And added in v1.2.0

func And(conditions ...Condition) Condition

And combines multiple conditions using logical AND.

func None added in v1.2.0

func None(conditions ...Condition) Condition

None returns a condition that is true if all provided conditions are false.

func Not added in v1.2.0

func Not(c Condition) Condition

Not returns the logical negation of the given condition.

func OnBean added in v1.2.0

func OnBean[T any](name ...string) Condition

OnBean requires that a bean of the given type (and optional name) exists.

func OnExpression added in v1.2.0

func OnExpression(expression string) Condition

OnExpression creates a condition from an expression.

func OnFunc added in v1.2.0

func OnFunc(fn func(ctx ConditionContext) (bool, error)) Condition

OnFunc creates a Condition backed by the given function.

func OnMissingBean added in v1.2.0

func OnMissingBean[T any](name ...string) Condition

OnMissingBean requires that no bean of the given type (and optional name) exists.

func OnOnce added in v1.2.3

func OnOnce(conditions ...Condition) Condition

OnOnce wraps the given conditions so they are evaluated only once. Subsequent calls return the same result. (Not concurrency-safe.)

func OnSingleBean added in v1.2.0

func OnSingleBean[T any](name ...string) Condition

OnSingleBean requires that exactly one instance of the given bean type exists.

func Or added in v1.2.0

func Or(conditions ...Condition) Condition

Or combines multiple conditions using logical OR.

type ConditionContext added in v1.2.3

type ConditionContext = gs.ConditionContext

ConditionContext provides the evaluation context for a Condition.

type ConditionOnProperty added in v1.2.3

type ConditionOnProperty = gs_cond.ConditionOnProperty

ConditionOnProperty is a convenience wrapper for property-based conditions.

func OnEnableJobs added in v1.2.3

func OnEnableJobs() ConditionOnProperty

OnEnableJobs is a shortcut for checking whether scheduled jobs are enabled.

func OnEnableServers added in v1.2.3

func OnEnableServers() ConditionOnProperty

OnEnableServers is a shortcut for checking whether servers are enabled.

func OnProperty

func OnProperty(name string) ConditionOnProperty

OnProperty creates a property-based condition.

type Dync added in v1.2.0

type Dync[T any] = gs_dync.Value[T]

Dync is a generic alias for a dynamic configuration value. It represents a property that can change at runtime.

type ReadySignal added in v1.2.0

type ReadySignal = gs.ReadySignal

ReadySignal represents a signal sent when the application is ready.

type Server added in v1.2.0

type Server = gs.Server

Server is an alias for gs.Server.

type SimpleHttpServer added in v1.2.0

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

SimpleHttpServer wraps a standard http.Server to integrate it into the Go-Spring application lifecycle.

func NewSimpleHttpServer added in v1.2.0

func NewSimpleHttpServer(h http.Handler, cfg SimpleHttpServerConfig) *SimpleHttpServer

NewSimpleHttpServer constructs a new SimpleHttpServer using the provided HTTP handler and configuration.

func (*SimpleHttpServer) ListenAndServe added in v1.2.0

func (s *SimpleHttpServer) ListenAndServe(sig ReadySignal) error

ListenAndServe starts the HTTP server and blocks until it is stopped. It waits for the given ReadySignal to be triggered before accepting traffic.

func (*SimpleHttpServer) Shutdown added in v1.2.0

func (s *SimpleHttpServer) Shutdown(ctx context.Context) error

Shutdown gracefully stops the HTTP server using the provided context, allowing in-flight requests to complete before closing.

type SimpleHttpServerConfig added in v1.2.5

type SimpleHttpServerConfig struct {
	// Address specifies the TCP address the server listens on.
	// Example: ":9090" (listen on all interfaces, port 9090).
	Address string `value:"${http.server.addr:=:9090}"`

	// ReadTimeout is the maximum duration for reading the entire
	// request, including the body.
	ReadTimeout time.Duration `value:"${http.server.readTimeout:=5s}"`

	// HeaderTimeout is the maximum duration for reading request headers.
	HeaderTimeout time.Duration `value:"${http.server.headerTimeout:=1s}"`

	// WriteTimeout is the maximum duration before timing out
	// a response write.
	WriteTimeout time.Duration `value:"${http.server.writeTimeout:=5s}"`

	// IdleTimeout is the maximum amount of time to wait for
	// the next request when keep-alive connections are enabled.
	IdleTimeout time.Duration `value:"${http.server.idleTimeout:=60s}"`
}

SimpleHttpServerConfig holds configuration for the SimpleHttpServer.

type SimplePProfServer added in v1.2.0

type SimplePProfServer struct {
	*SimpleHttpServer
}

SimplePProfServer is a simple HTTP server that exposes pprof endpoints.

func NewSimplePProfServer added in v1.2.0

func NewSimplePProfServer(addr string) *SimplePProfServer

NewSimplePProfServer creates a new SimplePProfServer at the given address. It registers the standard pprof handlers for runtime profiling and debugging.

Directories

Path Synopsis
internal
gs
gs_arg
Package gs_arg provides implementations for argument resolution and binding used by the Go-Spring framework.
Package gs_arg provides implementations for argument resolution and binding used by the Go-Spring framework.
gs_bean
Package gs_bean provides core bean management for Go-Spring framework.
Package gs_bean provides core bean management for Go-Spring framework.
gs_cond
Package gs_cond provides a set of composable conditions used to control bean registration for Go-Spring framework.
Package gs_cond provides a set of composable conditions used to control bean registration for Go-Spring framework.
gs_conf
Package gs_conf provides a layered configuration system for Go-Spring applications.
Package gs_conf provides a layered configuration system for Go-Spring applications.
gs_core
Package gs_core provides the core implementation of the Inversion of Control (IoC) container in the Go-Spring framework.
Package gs_core provides the core implementation of the Inversion of Control (IoC) container in the Go-Spring framework.
gs_dync
Package gs_dync provides dynamic configuration binding and refresh capabilities for Go-Spring applications.
Package gs_dync provides dynamic configuration binding and refresh capabilities for Go-Spring applications.

Jump to

Keyboard shortcuts

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