main.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "os"
  7. "os/signal"
  8. "syscall"
  9. "time"
  10. "github.com/joho/godotenv"
  11. "golang.org/x/sync/errgroup"
  12. "github.com/mk6i/open-oscar-server/server/webapi"
  13. )
  14. var (
  15. // default build fields populated by GoReleaser
  16. version = "dev"
  17. commit = "none"
  18. date = "unknown"
  19. )
  20. // parseFlags handles the command line flags and loads the config file into the
  21. // environment.
  22. func parseFlags() {
  23. cfgFile := flag.String("config", "settings.env", "Path to config file")
  24. showHelp := flag.Bool("help", false, "Display help")
  25. showVersion := flag.Bool("version", false, "Display build information")
  26. flag.Parse()
  27. switch {
  28. case *showVersion:
  29. fmt.Printf("%-10s %s\n", "version:", version)
  30. fmt.Printf("%-10s %s\n", "commit:", commit)
  31. fmt.Printf("%-10s %s\n", "date:", date)
  32. os.Exit(0)
  33. case *showHelp:
  34. flag.PrintDefaults()
  35. os.Exit(0)
  36. }
  37. // optionally populate environment variables with config file
  38. if err := godotenv.Load(*cfgFile); err != nil {
  39. fmt.Printf("Config file (%s) not found, defaulting to env vars for app config...\n", *cfgFile)
  40. } else {
  41. fmt.Printf("Successfully loaded config file (%s)\n", *cfgFile)
  42. }
  43. }
  44. func main() {
  45. parseFlags()
  46. ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
  47. defer stop()
  48. deps, err := MakeCommonDeps()
  49. if err != nil {
  50. fmt.Printf("startup failed: %s\n", err)
  51. os.Exit(1)
  52. }
  53. g, ctx := errgroup.WithContext(ctx)
  54. oscar := OSCAR(deps)
  55. g.Go(oscar.ListenAndServe)
  56. kerb := KerberosAPI(deps)
  57. g.Go(kerb.ListenAndServe)
  58. api := MgmtAPI(deps)
  59. g.Go(api.ListenAndServe)
  60. toc := TOC(deps, oscar)
  61. g.Go(toc.ListenAndServe)
  62. var webAPI *webapi.Server
  63. if deps.cfg.WebAPIEnabled {
  64. webAPI = WebAPI(deps)
  65. g.Go(webAPI.ListenAndServe)
  66. }
  67. // Start ICQ Legacy server if enabled
  68. icqLegacy := ICQLegacy(deps)
  69. if deps.cfg.ICQLegacy.Enabled {
  70. g.Go(icqLegacy.ListenAndServe)
  71. }
  72. <-ctx.Done()
  73. shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  74. defer cancel()
  75. _ = oscar.Shutdown(shutdownCtx)
  76. _ = kerb.Shutdown(shutdownCtx)
  77. _ = api.Shutdown(shutdownCtx)
  78. _ = toc.Shutdown(shutdownCtx)
  79. if deps.cfg.WebAPIEnabled {
  80. _ = webAPI.Shutdown(shutdownCtx)
  81. }
  82. if deps.cfg.ICQLegacy.Enabled {
  83. _ = icqLegacy.Shutdown(shutdownCtx)
  84. }
  85. if err = g.Wait(); err != nil {
  86. deps.logger.Error("server initialization failed", "err", err.Error())
  87. os.Exit(1)
  88. }
  89. }