1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- package cmd
- import (
- "context"
- "fmt"
- "github.com/gin-gonic/gin"
- "github.com/spf13/cobra"
- "log"
- "net/http"
- "os"
- "os/signal"
- "ppgo/common"
- "syscall"
- "time"
- )
- // RootCmd represents the base command when called without any subcommands
- var RootCmd = &cobra.Command{
- Use: "web",
- Short: "welcome use ppgo",
- // Uncomment the following line if your bare application
- // has an action associated with it:
- // Run: func(cmd *cobra.Command, args []string) { },
- }
- // 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.
- func Execute() {
- cobra.CheckErr(RootCmd.Execute())
- }
- func init() {
- RootCmd.PersistentFlags().StringVarP(&common.Env, "env", "e", "local", "系统环境,支持local dev")
- }
- func startHttp(r *gin.Engine, port string) {
- server := http.Server{
- Addr: fmt.Sprintf(":%s", port),
- Handler: r,
- }
- go func() {
- if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
- common.GetLog("common").Info("server listen err:%s", err)
- }
- }()
- quit := make(chan os.Signal, 1)
- signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
- <-quit
- ctx, channel := context.WithTimeout(context.Background(), 5*time.Second)
- defer channel()
- if err := server.Shutdown(ctx); err != nil {
- log.Fatal("server shutdown error")
- }
- common.GetLog("common").Info("server exiting...")
- }
|