2021-02-17 11:14:47 +02:00
|
|
|
package kratos
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-31 10:10:17 +02:00
|
|
|
"net/url"
|
2021-02-17 11:14:47 +02:00
|
|
|
"os"
|
2021-07-27 09:35:47 +02:00
|
|
|
"time"
|
2021-02-17 11:14:47 +02:00
|
|
|
|
|
|
|
"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
|
2021-05-31 10:10:17 +02:00
|
|
|
endpoints []*url.URL
|
2021-02-17 11:14:47 +02:00
|
|
|
|
|
|
|
ctx context.Context
|
|
|
|
sigs []os.Signal
|
|
|
|
|
2021-07-27 09:35:47 +02:00
|
|
|
logger log.Logger
|
|
|
|
registrar registry.Registrar
|
|
|
|
registrarTimeout time.Duration
|
|
|
|
servers []transport.Server
|
2021-02-17 11:14:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2021-05-31 10:10:17 +02:00
|
|
|
func Endpoint(endpoints ...*url.URL) Option {
|
2021-02-17 11:14:47 +02:00
|
|
|
return func(o *options) { o.endpoints = endpoints }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Context with service context.
|
|
|
|
func Context(ctx context.Context) Option {
|
|
|
|
return func(o *options) { o.ctx = ctx }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Logger with service logger.
|
|
|
|
func Logger(logger log.Logger) Option {
|
|
|
|
return func(o *options) { o.logger = logger }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Server with transport servers.
|
|
|
|
func Server(srv ...transport.Server) Option {
|
|
|
|
return func(o *options) { o.servers = srv }
|
|
|
|
}
|
2021-05-29 17:52:05 +02:00
|
|
|
|
|
|
|
// Signal with exit signals.
|
|
|
|
func Signal(sigs ...os.Signal) Option {
|
|
|
|
return func(o *options) { o.sigs = sigs }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Registrar with service registry.
|
|
|
|
func Registrar(r registry.Registrar) Option {
|
|
|
|
return func(o *options) { o.registrar = r }
|
|
|
|
}
|
2021-07-27 09:35:47 +02:00
|
|
|
|
|
|
|
// RegistrarTimeout with registrar timeout.
|
|
|
|
func RegistrarTimeout(t time.Duration) Option {
|
|
|
|
return func(o *options) { o.registrarTimeout = t }
|
|
|
|
}
|