github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/runtime/server/server.go (about) 1 package server 2 3 import ( 4 "os" 5 6 pb "github.com/tickoalcantara12/micro/v3/proto/runtime" 7 "github.com/tickoalcantara12/micro/v3/service" 8 log "github.com/tickoalcantara12/micro/v3/service/logger" 9 "github.com/tickoalcantara12/micro/v3/service/runtime" 10 "github.com/tickoalcantara12/micro/v3/service/runtime/handler" 11 "github.com/tickoalcantara12/micro/v3/service/runtime/manager" 12 "github.com/urfave/cli/v2" 13 ) 14 15 var ( 16 // name of the runtime 17 name = "runtime" 18 // address of the runtime 19 address = ":8088" 20 21 // Flags specific to the runtime service 22 Flags = []cli.Flag{ 23 &cli.StringFlag{ 24 Name: "source", 25 Usage: "Set the runtime source, e.g. micro/services", 26 EnvVars: []string{"MICRO_RUNTIME_SOURCE"}, 27 }, 28 &cli.IntFlag{ 29 Name: "retries", 30 Usage: "Set the max retries per service", 31 EnvVars: []string{"MICRO_RUNTIME_RETRIES"}, 32 }, 33 } 34 ) 35 36 // Run the runtime service 37 func Run(ctx *cli.Context) error { 38 if len(ctx.String("address")) > 0 { 39 address = ctx.String("address") 40 } 41 42 if len(ctx.String("server_name")) > 0 { 43 name = ctx.String("server_name") 44 } 45 46 var srvOpts []service.Option 47 if len(address) > 0 { 48 srvOpts = append(srvOpts, service.Address(address)) 49 } 50 51 // create runtime 52 if ctx.IsSet("source") { 53 runtime.DefaultRuntime.Init(runtime.WithSource(ctx.String("source"))) 54 } 55 56 // append name 57 srvOpts = append(srvOpts, service.Name(name)) 58 59 // new service 60 srv := service.New(srvOpts...) 61 62 // create a new runtime manager 63 manager := manager.New() 64 65 // start the manager 66 if err := manager.Start(); err != nil { 67 log.Errorf("failed to start: %s", err) 68 os.Exit(1) 69 } 70 71 // register the handlers 72 pb.RegisterRuntimeHandler(srv.Server(), &handler.Runtime{Runtime: manager}) 73 pb.RegisterBuildHandler(srv.Server(), new(handler.Build)) 74 pb.RegisterSourceHandler(srv.Server(), new(handler.Source)) 75 76 // start runtime service 77 if err := srv.Run(); err != nil { 78 log.Errorf("error running service: %v", err) 79 } 80 81 // stop the manager 82 if err := manager.Stop(); err != nil { 83 log.Errorf("failed to stop: %s", err) 84 os.Exit(1) 85 } 86 87 return nil 88 }