github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/broker/server/server.go (about) 1 package server 2 3 import ( 4 "time" 5 6 pb "github.com/tickoalcantara12/micro/v3/proto/broker" 7 "github.com/tickoalcantara12/micro/v3/service" 8 "github.com/tickoalcantara12/micro/v3/service/broker" 9 "github.com/tickoalcantara12/micro/v3/service/broker/handler" 10 "github.com/tickoalcantara12/micro/v3/service/logger" 11 "github.com/urfave/cli/v2" 12 ) 13 14 var ( 15 name = "broker" 16 address = ":8003" 17 ) 18 19 // Run the micro broker 20 func Run(ctx *cli.Context) error { 21 srvOpts := []service.Option{ 22 service.Name(name), 23 service.Address(address), 24 } 25 26 if i := time.Duration(ctx.Int("register_ttl")); i > 0 { 27 srvOpts = append(srvOpts, service.RegisterTTL(i*time.Second)) 28 } 29 if i := time.Duration(ctx.Int("register_interval")); i > 0 { 30 srvOpts = append(srvOpts, service.RegisterInterval(i*time.Second)) 31 } 32 33 // new service 34 srv := service.New(srvOpts...) 35 36 // connect to the broker 37 broker.DefaultBroker.Connect() 38 39 // register the broker Broker 40 pb.RegisterBrokerHandler(srv.Server(), new(handler.Broker)) 41 42 // run the service 43 if err := srv.Run(); err != nil { 44 logger.Fatal(err) 45 } 46 return nil 47 }