cmd

package
v0.0.0-...-549c83c Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2021 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var StartCmd = &cobra.Command{
	Use:   "start",
	Short: "Start up the Pharos API service",
	Long: `Start up the Pharos API service

Usage
	pharos start

`,
	RunE: func(cmd *cobra.Command, args []string) (err error) {
		ctx, signalStop := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
		defer signalStop()

		log, err := env.MakeLogger()
		if err != nil {
			return err
		}

		fileLimit, err := setFileLimit()
		if err != nil {
			return err
		}

		log.Info("Set file limit", zap.Uint64("fileLimit", fileLimit))

		conf, err := env.LoadConfig(ctx)
		if err != nil {
			return err
		}

		router := setupRouter(conf.DebugHTTP, log)

		router.GET("/ping", func(c *gin.Context) {
			c.String(http.StatusOK, "pong")
		})

		s := &http.Server{
			Addr:    net.JoinHostPort(host, httpPort),
			Handler: router,
		}

		go func() {
			if err := s.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
				log.Error("Http server errored", zap.Error(err))
			}
		}()

		tcp := transport.NewTCP(transport.Options{
			Host:      host,
			Port:      port,
			Reuseport: true,
			Store:     storage.NewInmemoryStore(),
			Log:       log.Named("transport"),
		})

		if err := tcp.Start(ctx); err != nil {
			return err
		}

		log.Info("Listening",
			zap.Any("config", conf),
			zap.String("host", host),
			zap.Int("port", port),
			zap.String("httpPort", httpPort))

		<-ctx.Done()

		signalStop()
		log.Info("Shutting down gracefully, press Ctrl+C again to force")

		ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
		defer cancel()

		s.SetKeepAlivesEnabled(false)

		if err := s.Shutdown(ctx); err != nil {
			log.Error("Http server forced to shutdown", zap.Error(err))
		}

		if err := tcp.Close(); err != nil {
			log.Error("TCP server forced to shutdown", zap.Error(err))
		}

		log.Info("Exiting")
		return nil
	},
}

Functions

func Execute

func Execute()

Execute adds all child commands to the root command and sets flags 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