main.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package main
  2. import (
  3. "context" // Added for graceful shutdown
  4. "flag"
  5. "fmt"
  6. "log"
  7. "os"
  8. "os/signal" // Added for graceful shutdown
  9. "syscall" // Added for graceful shutdown
  10. "time" // Added for graceful shutdown
  11. "git.linuxforward.com/byop/byop-engine/analyzer"
  12. "git.linuxforward.com/byop/byop-engine/analyzer/stacks/golang"
  13. "git.linuxforward.com/byop/byop-engine/analyzer/stacks/nodejs"
  14. "git.linuxforward.com/byop/byop-engine/analyzer/stacks/python"
  15. "git.linuxforward.com/byop/byop-engine/app"
  16. "git.linuxforward.com/byop/byop-engine/config"
  17. )
  18. // Version information
  19. var (
  20. Version = "0.1.0"
  21. BuildTime = "unknown"
  22. GitCommit = "unknown"
  23. )
  24. func printVersion() {
  25. fmt.Printf("BYOP Engine v%s\n", Version)
  26. fmt.Printf("Build time: %s\n", BuildTime)
  27. fmt.Printf("Git commit: %s\n", GitCommit)
  28. }
  29. func printHelp() {
  30. fmt.Printf("BYOP Engine - Bring Your Own Platform engine\n\n")
  31. fmt.Printf("Usage: %s [options]\n\n", os.Args[0])
  32. fmt.Println("Options:")
  33. flag.PrintDefaults()
  34. }
  35. func main() {
  36. // 1. Parse command line flags
  37. configPath := flag.String("config", "./config.yaml", "Path to configuration file")
  38. showVersion := flag.Bool("version", false, "Display version information and exit")
  39. showHelp := flag.Bool("help", false, "Display help information and exit")
  40. flag.Parse()
  41. // Handle version and help flags
  42. if *showVersion {
  43. printVersion()
  44. os.Exit(0)
  45. }
  46. if *showHelp {
  47. printHelp()
  48. os.Exit(0)
  49. }
  50. // 2. Parse and validate configuration
  51. cfg, err := config.Load(*configPath)
  52. if err != nil {
  53. log.Fatalf("Failed to load configuration: %v", err)
  54. }
  55. if err := cfg.Validate(); err != nil {
  56. log.Fatalf("Configuration validation failed: %v", err)
  57. }
  58. // Register analyzer stacks
  59. analyzer.RegisterStack(&golang.Golang{})
  60. analyzer.RegisterStack(&nodejs.NodeJS{})
  61. analyzer.RegisterStack(&python.Python{})
  62. log.Println("Registered analyzer stacks: Go, NodeJS, Python")
  63. // 3. Initialize application
  64. appInstance, err := app.NewApp(cfg) // Renamed to avoid conflict with package name
  65. if err != nil {
  66. log.Fatalf("Failed to initialize application: %v", err)
  67. }
  68. // 4. Start application in a goroutine
  69. go func() {
  70. if err := appInstance.Run(); err != nil {
  71. log.Printf("Error starting application: %v", err) // Use Printf for non-fatal errors in goroutines
  72. }
  73. }()
  74. // 5. Implement graceful shutdown
  75. quit := make(chan os.Signal, 1)
  76. signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
  77. <-quit // Block until a signal is received
  78. log.Println("Shutting down server...")
  79. // Create a context with a timeout for the shutdown
  80. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) // 30-second timeout for shutdown
  81. defer cancel()
  82. if err := appInstance.Shutdown(ctx); err != nil {
  83. log.Fatalf("Server shutdown failed: %v", err)
  84. }
  85. log.Println("Server gracefully stopped")
  86. }