service

package
v0.0.0-...-2523971 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2019 License: MIT Imports: 8 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Name should hold the name of the instance and is used
	// for metrics and logging
	Name string

	// InputLoader is the configuration loader that should be used
	// by the service instance. To try multiple loaders see
	// config.ChainLoader(...)
	InputLoader config.Loader

	// DisableEnvSubstition can be set to disable the replacement
	// of environment variables used in the configuration content
	// returned by InputLoader. When environment variable substition
	// is enabled a service will use github.com/a8m/envsubst.String
	// during LoadConfig. If a custom substitution function is required
	// see ConfigTokenSubstutionFunc below
	DisableEnvSubstition bool

	// ConfigTokenSubstutionFunc can be used to specify an alternative
	// configuration token substition function. If unset, a service
	// may use - see DisableEnvSubstition above - github.com/a8m/envsubst.String
	// to substitute environment variables found in the configuration
	// data returned by InputLoader. Setting this field will overwrite the
	// DisableEnvSubstition settings and the user is responsible to taking
	// care of environment variable substition them self
	ConfigTokenSubstutionFunc config.TokenSubstitutionFunc

	// Directives holds a list of configuration directives that are supported
	// by the service instance. Note that directive setup functions will be called
	// in the order the are listed below and *not* in the order they are defined
	// in the configuration file.
	Directives []Directive

	// Modules holds a list of API modules that should be mounted
	// on the service router
	Modules []api.Module
	// contains filtered or unexported fields
}

Config is passed to NewInstance and is used to initialize a new service instance

type Directive

type Directive struct {
	// Name is the name of the directive as it will appear
	// in the configuration file
	Name string

	// Setup is called when the directive is used inside the configuratio
	// file. It receives the service instance as well as a token dispenser
	// for all occurences of the directive. For more information checkout
	// config.Parse.ForEachInOrder
	Init func(*Instance, config.Dispenser) error
}

Directive is a configuration directive

type Instance

type Instance struct {
	// Router holds the root api.Router that is used by the instance
	Router api.Router

	// Logger is the default logger used by the service instance
	// If not configured it will default to log.Log
	Logger log.Interface
	// contains filtered or unexported fields
}

func NewInstance

func NewInstance(cfg Config) *Instance

NewInstance creates a new instance from the given configration. Since this should be called at the beginning of main() anyway it panics if an error is encountered. If it panics, it will always use an error so you can safely assert any recover() to error If cfg has an InputLoader set it will be called and the returned input will be used as a configuration source. Any error that encounteres when parsing the configuration will cause a panic. If more control about configuration errors is required leave InputLoader empty and use LoadConfig() yourself

func (*Instance) AddProvider

func (i *Instance) AddProvider(key, value interface{})

AddProvider registers a new provider that will be made available via the service router

func (*Instance) AddRouterOption

func (i *Instance) AddRouterOption(opt api.RouterOption)

AddRouterOption adds a router option to the service instance Note that all options must be added before calling InitRouter

func (*Instance) InitRouter

func (i *Instance) InitRouter(options ...api.RouterOption) error

InitRouter initializes the API router used by the service instance. If Instance.Router is already configured an error will be returned Note that Instance.Router is *not* protected from concurrent access so InitRouter should be called in a safe state at the beginning

func (*Instance) InitRouterWithEngine

func (i *Instance) InitRouterWithEngine(e *gin.Engine, options ...api.RouterOption) error

InitRouterWithEngine is like InitRouter but allows to specify the underlying *gin.Engine by calling NewRouterWithEngine instead

func (*Instance) LoadConfig

func (i *Instance) LoadConfig(input config.Input) error

LoadConfig loads the configuration from input and initializes the service instance. Any error aborts configuration parsing and causes the function to return

func (*Instance) Name

func (i *Instance) Name() string

Name returns the name of the instance

func (*Instance) ServeHTTP

func (i *Instance) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the http.Handler interface and proxies any HTTP requests to the underlying api.Router. It's a convenience method for directly using http.ListenAndServe(":8080", instance)

Jump to

Keyboard shortcuts

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