mongoconn.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package mongoconn
  2. import (
  3. "context"
  4. "encoding/json"
  5. "log"
  6. "os"
  7. "net/http"
  8. "go.mongodb.org/mongo-driver/mongo"
  9. "go.mongodb.org/mongo-driver/mongo/options"
  10. "github.com/gravitl/netmaker/config"
  11. )
  12. var Client *mongo.Client
  13. var NodeDB *mongo.Collection
  14. var GroupDB *mongo.Collection
  15. var user string
  16. var pass string
  17. var host string
  18. var port string
  19. var opts string
  20. func setVars() {
  21. //defaults
  22. user = "admin"
  23. pass = "password"
  24. host = "localhost"
  25. port = "27017"
  26. opts = "/?authSource=admin"
  27. //override with settings from config file
  28. if config.Config.MongoConn.User != "" {
  29. user = config.Config.MongoConn.User
  30. }
  31. if config.Config.MongoConn.Pass != "" {
  32. pass = config.Config.MongoConn.Pass
  33. }
  34. if config.Config.MongoConn.Host != "" {
  35. host = config.Config.MongoConn.Host
  36. }
  37. if config.Config.MongoConn.Port != "" {
  38. port = config.Config.MongoConn.Port
  39. }
  40. if config.Config.MongoConn.Opts != "" {
  41. opts = config.Config.MongoConn.Opts
  42. }
  43. //override with settings from env
  44. if os.Getenv("MONGO_USER") != "" {
  45. user = os.Getenv("MONGO_USER")
  46. }
  47. if os.Getenv("MONGO_PASS") != "" {
  48. pass = os.Getenv("MONGO_PASS")
  49. }
  50. if os.Getenv("MONGO_HOST") != "" {
  51. host = os.Getenv("MONGO_HOST")
  52. }
  53. if os.Getenv("MONGO_PORT") != "" {
  54. port = os.Getenv("MONGO_PORT")
  55. }
  56. if os.Getenv("MONGO_OPTS") != "" {
  57. opts = os.Getenv("MONGO_OPTS")
  58. }
  59. }
  60. //TODO: are we even using this besides at startup? Is it truely necessary?
  61. //TODO: Use config file instead of os.Getenv
  62. func ConnectDatabase() {
  63. log.Println("Database connecting...")
  64. // Set client options
  65. setVars()
  66. clientOptions := options.Client().ApplyURI( "mongodb://" +
  67. user + ":" +
  68. pass + "@" +
  69. host + ":" +
  70. port +
  71. opts )
  72. // Connect to MongoDB
  73. client, err := mongo.Connect(context.TODO(), clientOptions)
  74. Client = client
  75. if err != nil {
  76. log.Fatal(err)
  77. }
  78. // Check the connection
  79. err = Client.Ping(context.TODO(), nil)
  80. if err != nil {
  81. log.Fatal(err)
  82. }
  83. NodeDB = Client.Database("netmaker").Collection("nodes")
  84. GroupDB = Client.Database("netmaker").Collection("groups")
  85. log.Println("Database Connected.")
  86. }
  87. //TODO: IDK if we're using ConnectDB any more.... I think we're just using Client.Database
  88. //Review and see if this is necessary
  89. // ConnectDB : This is helper function to connect mongoDB
  90. func ConnectDB(db string, targetCollection string) *mongo.Collection {
  91. // Set client options
  92. //clientOptions := options.Client().ApplyURI("mongodb://mongoadmin:mongopassword@localhost:27017/?authSource=admin")
  93. clientOptions := options.Client().ApplyURI("mongodb://" + os.Getenv("MONGO_USER") + ":" +
  94. os.Getenv("MONGO_PASS") + "@" + os.Getenv("MONGO_HOST") + ":" + os.Getenv("MONGO_PORT") + os.Getenv("MONGO_OPTS") )
  95. // Connect to MongoDB
  96. client, err := mongo.Connect(context.TODO(), clientOptions)
  97. if err != nil {
  98. log.Fatal(err)
  99. }
  100. //collection := client.Database("go_rest_api").Collection("wg")
  101. collection := client.Database(db).Collection(targetCollection)
  102. return collection
  103. }
  104. // ErrorResponse : This is error model.
  105. type ErrorResponse struct {
  106. StatusCode int `json:"status"`
  107. ErrorMessage string `json:"message"`
  108. }
  109. // GetError : This is helper function to prepare error model.
  110. func GetError(err error, w http.ResponseWriter) {
  111. var response = ErrorResponse{
  112. ErrorMessage: err.Error(),
  113. StatusCode: http.StatusInternalServerError,
  114. }
  115. message, _ := json.Marshal(response)
  116. w.WriteHeader(response.StatusCode)
  117. w.Write(message)
  118. }