2019-06-06 18:37:40 +03:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2020-06-24 13:46:51 +03:00
|
|
|
"context"
|
|
|
|
|
2020-08-29 17:44:49 +03:00
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
2021-01-29 13:17:32 +03:00
|
|
|
"github.com/unistack-org/micro/v3/register"
|
2021-08-20 22:40:48 +03:00
|
|
|
"github.com/unistack-org/micro/v3/util/id"
|
2019-06-06 18:37:40 +03:00
|
|
|
)
|
|
|
|
|
2019-06-13 14:09:49 +03:00
|
|
|
// Options are router options
|
2019-06-06 18:37:40 +03:00
|
|
|
type Options struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
Logger logger.Logger
|
|
|
|
Context context.Context
|
2021-01-29 13:17:32 +03:00
|
|
|
Register register.Register
|
2021-03-06 19:45:13 +03:00
|
|
|
Name string
|
|
|
|
Gateway string
|
|
|
|
Network string
|
2021-09-30 21:13:13 +03:00
|
|
|
ID string
|
2021-03-06 19:45:13 +03:00
|
|
|
Address string
|
2020-08-06 13:32:06 +03:00
|
|
|
Precache bool
|
2019-06-06 18:37:40 +03:00
|
|
|
}
|
|
|
|
|
2021-09-30 21:13:13 +03:00
|
|
|
// ID sets Router Id
|
|
|
|
func ID(id string) Option {
|
2019-06-10 21:50:54 +03:00
|
|
|
return func(o *Options) {
|
2021-09-30 21:13:13 +03:00
|
|
|
o.ID = id
|
2019-06-10 21:50:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 17:12:07 +03:00
|
|
|
// Address sets router service address
|
2019-06-07 19:20:22 +03:00
|
|
|
func Address(a string) Option {
|
2019-06-07 15:29:09 +03:00
|
|
|
return func(o *Options) {
|
2019-06-07 19:20:22 +03:00
|
|
|
o.Address = a
|
2019-06-07 15:29:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 17:45:42 +03:00
|
|
|
// Gateway sets network gateway
|
|
|
|
func Gateway(g string) Option {
|
2019-06-10 21:50:54 +03:00
|
|
|
return func(o *Options) {
|
2019-07-09 17:45:42 +03:00
|
|
|
o.Gateway = g
|
2019-06-10 21:50:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 17:45:42 +03:00
|
|
|
// Network sets router network
|
|
|
|
func Network(n string) Option {
|
2019-06-28 00:52:51 +03:00
|
|
|
return func(o *Options) {
|
2019-07-09 17:45:42 +03:00
|
|
|
o.Network = n
|
2019-06-28 00:52:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-29 17:44:49 +03:00
|
|
|
// Logger sets the logger
|
|
|
|
func Logger(l logger.Logger) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Logger = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
// Register sets the local register
|
|
|
|
func Register(r register.Register) Option {
|
2019-06-07 19:20:22 +03:00
|
|
|
return func(o *Options) {
|
2021-01-29 13:17:32 +03:00
|
|
|
o.Register = r
|
2019-06-07 15:29:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 13:32:06 +03:00
|
|
|
// Precache the routes
|
|
|
|
func Precache() Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Precache = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
// Name of the router
|
|
|
|
func Name(n string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Name = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-17 15:44:35 +03:00
|
|
|
// NewOptions returns router default options
|
|
|
|
func NewOptions(opts ...Option) Options {
|
|
|
|
options := Options{
|
2021-09-30 21:13:13 +03:00
|
|
|
ID: id.Must(),
|
2020-09-17 15:44:35 +03:00
|
|
|
Network: DefaultNetwork,
|
2021-01-29 13:17:32 +03:00
|
|
|
Register: register.DefaultRegister,
|
2020-09-17 15:44:35 +03:00
|
|
|
Logger: logger.DefaultLogger,
|
|
|
|
Context: context.Background(),
|
2019-06-07 01:29:24 +03:00
|
|
|
}
|
2020-09-17 15:44:35 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
2019-06-07 01:29:24 +03:00
|
|
|
}
|