Documentation
¶
Overview ¶
Package vroomy builds HTTP/HTTPS services from registered Go plugins and TOML configuration. Register plugin instances before calling New or NewWithConfig, then call ListenUntilSignal to run the configured listeners and close the service when listening ends.
Plugins share a process-wide registry. Construction changes the process working directory, calls every plugin's Init, then injects dependencies and calls Load in dependency order. Handlers write responses through httpserve.Context.
Index ¶
- Constants
- func Register(key string, pi Plugin) error
- type BasePlugin
- type Config
- type Environment
- func (e Environment) Get(key string) (out string)
- func (e Environment) GetFloat64(key string) (out float64, err error)
- func (e Environment) GetInt(key string) (out int, err error)
- func (e Environment) GetInt64(key string) (out int64, err error)
- func (e Environment) GetTime(key, layout string) (out time.Time, err error)
- func (e Environment) GetTimeInLocation(key, layout string, loc *time.Location) (out time.Time, err error)
- func (e Environment) Must(key string) (out string, err error)
- func (e Environment) MustFloat64(key string) (out float64, err error)
- func (e Environment) MustInt(key string) (out int, err error)
- func (e Environment) MustInt64(key string) (out int64, err error)
- func (e Environment) MustTime(key, layout string) (out time.Time, err error)
- func (e Environment) MustTimeInLocation(key, layout string, loc *time.Location) (out time.Time, err error)
- type Flag
- type IncludeConfig
- type Plugin
- type Plugins
- type Response
- type Route
- type RouteGroup
- type Vroomy
Examples ¶
Constants ¶
const ( // ErrProtectedFlag is a legacy sentinel; the current code does not return it. ErrProtectedFlag = errors.Error("cannot use protected flag") // ErrInvalidHostPolicy is returned when the HostPolicy does not match the intended signature ErrInvalidHostPolicy = errors.Error("invalid HostPolicy handler within the autocert plugin") )
const ( // ErrInvalidTLSDirectory is returned by Listen when TLS is enabled without // TLSDir or both AutoCertDir and AutoCertHosts. ErrInvalidTLSDirectory = errors.Error("invalid tls directory, cannot be empty when tls port has been set") // ErrInvalidPreInitFunc is a legacy sentinel; lifecycle signatures are now // enforced by the Plugin interface, and the current code does not return it. ErrInvalidPreInitFunc = errors.Error("unsupported header for Init func encountered") // ErrInvalidLoadFunc is a legacy sentinel not returned by the current code. ErrInvalidLoadFunc = errors.Error("unsupported header for Load func encountered") // ErrNotAddressable is returned when a dependency destination cannot be set. ErrNotAddressable = errors.Error("provided backend must be addressable") // ErrInvalidDir is a legacy sentinel not returned by the current code. ErrInvalidDir = errors.Error("invalid directory, cannot be empty") // ErrPluginKeyExists is a legacy sentinel; duplicate keys return a formatted error. ErrPluginKeyExists = errors.Error("plugin cannot be added, key already exists") // ErrPluginNotLoaded is a legacy sentinel; missing plugins return formatted errors. ErrPluginNotLoaded = errors.Error("plugin with that key has not been loaded") // ErrExpectedEndParen is returned when an ending parenthesis is missing ErrExpectedEndParen = errors.Error("expected ending parenthesis") // ErrInvalidPluginHandler is a legacy sentinel; invalid signatures return formatted errors. ErrInvalidPluginHandler = errors.Error("plugin handler not valid") )
const ( // ErrGroupNotFound is returned when a group cannot be found by name ErrGroupNotFound = errors.Error("group not found") )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type BasePlugin ¶ added in v0.11.0
type BasePlugin struct{}
BasePlugin supplies no-op lifecycle methods and a nil backend for embedding.
func (*BasePlugin) Backend ¶ added in v0.11.0
func (b *BasePlugin) Backend() interface{}
Backend returns nil. Override it when another plugin needs this plugin's backend.
func (*BasePlugin) Close ¶ added in v0.11.0
func (b *BasePlugin) Close() error
Close performs no cleanup.
func (*BasePlugin) Init ¶ added in v0.11.0
func (b *BasePlugin) Init(env Environment) error
Init performs no initialization.
func (*BasePlugin) Load ¶ added in v0.11.0
func (b *BasePlugin) Load(env Environment) error
Load performs no dependency-dependent setup.
type Config ¶ added in v0.15.0
type Config struct {
Name string `toml:"name"`
Dir string `toml:"dir"`
Port uint16 `toml:"port"`
// TLSPort to listen on. To use TLS one of the two must be set:
// - TLSDir
// - AutoCertHosts/AutoCertDir
TLSPort uint16 `toml:"tlsPort"`
TLSDir string `toml:"tlsDir"`
AllowNonTLS bool `toml:"allowNonTLS"`
IncludeConfig
// Flags is a legacy field with no active runtime consumer.
Flags map[string]string `toml:"-"`
// Plugins stores import-path metadata with no active runtime consumer.
// It is separate from IncludeConfig.Plugins and does not filter the registry.
Plugins []string `toml:"plugins"`
// ErrorLogger receives httpserve request errors; initialization/listen errors
// are returned to the caller instead.
ErrorLogger func(error) `toml:"-"`
}
Config configures a Vroomy service. NewWithConfig retains and mutates this value.
func NewConfig ¶ added in v0.15.0
NewConfig decodes loc and its includes, defaults Dir to "./", and adds process environment entries whose keys are absent from the configured environment. File paths are relative to the current working directory, not loc's directory.
func (*Config) GetFilepath ¶ added in v0.19.0
GetFilepath returns config.toml within the CONFIG_PATH process environment directory, or the current directory if CONFIG_PATH is unset. It does not use c.
func (*Config) GetRouteGroup ¶ added in v0.15.0
func (c *Config) GetRouteGroup(name string) (g *RouteGroup, err error)
GetRouteGroup returns the first group matching name, or ErrGroupNotFound. An empty name returns nil, nil to select the root group.
type Environment ¶ added in v0.13.0
Environment holds string configuration values shared with plugin lifecycle hooks.
func (Environment) Get ¶ added in v0.13.0
func (e Environment) Get(key string) (out string)
Get returns the value for key, or an empty string if key is absent.
Example ¶
val := exampleEnvironment.Get("foo")
fmt.Println("Value of foo is", val)
func (Environment) GetFloat64 ¶ added in v0.18.0
func (e Environment) GetFloat64(key string) (out float64, err error)
GetFloat64 parses a float64, returning zero and nil if key is absent.
Example ¶
var (
val float64
err error
)
if val, err = exampleEnvironment.GetFloat64("foo"); err != nil {
// Handle error here
return
}
fmt.Println("Value of foo is", val)
func (Environment) GetInt ¶ added in v0.18.0
func (e Environment) GetInt(key string) (out int, err error)
GetInt parses a decimal int, returning zero and nil if key is absent.
Example ¶
var (
val int
err error
)
if val, err = exampleEnvironment.GetInt("foo"); err != nil {
// Handle error here
return
}
fmt.Println("Value of foo is", val)
func (Environment) GetInt64 ¶ added in v0.18.0
func (e Environment) GetInt64(key string) (out int64, err error)
GetInt64 parses a decimal int64, returning zero and nil if key is absent.
Example ¶
var (
val int64
err error
)
if val, err = exampleEnvironment.GetInt64("foo"); err != nil {
// Handle error here
return
}
fmt.Println("Value of foo is", val)
func (Environment) GetTime ¶ added in v0.18.0
func (e Environment) GetTime(key, layout string) (out time.Time, err error)
GetTime parses a time using layout, returning the zero time and nil if key is absent.
Example ¶
var (
val time.Time
err error
)
if val, err = exampleEnvironment.GetTime("foo", "2006-01-02"); err != nil {
// Handle error here
return
}
fmt.Println("Value of foo is", val)
func (Environment) GetTimeInLocation ¶ added in v0.18.0
func (e Environment) GetTimeInLocation(key, layout string, loc *time.Location) (out time.Time, err error)
GetTimeInLocation parses a time using layout and loc, returning the zero time and nil if key is absent.
Example ¶
var (
val time.Time
err error
)
if val, err = exampleEnvironment.GetTimeInLocation("foo", "2006-01-02", time.Local); err != nil {
// Handle error here
return
}
fmt.Println("Value of foo is", val)
func (Environment) Must ¶ added in v0.13.0
func (e Environment) Must(key string) (out string, err error)
Must returns an error if key is absent. A present empty string is accepted; despite its name, this method does not panic.
Example ¶
var (
val string
err error
)
if val, err = exampleEnvironment.Must("foo"); err != nil {
// Handle error here
return
}
fmt.Println("Value of foo is", val)
func (Environment) MustFloat64 ¶ added in v0.18.0
func (e Environment) MustFloat64(key string) (out float64, err error)
MustFloat64 requires key to be present and parses its value as a float64.
Example ¶
var (
val float64
err error
)
if val, err = exampleEnvironment.MustFloat64("foo"); err != nil {
// Handle error here
return
}
fmt.Println("Value of foo is", val)
func (Environment) MustInt ¶ added in v0.18.0
func (e Environment) MustInt(key string) (out int, err error)
MustInt requires key to be present and parses its value as a decimal int.
Example ¶
var (
val int
err error
)
if val, err = exampleEnvironment.MustInt("foo"); err != nil {
// Handle error here
return
}
fmt.Println("Value of foo is", val)
func (Environment) MustInt64 ¶ added in v0.18.0
func (e Environment) MustInt64(key string) (out int64, err error)
MustInt64 requires key to be present and parses its value as a decimal int64.
Example ¶
var (
val int64
err error
)
if val, err = exampleEnvironment.MustInt64("foo"); err != nil {
// Handle error here
return
}
fmt.Println("Value of foo is", val)
func (Environment) MustTime ¶ added in v0.18.0
func (e Environment) MustTime(key, layout string) (out time.Time, err error)
MustTime requires key to be present and parses its value using layout.
Example ¶
var (
val time.Time
err error
)
if val, err = exampleEnvironment.MustTime("foo", "2006-01-02"); err != nil {
// Handle error here
return
}
fmt.Println("Value of foo is", val)
func (Environment) MustTimeInLocation ¶ added in v0.18.0
func (e Environment) MustTimeInLocation(key, layout string, loc *time.Location) (out time.Time, err error)
MustTimeInLocation requires key to be present and parses its value using layout and loc.
Example ¶
var (
val time.Time
err error
)
if val, err = exampleEnvironment.MustTimeInLocation("foo", "2006-01-02", time.Local); err != nil {
// Handle error here
return
}
fmt.Println("Value of foo is", val)
type Flag ¶ added in v0.15.0
type Flag struct {
Name string `toml:"name"`
DefaultValue string `toml:"defaultValue"`
Usage string `toml:"usage"`
}
Flag represents a legacy TOML flag declaration. The current runtime does not install or apply these declarations.
type IncludeConfig ¶ added in v0.15.0
type IncludeConfig struct {
AutoCertHosts []string `toml:"autoCertHosts"`
AutoCertDir string `toml:"autoCertDir"`
// Application environment
Environment map[string]string `toml:"env"`
// Include lists paths to load. Entries merged from included files are appended
// but are not traversed by the current loadIncludes pass.
Include []string `toml:"include"`
// Plugins stores included plugin entries; it does not populate the outer
// Config.Plugins field or limit which registered plugins run.
Plugins []string `toml:"plugins"`
// FlagEntries stores legacy flag declarations; no flag parser consumes them.
FlagEntries []*Flag `toml:"flag"`
// Groups are the route groups
Groups []*RouteGroup `toml:"group"`
// Routes are the routes to listen for and serve
Routes []*Route `toml:"route"`
}
IncludeConfig contains the configuration fields accepted in included TOML files.
type Plugin ¶ added in v0.11.0
type Plugin interface {
// Init runs before dependency injection, in unspecified plugin order.
Init(env Environment) error
// Load runs after dependencies have been assigned and their plugins loaded.
Load(env Environment) error
// Backend returns the value injected into consumers' tagged fields.
Backend() interface{}
// Close releases plugin resources. Plugin close order is unspecified.
Close() error
}
Plugin provides lifecycle hooks and an optional shared backend. Register a non-nil pointer to a struct before constructing the service.
type Plugins ¶ added in v0.11.0
type Plugins struct {
// contains filtered or unexported fields
}
Plugins manages a plugin registry. Services use the package-level registry via Register; the zero value is not ready for registration.
func (*Plugins) Close ¶ added in v0.11.0
Close closes registered plugins in unspecified order and marks the registry closed.
func (*Plugins) Loaded ¶ added in v0.11.0
Loaded returns a copy of the registry map, sharing the same plugin instances.
func (*Plugins) Register ¶ added in v0.11.0
Register records a plugin under a unique key without initializing it.
type Response ¶ added in v0.15.0
type Response struct {
StatusCode int
ContentType string
Value interface{}
// Legacy adoption and callback metadata.
Adopted bool
Callback string
}
Response holds legacy response metadata. The current plugin handler resolver does not consume it; handlers write through httpserve.Context instead.
func NewAdopedtResponse ¶ added in v0.15.0
func NewAdopedtResponse() *Response
NewAdopedtResponse returns a Response with Adopted set to true. The exported name retains its historical spelling.
func NewResponse ¶ added in v0.15.0
NewResponse will return a new Response
type Route ¶ added in v0.15.0
type Route struct {
// HTTPHandlers are Go-supplied handlers; resolved Handlers are appended to them.
HTTPHandlers []httpserve.Handler `toml:"-"`
// Route name/description
Name string `toml:"name"`
// Route group
Group string `toml:"group"`
// Method selects PUT, POST, DELETE, or OPTIONS case-insensitively; other values use GET.
Method string `toml:"method"`
// HTTP path
HTTPPath string `toml:"httpPath"`
// Target is a legacy file/directory field with no current serving implementation.
Target string `toml:"target"`
// Plugin handlers
Handlers []string `toml:"handlers"`
}
Route represents a listening route
type RouteGroup ¶ added in v0.15.0
type RouteGroup struct {
Name string `toml:"name"`
// Route group
Group string `toml:"group"`
// Method is decoded but does not restrict the group's routes.
Method string `toml:"method"`
// HTTP path
HTTPPath string `toml:"httpPath"`
// Plugin handlers
Handlers []string `toml:"handlers"`
// HTTPHandlers are Go-supplied handlers; resolved Handlers are appended to them.
HTTPHandlers []httpserve.Handler `toml:"-"`
// G is populated during group initialization.
G httpserve.Group `toml:"-"`
}
RouteGroup represents a route group
type Vroomy ¶ added in v0.9.0
type Vroomy struct {
// contains filtered or unexported fields
}
Vroomy manages the web service
func New ¶
New loads a configuration file, defaults a missing dataDir environment key to "data", and constructs the service with NewWithConfig. It does not start listeners.
func NewWithConfig ¶ added in v0.9.0
NewWithConfig changes the process working directory to cfg.Dir, initializes registered plugins and dependencies, and creates routes. It retains and mutates cfg without loading includes or supplying defaults. Listeners start in Listen.
func (*Vroomy) Close ¶ added in v0.9.0
Close closes the service's HTTP server and plugins in unspecified order. Repeated calls return errors.ErrIsClosed. The separate HTTPS redirect listener is not retained by the service and is not closed here.
func (*Vroomy) Listen ¶ added in v0.9.0
Listen starts the configured HTTP/HTTPS listeners and blocks until a listener returns or ctx ends. Cancellation returns ctx.Err() without closing the service.
func (*Vroomy) ListenUntilSignal ¶ added in v0.17.5
ListenUntilSignal cancels listening on SIGINT, SIGTERM, or SIGABRT, then closes the service. It also closes when ctx ends or a listener returns, and suppresses context.Canceled while preserving other listen and close errors.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
hello
command
Command hello serves the routes in config.example.toml using a local plugin.
|
Command hello serves the routes in config.example.toml using a local plugin. |
|
hello/plugins
Package plugins registers the hello application's plugins when imported.
|
Package plugins registers the hello application's plugins when imported. |
