registry

package
v0.11.15 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2024 License: MIT Imports: 16 Imported by: 4

Documentation

Index

Constants

View Source
const MethodAny = "any"

MethodAny is a special value to pass for the Method key in a HandlerMap to call RouterGroup.Any instead of RouterGroup.METHOD. It is chosen so that it cannot overlap with any valid HTTP method.

Variables

View Source
var RegistryKey = PointerAt[Registry]("registry")
View Source
var ServiceNotFoundError = fmt.Errorf("service not found")

Functions

func EnvProvider added in v0.5.70

func EnvProvider(name string) *provider[string]

func NewInitializer

func NewInitializer(
	name string,
	initializer func(ctx context.Context, services *Registry, client ent.EntClientBase) error,
	starter func(ctx context.Context) error,
) *initializingService

NewInitializer creates a new service that just does simple initialization steps at app startup, instead of actually running a background service

func Provider

func Provider[T any](f func(Values) T) *provider[T]

func RegisterDefaultSignalListener

func RegisterDefaultSignalListener(s *Registry)

func WithValues

func WithValues(ctx context.Context, vs Values) context.Context

Types

type Controller

type Controller interface {
	Register(*Registry, gin.IRouter) error
}

Controller defines the interface for a controller that can register its routes against a (gin) router.

type HandlerMap

type HandlerMap = map[struct{ Method, Path string }]gin.HandlerFunc

HandlerMap defines a mapping from pairs of (method, path) to a handler function for a route. It is used in RegisterMap for a controller to add its routes in a table-driven format.

type Key

type Key interface {
	Address() string
	ValueType() reflect.Type
}

type LifecycleController

type LifecycleController interface {
	Controller
	StartupController
	ShutdownController
}

LifecycleController is a shorthand to represent a controller that implements both Startup and Shutdown methods.

type MutableValues

type MutableValues interface {
	Values
	Bind(Key, ValueSource) bool
	// TODO: want this in a helper method so it doesn't need to be re-implemented
	MustBind(Key, ValueSource)
}

func ChildValues

func ChildValues(parent Values, name string) MutableValues

func NewValues

func NewValues() MutableValues

func WithChildValues

func WithChildValues(ctx context.Context, name string) (context.Context, MutableValues)

type Registry

type Registry struct {
	MutableValues
	// contains filtered or unexported fields
}

func GetRegistry

func GetRegistry(vs Values) (*Registry, bool)

func New

func New(appName string, parent Values) *Registry

func (*Registry) AddController

func (r *Registry) AddController(c Controller)

AddController appends a controller instance to the list of those to register against a (gin) Router (typically an Engine)

func (*Registry) AddService

func (r *Registry) AddService(s Service) ServiceTag

AddService registers a Service instance to be affected by future calls to InitializeAll, StartAll, StopAll, and CleanupAll. It is an error (panic) to call AddService when StartAll is running.

func (*Registry) CleanupServices

func (r *Registry) CleanupServices(ctx context.Context) error

func (*Registry) Faults

func (r *Registry) Faults() *faults.Set

func (*Registry) InitializeServices

func (r *Registry) InitializeServices(ctx context.Context, client ent.EntClientBase) error

InitializeServices calls the Initialize method on all registered services, in parallel in goroutines.

func (*Registry) ReadyWaiter

func (r *Registry) ReadyWaiter(tag ServiceTag) <-chan struct{}

func (*Registry) RegisterControllers

func (r *Registry) RegisterControllers(router gin.IRouter) error

RegisterControllers notifies every added Controller to RegisterControllers itself with the given Router.

func (*Registry) RegisterMap

func (r *Registry) RegisterMap(router gin.IRouter, root string, endpoints HandlerMap) *gin.RouterGroup

RegisterMap is a helper for making many calls to RouterGroup.METHOD(...), using a table-driven approach. As a special case, if the Method in a map entry is "any", it will call RouterGroup.Any instead of a method-specific handler. As another special case, `nil` handlers will be replaced with a default handler that returns HTTP 501 Not Implemented.

func (*Registry) RequestStopServices

func (r *Registry) RequestStopServices()

RequestStopServices requests all running services stop, by cancelling their Context objects

func (*Registry) RunDefault

func (r *Registry) RunDefault(ctx context.Context, client ent.EntClientBase, logger *logging.Logger) error

func (*Registry) ServicesInitialized

func (r *Registry) ServicesInitialized() bool

func (*Registry) ServicesStarted

func (r *Registry) ServicesStarted() bool

func (*Registry) ShutdownControllers

func (r *Registry) ShutdownControllers(ctx context.Context, router gin.IRouter, s *http.Server) error

ShutdownControllers notifies all added ShutdownControllers that the given Router (typically an Engine) and Server combo are being shut down.

func (*Registry) StartControllers

func (r *Registry) StartControllers(ctx context.Context, router gin.IRouter, s *http.Server) error

StartControllers notifies all added StartupControllers that the given Router (typically an Engine) and Server combo are being shut down.

func (*Registry) StartServices

func (r *Registry) StartServices(ctx context.Context) error

StartServices starts all registered services in individual goroutines. It is an error to call StartServices if it is already running. StartServices returns once all services are started, it does not wait for them to complete. Use StopAll to request they stop and wait for the result.

func (*Registry) WaitAllReady

func (r *Registry) WaitAllReady(ctx context.Context) error

func (*Registry) WaitReadyByName

func (r *Registry) WaitReadyByName(ctx context.Context, name string) error

func (*Registry) WaitServices

func (r *Registry) WaitServices() error

WaitServices will wait for the running services, if any, to all end. It will return the resulting error, if any.

type Service

type Service interface {
	// Name describes the specific service for use in logging and status reports
	Name() string

	// Initialize should do any prep work for the service, but not actually start
	// it yet. The context should only be used for the duration of the initialization.
	Initialize(context.Context, *Registry, ent.EntClientBase) error

	// Start runs the service. It will be invoked on a goroutine, so it should
	// block and not return until the context is canceled, which is how the
	// service is requested to stop. The service must close the ready channel once
	// it is operational, so that any dependent services can know when they are OK
	// to proceed.
	Start(ctx context.Context, ready chan<- struct{}) error

	// Cleanup should release any resources acquired during Initialize. If another
	// service fails during Initialize, Cleanup may be called without Start ever
	// being called. If Start is called, Cleanup will not be called until after it
	// returns.
	Cleanup(context.Context, *Registry) error
}

type ServiceTag

type ServiceTag int

type ShutdownController

type ShutdownController interface {
	Controller
	Shutdown(context.Context, gin.IRouter, *http.Server) error
}

ShutdownController defines a controller that receives a notification for app shutdown, to do any cleanup it needs. Calls to Shutdown for each registered Controller may be run concurrently on multiple goroutines.

type StartupController

type StartupController interface {
	Controller
	Startup(context.Context, gin.IRouter, *http.Server) error
}

StartupController defines a controller that receives a notification for app Startup, to do any extra initialization it needs. Calls to Startup for each registered Controller may be run concurrently on multiple goroutines. Note that controllers MUST NOT register new routes or middleware here, they MUST do that in their Register function.

type TypedKey added in v0.5.69

type TypedKey[T any] interface {
	Key
	Value(Values) (value T, ok bool)
}

func Int64At

func Int64At(address string) TypedKey[int64]

deprecated: use ValueAt[int64]

func InterfaceAt

func InterfaceAt[T any](address string) TypedKey[T]

deprecated: use ValueAt[T]

func PointerAt

func PointerAt[T any](address string) TypedKey[*T]

deprecated: use ValueAt[*T]

func StringAt

func StringAt(address string) TypedKey[string]

deprecated: use ValueAt[T]

func ValueAt added in v0.6.0

func ValueAt[T any](address string) TypedKey[T]

type ValueSource

type ValueSource interface {
	Value(Values) any
	ValueType() reflect.Type
}

func Adapter added in v0.7.0

func Adapter[T, U any](vs ValueSource, f func(T) U) ValueSource

func Alias

func Alias(k Key) ValueSource

func ConstantValue

func ConstantValue(v any) ValueSource

type Values

type Values interface {
	Path() string
	ValueSource(Key) (ValueSource, bool)
	Value(Key) (any, bool)
}

func CachedValues

func CachedValues(vs Values) Values

func ContextValues

func ContextValues(ctx context.Context) Values

func WithCachedChildValues

func WithCachedChildValues(ctx context.Context, name string) (context.Context, Values)

Jump to

Keyboard shortcuts

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