You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
2.0 KiB
93 lines
2.0 KiB
package kratos
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
|
|
"github.com/go-kratos/kratos/v2/log"
|
|
"github.com/go-kratos/kratos/v2/registry"
|
|
"github.com/go-kratos/kratos/v2/transport"
|
|
)
|
|
|
|
// Option is an application option.
|
|
type Option func(o *options)
|
|
|
|
// options is an application options.
|
|
type options struct {
|
|
id string
|
|
name string
|
|
version string
|
|
metadata map[string]string
|
|
endpoints []string
|
|
|
|
ctx context.Context
|
|
sigs []os.Signal
|
|
|
|
logger log.Logger
|
|
registrar registry.Registrar
|
|
|
|
servers []transport.Server
|
|
|
|
before []func() error
|
|
after []func() error
|
|
}
|
|
|
|
// ID with service id.
|
|
func ID(id string) Option {
|
|
return func(o *options) { o.id = id }
|
|
}
|
|
|
|
// Name with service name.
|
|
func Name(name string) Option {
|
|
return func(o *options) { o.name = name }
|
|
}
|
|
|
|
// Version with service version.
|
|
func Version(version string) Option {
|
|
return func(o *options) { o.version = version }
|
|
}
|
|
|
|
// Metadata with service metadata.
|
|
func Metadata(md map[string]string) Option {
|
|
return func(o *options) { o.metadata = md }
|
|
}
|
|
|
|
// Endpoint with service endpoint.
|
|
func Endpoint(endpoints ...string) Option {
|
|
return func(o *options) { o.endpoints = endpoints }
|
|
}
|
|
|
|
// Context with service context.
|
|
func Context(ctx context.Context) Option {
|
|
return func(o *options) { o.ctx = ctx }
|
|
}
|
|
|
|
// Signal with exit signals.
|
|
func Signal(sigs ...os.Signal) Option {
|
|
return func(o *options) { o.sigs = sigs }
|
|
}
|
|
|
|
// Logger with service logger.
|
|
func Logger(logger log.Logger) Option {
|
|
return func(o *options) { o.logger = logger }
|
|
}
|
|
|
|
// Registrar with service registry.
|
|
func Registrar(r registry.Registrar) Option {
|
|
return func(o *options) { o.registrar = r }
|
|
}
|
|
|
|
// Server with transport servers.
|
|
func Server(srv ...transport.Server) Option {
|
|
return func(o *options) { o.servers = srv }
|
|
}
|
|
|
|
// Before before service starts.
|
|
func Before(fn func() error) Option {
|
|
return func(o *options) { o.before = append(o.before, fn) }
|
|
}
|
|
|
|
// After after services stops.
|
|
func After(fn func() error) Option {
|
|
return func(o *options) { o.after = append(o.after, fn) }
|
|
}
|
|
|