main.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //TODO: Harden. Add failover for every method and agent calls
  2. //TODO: Figure out why mongodb keeps failing (log rotation?)
  3. package main
  4. import (
  5. "log"
  6. "github.com/gravitl/netmaker/controllers"
  7. "github.com/gravitl/netmaker/mongoconn"
  8. "github.com/gravitl/netmaker/config"
  9. "fmt"
  10. "os"
  11. "net"
  12. "context"
  13. "sync"
  14. "os/signal"
  15. service "github.com/gravitl/netmaker/controllers"
  16. nodepb "github.com/gravitl/netmaker/grpc"
  17. "google.golang.org/grpc"
  18. )
  19. //Start MongoDB Connection and start API Request Handler
  20. func main() {
  21. log.Println("Server starting...")
  22. mongoconn.ConnectDatabase()
  23. var waitgroup sync.WaitGroup
  24. if config.Config.Server.AgentBackend {
  25. waitgroup.Add(1)
  26. go runGRPC(&waitgroup)
  27. }
  28. if config.Config.Server.RestBackend {
  29. waitgroup.Add(1)
  30. controller.HandleRESTRequests(&waitgroup)
  31. }
  32. if !config.Config.Server.RestBackend && !config.Config.Server.AgentBackend {
  33. fmt.Println("Oops! No Server Mode selected. Nothing being served.")
  34. }
  35. waitgroup.Wait()
  36. fmt.Println("Exiting now.")
  37. }
  38. func runGRPC(wg *sync.WaitGroup) {
  39. defer wg.Done()
  40. // Configure 'log' package to give file name and line number on eg. log.Fatal
  41. // Pipe flags to one another (log.LstdFLags = log.Ldate | log.Ltime)
  42. log.SetFlags(log.LstdFlags | log.Lshortfile)
  43. // Start our listener, 50051 is the default gRPC port
  44. grpcport := ":50051"
  45. if config.Config.Server.GrpcPort != "" {
  46. grpcport = ":" + config.Config.Server.GrpcPort
  47. }
  48. if os.Getenv("GRPC_PORT") != "" {
  49. grpcport = ":" + os.Getenv("GRPC_PORT")
  50. }
  51. listener, err := net.Listen("tcp", grpcport)
  52. // Handle errors if any
  53. if err != nil {
  54. log.Fatalf("Unable to listen on port" + grpcport + ": %v", err)
  55. }
  56. s := grpc.NewServer(
  57. authServerUnaryInterceptor(),
  58. authServerStreamInterceptor(),
  59. )
  60. // Create NodeService type
  61. srv := &service.NodeServiceServer{}
  62. // Register the service with the server
  63. nodepb.RegisterNodeServiceServer(s, srv)
  64. srv.NodeDB = mongoconn.NodeDB
  65. // Start the server in a child routine
  66. go func() {
  67. if err := s.Serve(listener); err != nil {
  68. log.Fatalf("Failed to serve: %v", err)
  69. }
  70. }()
  71. fmt.Println("Agent Server succesfully started on port " + grpcport + " (gRPC)")
  72. // Right way to stop the server using a SHUTDOWN HOOK
  73. // Create a channel to receive OS signals
  74. c := make(chan os.Signal)
  75. // Relay os.Interrupt to our channel (os.Interrupt = CTRL+C)
  76. // Ignore other incoming signals
  77. signal.Notify(c, os.Interrupt)
  78. // Block main routine until a signal is received
  79. // As long as user doesn't press CTRL+C a message is not passed and our main routine keeps running
  80. <-c
  81. // After receiving CTRL+C Properly stop the server
  82. fmt.Println("Stopping the Agent server...")
  83. s.Stop()
  84. listener.Close()
  85. fmt.Println("Agent server closed..")
  86. fmt.Println("Closing MongoDB connection")
  87. mongoconn.Client.Disconnect(context.TODO())
  88. fmt.Println("MongoDB connection closed.")
  89. }
  90. func authServerUnaryInterceptor() grpc.ServerOption {
  91. return grpc.UnaryInterceptor(controller.AuthServerUnaryInterceptor)
  92. }
  93. func authServerStreamInterceptor() grpc.ServerOption {
  94. return grpc.StreamInterceptor(controller.AuthServerStreamInterceptor)
  95. }