app.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package pairec
  2. import (
  3. "fmt"
  4. "net/http"
  5. "os"
  6. "sync"
  7. "time"
  8. "gitlab.alibaba-inc.com/pai_biz_arch/pairec/log"
  9. "gitlab.alibaba-inc.com/pai_biz_arch/pairec/recconf"
  10. )
  11. var (
  12. PairecApp *App
  13. )
  14. func init() {
  15. PairecApp = NewApp()
  16. }
  17. type App struct {
  18. Handlers *ControllerRegister
  19. Server *http.Server
  20. }
  21. func NewApp() *App {
  22. cr := NewControllerRegister()
  23. app := &App{Handlers: cr, Server: &http.Server{}}
  24. return app
  25. }
  26. func (app *App) Run() {
  27. mode := os.Getenv("RUN_MODE")
  28. if mode == "COMMAND" {
  29. return
  30. }
  31. addr := fmt.Sprintf("%s:%d", recconf.Config.ListenConf.HttpAddr, recconf.Config.ListenConf.HttpPort)
  32. app.Server.Handler = app.Handlers
  33. app.Server.Addr = addr
  34. app.Server.ReadTimeout = 30 * time.Second
  35. app.Server.WriteTimeout = 30 * time.Second
  36. app.Server.MaxHeaderBytes = 1 << 20
  37. wg := sync.WaitGroup{}
  38. wg.Add(1)
  39. go func() {
  40. defer wg.Done()
  41. if err := app.Server.ListenAndServe(); err != http.ErrServerClosed {
  42. log.Error(fmt.Sprintf("server stop, err=%v", err))
  43. }
  44. }()
  45. fmt.Println("server start")
  46. wg.Wait()
  47. log.Flush()
  48. }