A FAIRE.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
3.9 KiB

  1. /*
  2. ------------------------------------------------------------------------------------------------------------------------
  3. ####### main ####### Copyright (c) 2021 mls-361 #################################################### MIT License #######
  4. ------------------------------------------------------------------------------------------------------------------------
  5. */
  6. package main
  7. import (
  8. "math/rand"
  9. "os"
  10. "os/signal"
  11. "sync"
  12. "syscall"
  13. "time"
  14. "forge.chapril.org/mls-361/application"
  15. "forge.chapril.org/mls-361/errors"
  16. "forge.chapril.org/armen/armen/internal/api"
  17. "forge.chapril.org/armen/armen/internal/cli"
  18. "forge.chapril.org/armen/armen/internal/components"
  19. "forge.chapril.org/armen/armen/internal/config"
  20. "forge.chapril.org/armen/armen/internal/crypto"
  21. "forge.chapril.org/armen/armen/internal/demo"
  22. "forge.chapril.org/armen/armen/internal/gui"
  23. "forge.chapril.org/armen/armen/internal/logger"
  24. "forge.chapril.org/armen/armen/internal/model"
  25. "forge.chapril.org/armen/armen/internal/router"
  26. "forge.chapril.org/armen/armen/internal/server"
  27. "forge.chapril.org/armen/armen/internal/storage"
  28. )
  29. var (
  30. _version string
  31. _builtAt string
  32. )
  33. func init() {
  34. rand.Seed(time.Now().UnixNano())
  35. }
  36. func buildComponents(ccs *components.Components) error {
  37. var err error
  38. ccs.Config, err = config.Load(ccs)
  39. if err != nil {
  40. return errors.WithMessage(err, "config") ///////////////////////////////////////////////////////////////////////
  41. }
  42. ccs.Logger, err = logger.Build(ccs)
  43. if err != nil {
  44. return errors.WithMessage(err, "logger") ///////////////////////////////////////////////////////////////////////
  45. }
  46. ccs.Crypto, err = crypto.Build(ccs)
  47. if err != nil {
  48. return errors.WithMessage(err, "crypto") ///////////////////////////////////////////////////////////////////////
  49. }
  50. ccs.Storage, err = storage.Build(ccs)
  51. if err != nil {
  52. return errors.WithMessage(err, "storage") //////////////////////////////////////////////////////////////////////
  53. }
  54. ccs.Model, err = model.Build(ccs)
  55. if err != nil {
  56. return errors.WithMessage(err, "model") ////////////////////////////////////////////////////////////////////////
  57. }
  58. if len(os.Args) > 1 {
  59. return nil
  60. }
  61. ccs.Router = router.Build(ccs)
  62. ccs.Server, err = server.Build(ccs)
  63. if err != nil {
  64. return errors.WithMessage(err, "server") ///////////////////////////////////////////////////////////////////////
  65. }
  66. if err = demo.Build(ccs); err != nil {
  67. return errors.WithMessage(err, "demo") /////////////////////////////////////////////////////////////////////////
  68. }
  69. api.Build(ccs)
  70. gui.Build(ccs)
  71. return nil
  72. }
  73. func waitEnd(server components.Server) (*sync.WaitGroup, chan struct{}) {
  74. endCh := make(chan struct{})
  75. group := new(sync.WaitGroup)
  76. group.Add(1)
  77. go func() { //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  78. defer group.Done()
  79. defer server.Stop()
  80. sigCh := make(chan os.Signal, 1)
  81. defer close(sigCh)
  82. signal.Notify(sigCh, os.Interrupt, syscall.SIGABRT, syscall.SIGQUIT, syscall.SIGTERM)
  83. select {
  84. case <-sigCh:
  85. case <-endCh:
  86. }
  87. }()
  88. return group, endCh
  89. }
  90. func run() error {
  91. app := application.New("armen", _version, _builtAt)
  92. if err := app.Initialize(); err != nil {
  93. return app.OnError(err)
  94. }
  95. ccs := components.New(app)
  96. defer ccs.Shutdown()
  97. if err := buildComponents(ccs); err != nil {
  98. if ccs.Logger != nil {
  99. ccs.Logger.Fatal( //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  100. "Components build error",
  101. "reason", err,
  102. )
  103. }
  104. return app.OnError(err)
  105. }
  106. if len(os.Args) > 1 {
  107. return app.OnError(cli.Run(ccs))
  108. }
  109. group, endCh := waitEnd(ccs.Server)
  110. ccs.Startup()
  111. demo.Start()
  112. err := ccs.Server.Start()
  113. close(endCh)
  114. demo.Stop()
  115. group.Wait()
  116. return app.OnError(err)
  117. }
  118. func main() {
  119. if run() != nil {
  120. os.Exit(1)
  121. }
  122. }
  123. /*
  124. ######################################################################################################## @(°_°)@ #######
  125. */