Server is the entrypoint for the server command.
(cfg *config.Config)
| 24 | |
| 25 | // Server is the entrypoint for the server command. |
| 26 | func Server(cfg *config.Config) *cobra.Command { |
| 27 | return &cobra.Command{ |
| 28 | Use: "server", |
| 29 | Short: fmt.Sprintf("start the %s service without runtime (unsupervised mode)", cfg.Service.Name), |
| 30 | PreRunE: func(cmd *cobra.Command, args []string) error { |
| 31 | return configlog.ReturnFatal(parser.ParseConfig(cfg)) |
| 32 | }, |
| 33 | RunE: func(cmd *cobra.Command, args []string) error { |
| 34 | logger := log.Configure(cfg.Service.Name, cfg.Commons, cfg.LogLevel) |
| 35 | traceProvider, err := tracing.GetTraceProvider(cmd.Context(), cfg.Commons.TracesExporter, cfg.Service.Name) |
| 36 | if err != nil { |
| 37 | return err |
| 38 | } |
| 39 | cfg.GrpcClient, err = ogrpc.NewClient( |
| 40 | append(ogrpc.GetClientOptions(cfg.GRPCClientTLS), ogrpc.WithTraceProvider(traceProvider))..., |
| 41 | ) |
| 42 | if err != nil { |
| 43 | return err |
| 44 | } |
| 45 | |
| 46 | var cancel context.CancelFunc |
| 47 | if cfg.Context == nil { |
| 48 | cfg.Context, cancel = signal.NotifyContext(context.Background(), runner.StopSignals...) |
| 49 | defer cancel() |
| 50 | } |
| 51 | ctx := cfg.Context |
| 52 | |
| 53 | mtrcs := metrics.New() |
| 54 | mtrcs.BuildInfo.WithLabelValues(version.GetString()).Set(1) |
| 55 | |
| 56 | handle := svc.NewDefaultLanguageService(cfg, svc.NewService(cfg, logger)) |
| 57 | |
| 58 | gr := runner.NewGroup() |
| 59 | |
| 60 | // prepare an HTTP server and add it to the group run. |
| 61 | httpServer, err := http.Server( |
| 62 | http.Name(cfg.Service.Name), |
| 63 | http.Logger(logger), |
| 64 | http.Context(ctx), |
| 65 | http.Config(cfg), |
| 66 | http.Metrics(mtrcs), |
| 67 | http.ServiceHandler(handle), |
| 68 | http.TraceProvider(traceProvider), |
| 69 | ) |
| 70 | if err != nil { |
| 71 | logger.Error(). |
| 72 | Err(err). |
| 73 | Msg("Error initializing http service") |
| 74 | return fmt.Errorf("could not initialize http service: %w", err) |
| 75 | } |
| 76 | gr.Add(runner.NewGoMicroHttpServerRunner(cfg.Service.Name+".http", httpServer)) |
| 77 | |
| 78 | // prepare a gRPC server and add it to the group run. |
| 79 | grpcServer := grpc.Server( |
| 80 | grpc.Name(cfg.Service.Name), |
| 81 | grpc.Logger(logger), |
| 82 | grpc.Context(ctx), |
| 83 | grpc.Config(cfg), |
no test coverage detected