snout

package module
v3.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2024 License: MIT Imports: 16 Imported by: 0

README

snout Go Reference

Bootstrap package for building Services in Go, Handle Signaling and Config coming from env, yaml or json files as well as envVars

Example



func main() {
	kernel := snout.Kernel[Config]{
		RunE: Run,
	}
	kernelBootstrap := kernel.Bootstrap(
	    context.Background(),
	)
	if err := kernelBootstrap.Initialize(); err != nil {
		if err != context.Canceled {
			panic(err)
		}
	}
}

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"`
}

func Run(ctx context.Context, cfg Config) error{
  //
  // ..  
  //
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrPanic = errors.New("panic")

ErrPanic is an error indicating a panic occurred.

View Source
var ErrValidation = errors.New("validation error")

ErrValidation is an error indicating a validation failure.

Functions

This section is empty.

Types

type Env

type Env struct {
	VarFile    string
	VarsPrefix string
}

Env represents the environment configuration.

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

type KernelOptions struct {
	ServiceName string
	Env         Env
}

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

func WithEnvVarFolderLocation(folderLocation string) Options

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

func WithEnvVarPrefix(prefix string) Options

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

func WithServiceName(name string) Options

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.

Jump to

Keyboard shortcuts

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