During the process of writing software in the team, you develop a certain style and define standards that meet the requirements for this software. These standards grow into libraries and frameworks. This is our approach based on interface-based programming and modular programming.
package main
import (
"github.com/goava/slice"
"github.com/goava/di"
// imports omitted
)
func main() {
slice.Run(
slice.WithName("grpc-service"),
slice.WithBundles(
logging.Bundle,
monitoring.Bundle,
grpc.Bundle,
),
slice.WithComponents(
slice.Provide(NewDispatcher, di.As(new(slice.Dispatcher))),
slice.Provide(grpcsrv.NewService, di.As(new(grpc.Service))),
),
)
}
The minimum that you need to run the application on slice
.
Use slice.WithName("your name")
to specify the application name.
slice.Run(
slice.WithName("sliced"),
// ...
)
Use environment variable ENV
to specify the application environment.
The value can be any string. Environments that have a prefix dev
will
be recognized as a development environment. Others will be recognized as
production.
Provide your own slice.Dispatcher
implementation:
slice.Run(
slice.WithName("sliced"),
slice.WithComponents(
slice.Provide(NewDispatcher, di.As(new(slice.Dispatcher))),
),
)
- Initializes
slice.StdLogger
- Checks application name
- Initializes
slice.Context
- Parses environment variables:
ENV
DEBUG
- Provide
slice.Env
- Initializes
slice.Info
- Checks bundle acyclic and sort dependencies
- Validates component signatures
- Resolves
slice.ParameterParser
- Collects parameters
- Checks
--parameters
flag - Provides parameters
- Resolves
slice.Logger
- Check dispatchers exists
- Sets
StartTimeout
andShutdownTimeout
- Runs interrupt signal catcher
- Invokes
BeforeStart
bundle hooks - Resolves dispatchers
- Run dispatchers
- Invokes
BeforeShutdown
bundle hooks in reverse order
Applications created with slice
support parameter parsing. By default,
it's processed by
envconfig.
You can use your own parameter parser. To do this, implement the
ParameterParser
interface and use it using the WithParameterParser()
or by setting the ParameterParser
field of the Application
.
You can print all parameters by using <binary-name> --parameters
.
Example output with default parameter parser and following structure:
// Parameters contains application configuration.
type Parameters struct {
Addr string `envconfig:"addr" required:"true" desc:"Server address"`
ReadTimeout time.Duration `envconfig:"read_timeout" required:"true" desc:"Server read timeout"`
WriteTimeout time.Duration `envconfig:"write_timeout" required:"true" desc:"Server write timeout"`
}
Output:
KEY TYPE DEFAULT REQUIRED DESCRIPTION
ADDR String true Server address
READ_TIMEOUT Duration true Server read timeout
WRITE_TIMEOUT Duration true Server write timeout
A Context carries a deadline, a cancellation signal, and other values across API boundaries.
Info contains information about application: name, env, debug.
TBD
TBD