func main() { service := micro.NewService( micro.Name("go.micro.srv.router"), micro.Version("latest"), micro.Flags( cli.StringFlag{ Name: "database_url", EnvVar: "DATABASE_URL", Usage: "The database URL e.g root@tcp(127.0.0.1:3306)/router", }, ), micro.Action(func(c *cli.Context) { if len(c.String("database_url")) > 0 { mysql.Url = c.String("database_url") } }), ) service.Init( micro.BeforeStart(func() error { router.Init(service) return nil }), ) proto.RegisterRouterHandler(service.Server(), new(handler.Router)) label.RegisterLabelHandler(service.Server(), new(handler.Label)) rule.RegisterRuleHandler(service.Server(), new(handler.Rule)) // subcriber to stats service.Server().Subscribe(service.Server().NewSubscriber(router.StatsTopic, router.ProcessStats)) // initialise database if err := db.Init(); err != nil { log.Fatal(err) } if err := service.Run(); err != nil { log.Fatal(err) } }
func srv(ctx *cli.Context) { service := micro.NewService( micro.Name("go.micro.srv.router"), micro.RegisterTTL( time.Duration(ctx.GlobalInt("register_ttl"))*time.Second, ), micro.RegisterInterval( time.Duration(ctx.GlobalInt("register_interval"))*time.Second, ), ) if len(ctx.GlobalString("database_url")) > 0 { mysql.Url = ctx.GlobalString("database_url") } router.Init(service) proto.RegisterRouterHandler(service.Server(), new(handler.Router)) label.RegisterLabelHandler(service.Server(), new(handler.Label)) rule.RegisterRuleHandler(service.Server(), new(handler.Rule)) // subcriber to stats service.Server().Subscribe( service.Server().NewSubscriber( router.StatsTopic, router.ProcessStats, ), ) // initialise database if err := db.Init(); err != nil { log.Fatal(err) } if err := service.Run(); err != nil { log.Fatal(err) } }