commands

package
v1.1.1139 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RootCmd = &cobra.Command{
	Use:   "shar",
	Short: "SHAR Server",
	Long:  ``,

	Run: func(cmd *cobra.Command, args []string) {
		cfg, err := config.GetEnvironment()
		if err != nil {
			log.Fatal(err)
		}
		var lev slog.Level
		var addSource bool
		switch cfg.LogLevel {
		case "debug":
			lev = slog.LevelDebug
			addSource = true
		case "info":
			lev = slog.LevelInfo
		case "warn":
			lev = slog.LevelWarn
		default:
			lev = slog.LevelError
		}

		conn, err := nats.Connect(cfg.NatsURL)
		if err != nil {
			slog.Error("connect to NATS", "error", err, slog.String("url", cfg.NatsURL))
			panic(err)
		}

		handlerFactoryFns := map[string]func() slog.Handler{
			"text": func() slog.Handler {
				return common.NewTextHandler(lev, addSource)
			},
			"shar-handler": func() slog.Handler {
				return common.NewSharHandler(common.HandlerOptions{Level: lev}, &common.NatsLogPublisher{Conn: conn})
			},
		}

		cfgHandlers := strings.Split(cfg.LogHandler, ",")
		handlers := make([]slog.Handler, 0, len(cfgHandlers))
		for _, h := range cfgHandlers {
			handlers = append(handlers, handlerFactoryFns[h]())
		}

		logx.SetDefault("shar", common.NewMultiHandler(handlers))

		if err != nil {
			panic(err)
		}

		var svr *server.Server
		if svr, err = server.New(option.Concurrency(cfg.Concurrency), option.NatsConn(conn), option.NatsUrl(cfg.NatsURL), option.GrpcPort(cfg.Port)); err != nil {
			panic(fmt.Errorf("creating server: %w", err))
		}

		if err = svr.Listen(); err != nil {
			panic(fmt.Errorf("starting server: %w", err))
		}
	},
	PersistentPreRun: func(cmd *cobra.Command, args []string) {

	},
}

RootCmd represents the base command when called without any subcommands

Functions

func Execute

func Execute()

Execute adds all child commands to the root command and sets flag appropriately. This is called by main.main(). It only needs to happen once to the RootCmd.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL