main.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. func init() {
  21. cfgFile := flag.String("config", "settings.env", "Path to config file")
  22. showHelp := flag.Bool("help", false, "Display help")
  23. showVersion := flag.Bool("version", false, "Display build information")
  24. flag.Parse()
  25. switch {
  26. case *showVersion:
  27. fmt.Printf("%-10s %s\n", "version:", version)
  28. fmt.Printf("%-10s %s\n", "commit:", commit)
  29. fmt.Printf("%-10s %s\n", "date:", date)
  30. os.Exit(0)
  31. case *showHelp:
  32. flag.PrintDefaults()
  33. os.Exit(0)
  34. }
  35. // optionally populate environment variables with config file
  36. if err := godotenv.Load(*cfgFile); err != nil {
  37. fmt.Printf("Config file (%s) not found, defaulting to env vars for app config...\n", *cfgFile)
  38. } else {
  39. fmt.Printf("Successfully loaded config file (%s)\n", *cfgFile)
  40. }
  41. }
  42. func main() {
  43. ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
  44. defer stop()
  45. deps, err := MakeCommonDeps()
  46. if err != nil {
  47. fmt.Printf("startup failed: %s\n", err)
  48. os.Exit(1)
  49. }
  50. g, ctx := errgroup.WithContext(ctx)
  51. oscar := OSCAR(deps)
  52. g.Go(oscar.ListenAndServe)
  53. kerb := KerberosAPI(deps)
  54. g.Go(kerb.ListenAndServe)
  55. api := MgmtAPI(deps)
  56. g.Go(api.ListenAndServe)
  57. toc := TOC(deps)
  58. g.Go(toc.ListenAndServe)
  59. var webAPI *webapi.Server
  60. if os.Getenv("ENABLE_WEBAPI") == "1" {
  61. webAPI = WebAPI(deps)
  62. g.Go(webAPI.ListenAndServe)
  63. }
  64. // Start ICQ Legacy server if enabled
  65. icqLegacy := ICQLegacy(deps)
  66. if deps.cfg.ICQLegacy.Enabled {
  67. g.Go(icqLegacy.ListenAndServe)
  68. }
  69. <-ctx.Done()
  70. shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  71. defer cancel()
  72. _ = oscar.Shutdown(shutdownCtx)
  73. _ = kerb.Shutdown(shutdownCtx)
  74. _ = api.Shutdown(shutdownCtx)
  75. _ = toc.Shutdown(shutdownCtx)
  76. if os.Getenv("ENABLE_WEBAPI") == "1" {
  77. _ = webAPI.Shutdown(shutdownCtx)
  78. }
  79. if deps.cfg.ICQLegacy.Enabled {
  80. _ = icqLegacy.Shutdown(shutdownCtx)
  81. }
  82. if err = g.Wait(); err != nil {
  83. deps.logger.Error("server initialization failed", "err", err.Error())
  84. os.Exit(1)
  85. }
  86. }