app

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: May 15, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ContextKeyEnvironment ContextKey = "srvc:environment"
	EnvProduction         string     = "production"
	EnvDevelopment        string     = "development"
	EnvTest               string     = "test"

	DefaultConfigAddress      string = ":8000"
	DefaultConfigPIDFile      string = "tmp/server.pid"
	DefaultConfigReadTimeout  string = "30s"
	DefaultConfigWriteTimeout string = "30s"

	ConfigValueExternalPrefix string = "ext:"
)

Variables

View Source
var (
	ErrNotInitialized = errors.New(
		"it seems like you haven't initialized your service. Please use " +
			"`srvc init` before running this command",
	)
)

Functions

func DefaultRunner

func DefaultRunner(ctx Context, _ *Config) error

DefaultRunner prints out a message prompting how to get started with srvc.

func LoadVariableFromEnv

func LoadVariableFromEnv(ctx Context, name string) (string, error)

LoadVariableFromEnv loads a given variable by name from the environment.

The func throws an error if the variable is not in the environment.

Types

type App

type App struct {
	// Runner is your application code. You are provided the context as well as
	// the configuration for the application.
	//
	// `srvc` handles loading your configuration when the app is run for you.
	// You can specify how to load configuration values you can't hardcode in
	// your configuration files.
	//
	// By default, it will use app.DefaultRunner() as the application runner.
	Runner func(Context, *Config) error

	// LoadVariable allows the application to load a variable via a user-defined
	// function.
	//
	// By default, it will use app.LoadVariableFromEnv() to load variables via
	// the environment.
	//
	// Config files with variable values in the "ext:XXX" format will be loaded
	// via this func, providing "XXX" as the string.
	//
	// If the configuration value is missing from the configuration file, then
	// that variable is also passed to this function.
	LoadVariable LoadVariableFunc
}

App is the structure that lets users define their application.

Any struct values that aren't provided by the user will be defaulted.

func (*App) InitializeDefaults

func (a *App) InitializeDefaults()

InitializeDefaults initializes the app for any fields that are not provided by the user.

func (*App) ResolveConfig

func (a *App) ResolveConfig(ctx Context, file fs.File) (*Config, error)

Resolve Config resolves the configuration values for an environment and the details of a configuration file.

type Config

type Config struct {
	// Address is the address to operate a server at, in the format ":####".
	// Default: ":8000"
	Address string `yaml:"address"`
	// PIDFile is the file name to store the PID of the running app.
	// Default: "tmp/server.pid"
	PIDFile string `yaml:"pid_file"`
	// 	Environment is the environment the app is running in.
	Environment string `yaml:"env"`
	// ReadTimeout is the read timeout, if running a server, in time.Duration
	// format.
	// Default: "30s"
	ReadTimeout string `yaml:"read_timeout"`
	// WriteTimeout is the write timeout, if running a server, in time.Duration
	// format.
	// Default: "30s"
	WriteTimeout string `yaml:"write_timeout"`
	// X contains any additinal custom key/vals you want available in your
	// configuration.
	X map[string]string `yaml:"x"`
}

Config is a struct that defines common fields used in Go applications.

type ConfigFile

type ConfigFile struct {
	Default     Config `yaml:"default"`
	Test        Config `yaml:"test"`
	Development Config `yaml:"development"`
	Production  Config `yaml:"production"`
}

ConfigFile defines the file format of the config file

func LoadConfigFile

func LoadConfigFile(file fs.File) (*ConfigFile, error)

LoadConfigFile loads the contents of the config file into the ConfigFile struct.

type Context

type Context interface {
	Version() string
	context.Context
}

type ContextKey

type ContextKey string

type DefaultContext added in v0.0.2

type DefaultContext struct {
	Ver string
	context.Context
}

func (*DefaultContext) Version added in v0.0.2

func (c *DefaultContext) Version() string

type ErrVariableNotFound

type ErrVariableNotFound struct {
	Name string
	Err  error
}

func (*ErrVariableNotFound) Error

func (e *ErrVariableNotFound) Error() string

type LoadVariableFunc

type LoadVariableFunc func(Context, string) (string, error)

LoadVariableFunc is a function that helps load variables

Jump to

Keyboard shortcuts

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