api server,Also, you can use gin,echo ... framework wrap
()
| 30 | |
| 31 | //api server,Also, you can use gin,echo ... framework wrap |
| 32 | func (c *Chat) Run() { |
| 33 | //init rpc client |
| 34 | rpc.InitLogicRpcClient() |
| 35 | |
| 36 | r := router.Register() |
| 37 | runMode := config.GetGinRunMode() |
| 38 | logrus.Info("server start , now run mode is ", runMode) |
| 39 | gin.SetMode(runMode) |
| 40 | apiConfig := config.Conf.Api |
| 41 | port := apiConfig.ApiBase.ListenPort |
| 42 | flag.Parse() |
| 43 | |
| 44 | srv := &http.Server{ |
| 45 | Addr: fmt.Sprintf(":%d", port), |
| 46 | Handler: r, |
| 47 | } |
| 48 | |
| 49 | go func() { |
| 50 | if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { |
| 51 | logrus.Errorf("start listen : %s\n", err) |
| 52 | } |
| 53 | }() |
| 54 | // if have two quit signal , this signal will priority capture ,also can graceful shutdown |
| 55 | quit := make(chan os.Signal) |
| 56 | signal.Notify(quit, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) |
| 57 | <-quit |
| 58 | logrus.Infof("Shutdown Server ...") |
| 59 | |
| 60 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 61 | defer cancel() |
| 62 | if err := srv.Shutdown(ctx); err != nil { |
| 63 | logrus.Errorf("Server Shutdown:", err) |
| 64 | } |
| 65 | logrus.Infof("Server exiting") |
| 66 | os.Exit(0) |
| 67 | } |
no test coverage detected