snout

package module
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2022 License: MIT Imports: 13 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{
		RunE: Run,
	}
	kernelBootstrap := kernel.Bootstrap(
	    context.Background(),
		new(Config),
	)
	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 = fmt.Errorf("panic")
View Source
var ErrValidation = fmt.Errorf("validation error")

Functions

This section is empty.

Types

type Kernel

type Kernel struct {
	RunE interface{}
}

func (*Kernel) Bootstrap

func (k *Kernel) Bootstrap(ctx context.Context, cfg interface{}, opts ...Options) kernelBootstrap

Bootstrap service creating a Ctx with Signalling and fetching EnvVars from env, yaml or json file, or straight from envVars from the OS.

type Options

type Options func(kernel *kernelOptions)

func WithEnvVarFolderLocation

func WithEnvVarFolderLocation(folderLocation string) Options

WithEnvVarFolderLocation Specify where to look up form the env var file.

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(ctx context.Context, config Config) {
	// wire your app all together using config struct
}

// Create your kernel struct with the function expecting a context and your config struct
kernel := snout.Kernel{
	RunE: Run,
}

// Pass a pointer to config to the kernel for it to be able to deserialize
kernelBootstrap := kernel.Bootstrap(context.Background(), new(Config), snout.WithEnvVarFolderLocation("/etc/config/"))

// Initialize your app and handle any error coming from it
if err := kernelBootstrap.Initialize(); err != nil {
	if err != context.Canceled {
		panic(err)
	}
}
Output:

func WithEnvVarPrefix

func WithEnvVarPrefix(prefix string) Options

WithEnvVarPrefix strips any prefix from os EnvVars to map it into Config struct.

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(ctx context.Context, config Config) {
	// wire your app all together using config struct
}

// Create your kernel struct with the function expecting a context and your config struct
kernel := snout.Kernel{
	RunE: Run,
}

// Pass a pointer to config to the kernel for it to be able to deserialize
kernelBootstrap := kernel.Bootstrap(context.Background(), new(Config), snout.WithEnvVarPrefix("APP"))

// Initialize your app and handle any error coming from it
if err := kernelBootstrap.Initialize(); err != nil {
	if err != context.Canceled {
		panic(err)
	}
}
Output:

func WithServiceName

func WithServiceName(name string) Options

WithServiceName creates a profile based on the service name to look up for envVar files

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(ctx context.Context, config Config) {
	// wire your app all together using config struct
}

// Create your kernel struct with the function expecting a context and your config struct
kernel := snout.Kernel{
	RunE: Run,
}

// Pass a pointer to config to the kernel for it to be able to deserialize
kernelBootstrap := kernel.Bootstrap(
	context.Background(),
	new(Config),
	snout.WithServiceName("MyCustomServiceName"),
)

// Initialize your app and handle any error coming from it
if err := kernelBootstrap.Initialize(); err != nil {
	if err != context.Canceled {
		panic(err)
	}
}
Output:

Jump to

Keyboard shortcuts

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