snout

package module
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2021 License: MIT Imports: 11 Imported by: 1

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(
		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:")

Functions

This section is empty.

Types

type Kernel

type Kernel struct {
	RunE interface{}
}

func (*Kernel) Bootstrap

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

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

type Options

type Options func(kernel *kernelOptions)

func WithEnvVarFolderLocation added in v1.1.1

func WithEnvVarFolderLocation(folderLocation string) Options

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

Example
package main

import (
	"context"

	"github.com/chiguirez/snout"
)

func main() {
	// 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(
		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 added in v1.1.1

func WithEnvVarPrefix(prefix string) Options

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

Example
package main

import (
	"context"

	"github.com/chiguirez/snout"
)

func main() {
	// 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(
		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 added in v1.1.1

func WithServiceName(name string) Options

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

Example
package main

import (
	"context"

	"github.com/chiguirez/snout"
)

func main() {
	// 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(
		new(Config),
		snout.WithServiceName("MyCustomServiceName"), // This will look up for any file under the envVarFolderLocation with this name
	)

	// 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