Documentation ¶
Index ¶
- Constants
- Variables
- func NewInitializer(name string, ...) *initializingService
- func RegisterDefaultSignalListener(s *Registry)
- func WithValues(ctx context.Context, vs Values) context.Context
- type Controller
- type HandlerMap
- type Key
- type LifecycleController
- type MutableValues
- type Registry
- func (r *Registry) AddController(c Controller)
- func (r *Registry) AddService(s Service) ServiceTag
- func (r *Registry) CleanupServices(ctx context.Context) error
- func (r *Registry) Faults() *faults.Set
- func (r *Registry) InitializeServices(ctx context.Context, client ent.EntClient) error
- func (r *Registry) ReadyWaiter(tag ServiceTag) <-chan struct{}
- func (r *Registry) RegisterControllers(router gin.IRouter) error
- func (r *Registry) RegisterMap(router gin.IRouter, root string, endpoints HandlerMap) *gin.RouterGroup
- func (r *Registry) RequestStopServices()
- func (r *Registry) RunDefault(ctx context.Context, client ent.EntClient, logger *logging.Logger) error
- func (r *Registry) ServicesInitialized() bool
- func (r *Registry) ServicesStarted() bool
- func (r *Registry) ShutdownControllers(ctx context.Context, router gin.IRouter, s *http.Server) error
- func (r *Registry) StartControllers(ctx context.Context, router gin.IRouter, s *http.Server) error
- func (r *Registry) StartServices(ctx context.Context) error
- func (r *Registry) WaitAllReady(ctx context.Context) error
- func (r *Registry) WaitReadyByName(ctx context.Context, name string) error
- func (r *Registry) WaitServices() error
- type Service
- type ServiceTag
- type ShutdownController
- type StartupController
- type ValueSource
- type Values
Constants ¶
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 ¶
var RegistryKey = PointerAt("registry", (*Registry)(nil))
var ServiceNotFoundError = fmt.Errorf("service not found")
Functions ¶
func NewInitializer ¶
func NewInitializer( name string, initializer func(ctx context.Context, services *Registry, client ent.EntClient) 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 RegisterDefaultSignalListener ¶
func RegisterDefaultSignalListener(s *Registry)
Types ¶
type Controller ¶
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 ¶
func InterfaceAt ¶
InterfaceAt creates a binding key for an interface value. Pass (*T)(nil) for the nilp arg to get a binding for interface type T
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 }
func ChildValues ¶
func ChildValues(parent Values, name string) MutableValues
func NewValues ¶
func NewValues() MutableValues
func WithChildValues ¶
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
func GetRegistry ¶
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) InitializeServices ¶
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 ¶
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 (*Registry) ServicesInitialized ¶
func (*Registry) ServicesStarted ¶
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 ¶
StartControllers notifies all added StartupControllers that the given Router (typically an Engine) and Server combo are being shut down.
func (*Registry) StartServices ¶
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) WaitReadyByName ¶
func (*Registry) WaitServices ¶
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.EntClient) 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 ValueSource ¶
func Alias ¶
func Alias(k Key) ValueSource
func ConstantValue ¶
func ConstantValue(v interface{}) ValueSource
func Provider ¶
func Provider(f interface{}) ValueSource
type Values ¶
type Values interface { Path() string ValueSource(Key) (ValueSource, bool) Value(Key) (interface{}, bool) }