server

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2024 License: MIT Imports: 30 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Server = &cobra.Command{
	Use:   "server",
	Short: "start go-admin-server server.",
	Long:  "This command is the bootstrap of go-admin-server.",
	PreRun: func(cmd *cobra.Command, args []string) {
		if configFile != "" && nacosConfigFile != "" {
			log.Fatal("command options --config(or -c)) and --nacosConfig(or -C) cannot be specified together.")
		}
	},
	Run: func(cmd *cobra.Command, args []string) {
		loadConfig()

		if cfg.Log.Level == "" {
			cfg.Log.Level = "info"
		}
		logHandler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
			Level: parseSlogLevel(cfg.Log.Level),
		})
		slog.SetDefault(slog.New(logHandler))

		closeCache := utils.InitializeCache()
		defer closeCache()
		closeMysql := database.InitializeMysql(&cfg.Mysql)
		defer closeMysql()
		closeRedis := database.InitializeRedis(&cfg.Redis)
		defer closeRedis()
		closeMongo := database.InitializeMongo(&cfg.Mongo)
		defer closeMongo()

		if cfg.RabbitMQ.Enable {
			producer, err := createRabbit(cfg.RabbitMQ)
			if err != nil {
				slog.Error("-", err)
				os.Exit(1)
			}
			defer producer.Close()
			if err = mq.InitExchangeAndQueues(producer); err != nil {
				slog.Error("-", slog.Any("err", err))
				os.Exit(1)
			}

			consumer, err := createRabbit(cfg.RabbitMQ)
			if err != nil {
				slog.Error("-", err)
				os.Exit(1)
			}
			defer consumer.Close()
			go mq.RunConsumer(consumer)
		}

		ctx, cancel := context.WithCancel(context.Background())
		defer cancel()

		if cfg.Grpc.Enable {
			go func() {
				defer func() {
					if r := recover(); r != nil {
						slog.Error("grpc error", slog.Any("err", r))
					}
				}()
				grpc.Run(ctx, cfg.Grpc.Addr)
			}()
		}

		middleware.Initialize(cfg.ContextPath)
		go runWebServer(ctx)

		if cfg.Pprof.Enable {
			go runPprofAnalysis()
		}

		var shutdown = make(chan os.Signal, 1)
		defer close(shutdown)
		signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)
		<-shutdown
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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