v3 refactor (#1868)
* Move to v3 Co-authored-by: Ben Toogood <bentoogood@gmail.com>
This commit is contained in:
@@ -6,9 +6,8 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/micro/cli/v2"
|
||||
"github.com/micro/go-micro/v2"
|
||||
"github.com/micro/go-micro/v2/registry"
|
||||
"github.com/micro/go-micro/v3/registry"
|
||||
"github.com/micro/go-micro/v3/registry/memory"
|
||||
)
|
||||
|
||||
//Options for web
|
||||
@@ -20,9 +19,6 @@ type Options struct {
|
||||
Address string
|
||||
Advertise string
|
||||
|
||||
Action func(*cli.Context)
|
||||
Flags []cli.Flag
|
||||
|
||||
RegisterTTL time.Duration
|
||||
RegisterInterval time.Duration
|
||||
|
||||
@@ -36,7 +32,6 @@ type Options struct {
|
||||
Context context.Context
|
||||
|
||||
Registry registry.Registry
|
||||
Service micro.Service
|
||||
|
||||
Secure bool
|
||||
TLSConfig *tls.Config
|
||||
@@ -60,7 +55,6 @@ func newOptions(opts ...Option) Options {
|
||||
RegisterTTL: DefaultRegisterTTL,
|
||||
RegisterInterval: DefaultRegisterInterval,
|
||||
StaticDir: DefaultStaticDir,
|
||||
Service: micro.NewService(),
|
||||
Context: context.TODO(),
|
||||
Signal: true,
|
||||
}
|
||||
@@ -69,6 +63,10 @@ func newOptions(opts ...Option) Options {
|
||||
o(&opt)
|
||||
}
|
||||
|
||||
if opt.Registry == nil {
|
||||
opt.Registry = memory.NewRegistry()
|
||||
}
|
||||
|
||||
if opt.RegisterCheck == nil {
|
||||
opt.RegisterCheck = DefaultRegisterCheck
|
||||
}
|
||||
@@ -172,27 +170,6 @@ func Server(srv *http.Server) Option {
|
||||
}
|
||||
}
|
||||
|
||||
// MicroService sets the micro.Service used internally
|
||||
func MicroService(s micro.Service) Option {
|
||||
return func(o *Options) {
|
||||
o.Service = s
|
||||
}
|
||||
}
|
||||
|
||||
// Flags sets the command flags.
|
||||
func Flags(flags ...cli.Flag) Option {
|
||||
return func(o *Options) {
|
||||
o.Flags = append(o.Flags, flags...)
|
||||
}
|
||||
}
|
||||
|
||||
// Action sets the command action.
|
||||
func Action(a func(*cli.Context)) Option {
|
||||
return func(o *Options) {
|
||||
o.Action = a
|
||||
}
|
||||
}
|
||||
|
||||
// BeforeStart is executed before the server starts.
|
||||
func BeforeStart(fn func() error) Option {
|
||||
return func(o *Options) {
|
||||
|
||||
108
web/service.go
108
web/service.go
@@ -12,17 +12,13 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/micro/cli/v2"
|
||||
"github.com/micro/go-micro/v2"
|
||||
"github.com/micro/go-micro/v2/logger"
|
||||
"github.com/micro/go-micro/v2/registry"
|
||||
maddr "github.com/micro/go-micro/v2/util/addr"
|
||||
authutil "github.com/micro/go-micro/v2/util/auth"
|
||||
"github.com/micro/go-micro/v2/util/backoff"
|
||||
mhttp "github.com/micro/go-micro/v2/util/http"
|
||||
mnet "github.com/micro/go-micro/v2/util/net"
|
||||
signalutil "github.com/micro/go-micro/v2/util/signal"
|
||||
mls "github.com/micro/go-micro/v2/util/tls"
|
||||
"github.com/micro/go-micro/v3/logger"
|
||||
"github.com/micro/go-micro/v3/registry"
|
||||
maddr "github.com/micro/go-micro/v3/util/addr"
|
||||
"github.com/micro/go-micro/v3/util/backoff"
|
||||
mnet "github.com/micro/go-micro/v3/util/net"
|
||||
signalutil "github.com/micro/go-micro/v3/util/signal"
|
||||
mls "github.com/micro/go-micro/v3/util/tls"
|
||||
)
|
||||
|
||||
type service struct {
|
||||
@@ -120,11 +116,7 @@ func (s *service) register() error {
|
||||
return nil
|
||||
}
|
||||
// default to service registry
|
||||
r := s.opts.Service.Options().Registry
|
||||
// switch to option if specified
|
||||
if s.opts.Registry != nil {
|
||||
r = s.opts.Registry
|
||||
}
|
||||
r := s.opts.Registry
|
||||
|
||||
// service node need modify, node address maybe changed
|
||||
srv := s.genSrv()
|
||||
@@ -144,7 +136,6 @@ func (s *service) register() error {
|
||||
// register options
|
||||
rOpts := []registry.RegisterOption{
|
||||
registry.RegisterTTL(s.opts.RegisterTTL),
|
||||
registry.RegisterDomain(s.opts.Service.Server().Options().Namespace),
|
||||
}
|
||||
|
||||
// try three times if necessary
|
||||
@@ -173,11 +164,8 @@ func (s *service) deregister() error {
|
||||
return nil
|
||||
}
|
||||
// default to service registry
|
||||
r := s.opts.Service.Options().Registry
|
||||
// switch to option if specified
|
||||
if s.opts.Registry != nil {
|
||||
r = s.opts.Registry
|
||||
}
|
||||
r := s.opts.Registry
|
||||
|
||||
return r.Deregister(s.srv)
|
||||
}
|
||||
|
||||
@@ -300,15 +288,6 @@ func (s *service) stop() error {
|
||||
return <-ch
|
||||
}
|
||||
|
||||
func (s *service) Client() *http.Client {
|
||||
rt := mhttp.NewRoundTripper(
|
||||
mhttp.WithRouter(s.opts.Service.Options().Router),
|
||||
)
|
||||
return &http.Client{
|
||||
Transport: rt,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *service) Handle(pattern string, handler http.Handler) {
|
||||
var seen bool
|
||||
s.RLock()
|
||||
@@ -377,68 +356,6 @@ func (s *service) Init(opts ...Option) error {
|
||||
o(&s.opts)
|
||||
}
|
||||
|
||||
serviceOpts := []micro.Option{}
|
||||
|
||||
if len(s.opts.Flags) > 0 {
|
||||
serviceOpts = append(serviceOpts, micro.Flags(s.opts.Flags...))
|
||||
}
|
||||
|
||||
if s.opts.Registry != nil {
|
||||
serviceOpts = append(serviceOpts, micro.Registry(s.opts.Registry))
|
||||
}
|
||||
|
||||
s.Unlock()
|
||||
|
||||
serviceOpts = append(serviceOpts, micro.Action(func(ctx *cli.Context) error {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
|
||||
if ttl := ctx.Int("register_ttl"); ttl > 0 {
|
||||
s.opts.RegisterTTL = time.Duration(ttl) * time.Second
|
||||
}
|
||||
|
||||
if interval := ctx.Int("register_interval"); interval > 0 {
|
||||
s.opts.RegisterInterval = time.Duration(interval) * time.Second
|
||||
}
|
||||
|
||||
if name := ctx.String("server_name"); len(name) > 0 {
|
||||
s.opts.Name = name
|
||||
}
|
||||
|
||||
if ver := ctx.String("server_version"); len(ver) > 0 {
|
||||
s.opts.Version = ver
|
||||
}
|
||||
|
||||
if id := ctx.String("server_id"); len(id) > 0 {
|
||||
s.opts.Id = id
|
||||
}
|
||||
|
||||
if addr := ctx.String("server_address"); len(addr) > 0 {
|
||||
s.opts.Address = addr
|
||||
}
|
||||
|
||||
if adv := ctx.String("server_advertise"); len(adv) > 0 {
|
||||
s.opts.Advertise = adv
|
||||
}
|
||||
|
||||
if s.opts.Action != nil {
|
||||
s.opts.Action(ctx)
|
||||
}
|
||||
|
||||
return nil
|
||||
}))
|
||||
|
||||
s.RLock()
|
||||
// pass in own name and version
|
||||
if s.opts.Service.Name() == "" {
|
||||
serviceOpts = append(serviceOpts, micro.Name(s.opts.Name))
|
||||
}
|
||||
serviceOpts = append(serviceOpts, micro.Version(s.opts.Version))
|
||||
s.RUnlock()
|
||||
|
||||
s.opts.Service.Init(serviceOpts...)
|
||||
|
||||
s.Lock()
|
||||
srv := s.genSrv()
|
||||
srv.Endpoints = s.srv.Endpoints
|
||||
s.srv = srv
|
||||
@@ -448,11 +365,6 @@ func (s *service) Init(opts ...Option) error {
|
||||
}
|
||||
|
||||
func (s *service) Run() error {
|
||||
// generate an auth account
|
||||
if err := authutil.Verify(s.opts.Service.Options().Auth); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.start(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -11,8 +11,8 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/micro/go-micro/v2/registry"
|
||||
"github.com/micro/go-micro/v2/registry/memory"
|
||||
"github.com/micro/go-micro/v3/registry"
|
||||
"github.com/micro/go-micro/v3/registry/memory"
|
||||
)
|
||||
|
||||
func TestService(t *testing.T) {
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
|
||||
// Service is a web service with service discovery built in
|
||||
type Service interface {
|
||||
Client() *http.Client
|
||||
Init(opts ...Option) error
|
||||
Options() Options
|
||||
Handle(pattern string, handler http.Handler)
|
||||
|
||||
@@ -7,14 +7,12 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/micro/cli/v2"
|
||||
"github.com/micro/go-micro/v2"
|
||||
"github.com/micro/go-micro/v2/logger"
|
||||
"github.com/micro/go-micro/v2/web"
|
||||
"github.com/micro/go-micro/v3/logger"
|
||||
"github.com/micro/go-micro/v3/web"
|
||||
)
|
||||
|
||||
func TestWeb(t *testing.T) {
|
||||
for i := 0; i < 10; i++ {
|
||||
for i := 0; i < 3; i++ {
|
||||
fmt.Println("Test nr", i)
|
||||
testFunc()
|
||||
}
|
||||
@@ -24,27 +22,7 @@ func testFunc() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*250)
|
||||
defer cancel()
|
||||
|
||||
s := micro.NewService(
|
||||
micro.Name("test"),
|
||||
micro.Context(ctx),
|
||||
micro.HandleSignal(false),
|
||||
micro.Flags(
|
||||
&cli.StringFlag{
|
||||
Name: "test.timeout",
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "test.v",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "test.run",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "test.testlogfile",
|
||||
},
|
||||
),
|
||||
)
|
||||
w := web.NewService(
|
||||
web.MicroService(s),
|
||||
web.Context(ctx),
|
||||
web.HandleSignal(false),
|
||||
)
|
||||
@@ -52,14 +30,7 @@ func testFunc() {
|
||||
//w.Init()
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(2)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
err := s.Run()
|
||||
if err != nil {
|
||||
logger.Errorf("micro run error: %v", err)
|
||||
}
|
||||
}()
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
err := w.Run()
|
||||
|
||||
Reference in New Issue
Block a user