Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrPanic = errors.New("panic")
ErrPanic is an error indicating a panic occurred.
var ErrValidation = errors.New("validation error")
ErrValidation is an error indicating a validation failure.
Functions ¶
This section is empty.
Types ¶
type Kernel ¶
type Kernel[T ServiceConfig] struct { RunE func(ctx context.Context, cfg T) error }
Kernel represents a service kernel with a run function.
func (*Kernel[T]) Bootstrap ¶
func (k *Kernel[T]) Bootstrap(ctx context.Context, opts ...Options) KernelBootstrap[T]
Bootstrap initializes the kernel with given options, setting up context and fetching configuration.
Example ¶
// Create a config struct and map using snout tags, env, json, yaml files could be used as well as envVars to // as data source to deserialize into the config struct type Config struct { Kafka struct { BrokerAddress string `snout:"broker_address"` ConsumerGroup string `snout:"consumer_group"` Topic string `snout:"topic"` } `snout:"kafka"` App struct { // ... } `snout:"app"` } Run := func(context.Context, Config) error { // wire your app all together using config struct fmt.Println("App Initialized") return nil } // Create your kernel struct with the function expecting a context and your config struct kernel := snout.Kernel[Config]{ RunE: Run, } // Pass a pointer to config to the kernel for it to be able to deserialize kernelBootstrap := kernel.Bootstrap(context.Background()) // Initialize your app and handle any error coming from it if err := kernelBootstrap.Initialize(); err != nil { if !errors.Is(err, context.Canceled) { panic(err) } }
Output: App Initialized
type KernelBootstrap ¶
type KernelBootstrap[T ServiceConfig] struct { // contains filtered or unexported fields }
KernelBootstrap holds the context, configuration, and run function for the kernel.
func (KernelBootstrap[T]) Initialize ¶
func (kb KernelBootstrap[T]) Initialize() (err error)
Initialize validates the configuration and runs the kernel.
type KernelOptions ¶
KernelOptions contains options for configuring the kernel.
func NewKernelOptions ¶
func NewKernelOptions() *KernelOptions
NewKernelOptions returns a new instance of KernelOptions with default values.
type Options ¶
type Options func(kernel *KernelOptions)
Options is a function type for configuring KernelOptions.
func WithEnvVarFolderLocation ¶
WithEnvVarFolderLocation sets the folder location for environment variable files in KernelOptions.
Example ¶
// Create a config struct and map using snout tags, env, json, yaml files could be used as well as envVars to // as data source to deserialize into the config struct type Config struct { Kafka struct { BrokerAddress string `snout:"broker_address"` ConsumerGroup string `snout:"consumer_group"` Topic string `snout:"topic"` } `snout:"kafka"` App struct { // ... } `snout:"app"` } Run := func(context.Context, Config) error { // wire your app all together using config struct fmt.Println("App Initialized with Config from Folder") return nil } // Create your kernel struct with the function expecting a context and your config struct kernel := snout.Kernel[Config]{ RunE: Run, } // Pass a pointer to config to the kernel for it to be able to deserialize kernelBootstrap := kernel.Bootstrap(context.Background(), snout.WithEnvVarFolderLocation("/etc/config/")) // Initialize your app and handle any error coming from it if err := kernelBootstrap.Initialize(); err != nil { if !errors.Is(err, context.Canceled) { panic(err) } }
Output: App Initialized with Config from Folder
func WithEnvVarPrefix ¶
WithEnvVarPrefix sets the environment variable prefix in KernelOptions.
Example ¶
// Create a config struct and map using snout tags, env, json, yaml files could be used as well as envVars to // as data source to deserialize into the config struct type Config struct { Kafka struct { BrokerAddress string `snout:"broker_address"` ConsumerGroup string `snout:"consumer_group"` Topic string `snout:"topic"` } `snout:"kafka"` App struct { // ... } `snout:"app"` } Run := func(context.Context, Config) error { // wire your app all together using config struct fmt.Println("App Initialized with EnvVar Prefix") return nil } // Create your kernel struct with the function expecting a context and your config struct kernel := snout.Kernel[Config]{ RunE: Run, } // Pass a pointer to config to the kernel for it to be able to deserialize kernelBootstrap := kernel.Bootstrap(context.Background(), snout.WithEnvVarPrefix("APP")) // Initialize your app and handle any error coming from it if err := kernelBootstrap.Initialize(); err != nil { if !errors.Is(err, context.Canceled) { panic(err) } }
Output: App Initialized with EnvVar Prefix
func WithServiceName ¶
WithServiceName sets the service name in KernelOptions.
Example ¶
// Create a config struct and map using snout tags, env, json, yaml files could be used as well as envVars to // as data source to deserialize into the config struct type Config struct { Kafka struct { BrokerAddress string `snout:"broker_address"` ConsumerGroup string `snout:"consumer_group"` Topic string `snout:"topic"` } `snout:"kafka"` App struct { // ... } `snout:"app"` } Run := func(context.Context, Config) error { // wire your app all together using config struct fmt.Println("App Initialized with Service Name") return nil } // Create your kernel struct with the function expecting a context and your config struct kernel := snout.Kernel[Config]{ RunE: Run, } // Pass a pointer to config to the kernel for it to be able to deserialize kernelBootstrap := kernel.Bootstrap( context.Background(), snout.WithServiceName("MyCustomServiceName"), ) // Initialize your app and handle any error coming from it if err := kernelBootstrap.Initialize(); err != nil { if !errors.Is(err, context.Canceled) { panic(err) } }
Output: App Initialized with Service Name
type ServiceConfig ¶
type ServiceConfig any
ServiceConfig is a generic type for service configuration.