### Initialize Debug Server in Main Source: https://github.com/agurinov/gopl/blob/main/http/handlers/README.md Demonstrates how to start the debug server within the application command lifecycle. ```go // main.go import cmd "github.com/agurinov/gopl/appcmd" const cmdName = "api" func main() { ctx, stop, logger, err := cmd.Prepare(cmdName) if err != nil { logger.Fatal("can't init cmd", zap.Error(err)) } defer stop() di := newDI(ctx, cfg) cmd.Start( ctx, logger, di.debugServer.ListenAndServe, ) } ``` -------------------------------- ### Implement Functional Options Source: https://github.com/agurinov/gopl/blob/main/patterns/creational/README.md Provides implementation examples for functional options, including context-aware variants. ```go // component_f_opt.go import ( "go.uber.org/zap" ) func WithLogger(logger *zap.Logger) MyComponentOption { return func(c *MyComponent) error { if logger == nil { return nil } c.logger = logger.Named("my.component") return nil } } func WithContextLogger(logger *zap.Logger) MyComponentOption { return func(ctx context.Context, c *MyComponent) error { if logger == nil { return nil } c.logger = logger.Named("my.component") return nil } } ``` -------------------------------- ### Start Prober in Application Entry Point Source: https://github.com/agurinov/gopl/blob/main/diag/probes/README.md Shows how to integrate the prober's run method into the application startup command. ```go // main.go import cmd "github.com/agurinov/gopl/appcmd" const cmdName = "api" func main() { ctx, stop, logger, err := cmd.Prepare(cmdName) if err != nil { logger.Fatal("can't init cmd", zap.Error(err)) } defer stop() di := newDI(ctx, cfg) cmd.Start( ctx, logger, di.prober.Run, ) } ``` -------------------------------- ### Initialize and Configure Prober in DI Container Source: https://github.com/agurinov/gopl/blob/main/diag/probes/README.md Demonstrates setting up a Prober with custom intervals, timeouts, and readiness checks within a dependency injection container. ```go // di.go import ( "github.com/agurinov/gopl/diag/probes" c "github.com/agurinov/gopl/patterns/creational" "github.com/agurinov/gopl/http" "github.com/agurinov/gopl/http/handlers" ) type diContainer struct { prober *probes.Prober debugServer http.Server } prober := c.Must( probes.New( probes.WithLogger(logger), probes.WithCheckInterval(5*time.Second), probes.WithCheckTimeout(time.Second), ), ) // Good idea to stop traffic from orchestrator via readiness probe. closer.AddCloser( run.SimpleFn(prober.Close), graceful.InFirstWave(), ) // Prober can be embedded as handler in debug http server to be used by orchestrator. // About debugServer and debugHandler look docs from those packages. debugHandler := c.Must( handlers.New( handlers.WithDebugProber(prober), ), ) debugServer := c.Must( http.NewServer( http.WithServerHandler(debugHandler.Handler()), ), ) sqlx := c.Must( // ... ) // db is a critical component. // No ping -> no readiness probe -> no traffic from orchestrator. prober.WithReadinessProbe(sqlx.Ping) // After all dependency tree is ready we talk to orchestrator that app is started. prober.SetStartup(true) return diContainer{ debugServer: debugServer, prober: prober, } ``` -------------------------------- ### Define application entry point Source: https://github.com/agurinov/gopl/blob/main/README.md The executable entry point for containerized applications, intended for minimal bootstrap logic. ```go // main.go // The executable entry point that runs inside a container. // Minimal logic — just bootstrap and start. ``` -------------------------------- ### Registering Closers in DI Container Source: https://github.com/agurinov/gopl/blob/main/graceful/README.md Demonstrates initializing the graceful.Closer with functional options and registering various service cleanup functions during dependency injection. ```go // di.go import ( c "github.com/agurinov/gopl/patterns/creational" "github.com/agurinov/gopl/graceful" "github.com/agurinov/gopl/run" ) type diContainer struct { closer *graceful.Closer } closer := c.Must( graceful.NewCloser( graceful.WithLogger(logger), graceful.WithTimeout(30*time.Second), ), ) svc1 := c.Must( // ... ) closer.AddCloser( svc1.Close, graceful.InFirstWave(), ) svc2 := c.Must( // ... ) closer.AddCloser( run.SimpleFn(svc2.Close), ) svc3 := c.Must( // ... ) closer.AddCloser( run.ErrorFn(svc3.Close), ) return diContainer{ closer: closer, } ``` -------------------------------- ### Triggering Shutdown in Main Source: https://github.com/agurinov/gopl/blob/main/graceful/README.md Shows how to integrate the closer's WaitForShutdown method into the application command lifecycle. ```go // main.go import cmd "github.com/agurinov/gopl/appcmd" const cmdName = "api" func main() { ctx, stop, logger, err := cmd.Prepare(cmdName) if err != nil { logger.Fatal("can't init cmd", zap.Error(err)) } defer stop() di := newDI(ctx, cfg) cmd.Start( ctx, logger, di.closer.WaitForShutdown, ) } ``` -------------------------------- ### Configure dependency injection Source: https://github.com/agurinov/gopl/blob/main/README.md Explicit construction of the dependency graph to ensure transparent and refactor-friendly service wiring. ```go // di.go // Explicit construction of the full dependency graph. // All services, repositories, clients and middleware are wired together in a single, readable place — making dependency flow transparent and refactor-friendly. ``` -------------------------------- ### Configure Debug Handler with DI Source: https://github.com/agurinov/gopl/blob/main/http/handlers/README.md Sets up the debug handler using a dependency injection container, including atomic logging levels and custom middlewares. ```go // di.go import ( c "github.com/agurinov/gopl/patterns/creational" "github.com/agurinov/gopl/http/handlers" ) type diContainer struct { debugServer http.Server } // Zap atomic level to be changed on the fly. logger, lvl := c.MustDuo( cfg.Logger.New(), ) nopanicHandler := c.Must( nopanic.NewHandler(), ) // Orchestrator prober. prober := c.Must( cfg.Probes.NewProber(), ) debugHandler := c.Must( handlers.NewDebug( handlers.WithDebugAtomicLevel(lvl), handlers.WithDebugLogger(logger), handlers.WithDebugCustomMiddlewares( nopanicHandler.Middleware(), ), handlers.WithDebugProber(prober), ), ) // Server to serve all debugHandler endpoints. debugServer := c.Must( cfg.HTTP.NewServer(), ) return diContainer{ debugServer: debugServer, } ``` -------------------------------- ### Define Component Options Source: https://github.com/agurinov/gopl/blob/main/patterns/creational/README.md Defines the structure and option types for a component using the creational pattern. ```go // component.go import ( "go.uber.org/zap" c "github.com/agurinov/gopl/patterns/creational" ) type ( MyComponent struct { logger *zap.Logger s string i int } // Or it can alias as: MyComponentOption = c.Option[Closer] MyComponentOption c.Option[Closer] // Or it can alias as: MyCtxComponentOption = c.OptionWithContext[Closer] MyCtxComponentOption c.OptionWithContext[Closer] ) var ( // Creating new oject with values from provided options. New = c.New[MyComponent, MyComponentOption] // Creating and validating resulting object. // Object must have .Valdate() method. New = c.NewWithValidate[MyComponent, MyComponentOption] // Creating new oject with available context to create some socket. New = c.NewWithContext[MyComponent, MyCtxComponentOption] // Creating and validating new oject with available context to create some socket. // Object must have .Valdate() method. New = c.NewWithContextValidate[MyComponent, MyCtxComponentOption] ) // Also there are pair of methods Construct* which can patch already existing object. // Useful in case of known default state. // Feel free to combine ctx and validate mechanics as you want. func New(opts ...MyComponentOption) (*MyComponent, error) { c := MyComponent{ s: "foobar", i: 100500, } obj, err := c.ConstructWithValidate(c, opts...) if err != nil { return nil, err } return &obj, nil } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.