github.com/abolfazlbeh/zhycan@v0.0.0-20230819144214-24cf38237387/internal/command/run_server.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/abolfazlbeh/zhycan/internal/http"
     6  	"github.com/spf13/cobra"
     7  	"os"
     8  	"os/signal"
     9  	"syscall"
    10  )
    11  
    12  const (
    13  	RunServerInitMsg     = `Zhycan > Running Server ...`
    14  	RunServerShutdownMsg = `Zhycan > Shutting Down Server ...`
    15  )
    16  
    17  func NewRunServerCmd() *cobra.Command {
    18  	runServerCmd := &cobra.Command{
    19  		Use:   "runserver",
    20  		Short: "Run Restfull Server And Other Engine If Existed",
    21  		Long:  ``,
    22  
    23  		Run:  runServerCmdExecute,
    24  		RunE: runServerCmdExecuteE,
    25  	}
    26  	return runServerCmd
    27  }
    28  
    29  func runServerCmdExecuteE(cmd *cobra.Command, args []string) error {
    30  	runServerCmdExecute(cmd, args)
    31  	return nil
    32  }
    33  
    34  func runServerCmdExecute(cmd *cobra.Command, args []string) {
    35  	// TODO: in future 'args' must be considered
    36  	fmt.Fprintf(cmd.OutOrStdout(), RunServerInitMsg)
    37  
    38  	http.GetManager().StartServers()
    39  
    40  	quit := make(chan os.Signal)
    41  	// kill (no param) default send syscall.SIGTERM
    42  	// kill -2 is syscall.SIGINT
    43  	// kill -9 is syscall.SIGKILL but can't be caught, so don't need to add it
    44  	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
    45  	<-quit
    46  	fmt.Fprintf(cmd.OutOrStdout(), RunServerShutdownMsg)
    47  }