update db connection
This commit is contained in:
@@ -3,51 +3,91 @@ package database
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo/readpref"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
_ "github.com/joho/godotenv/autoload"
|
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Service interface {
|
type Service interface {
|
||||||
Health() map[string]string
|
Health() map[string]string
|
||||||
|
GetDB() *mongo.Database
|
||||||
}
|
}
|
||||||
|
|
||||||
type service struct {
|
type service struct {
|
||||||
db *mongo.Client
|
client *mongo.Client
|
||||||
|
db *mongo.Database
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
host = os.Getenv("BLUEPRINT_DB_HOST")
|
host = os.Getenv("BLUEPRINT_DB_HOST")
|
||||||
port = os.Getenv("BLUEPRINT_DB_PORT")
|
port = os.Getenv("BLUEPRINT_DB_PORT")
|
||||||
//database = os.Getenv("BLUEPRINT_DB_DATABASE")
|
user = os.Getenv("BLUEPRINT_DB_USER")
|
||||||
|
pass = os.Getenv("BLUEPRINT_DB_PASS")
|
||||||
)
|
)
|
||||||
|
|
||||||
func New() Service {
|
func New(database string) Service {
|
||||||
client, err := mongo.Connect(context.Background(), options.Client().ApplyURI(fmt.Sprintf("mongodb://%s:%s", host, port)))
|
mongoURI := fmt.Sprintf("mongodb://%s:%s@%s:%s/%s?authSource=admin",
|
||||||
|
user, pass, host, port, database)
|
||||||
|
|
||||||
|
var client *mongo.Client
|
||||||
|
var err error
|
||||||
|
log.Println("Connecting to MongoDB...")
|
||||||
|
log.Println(mongoURI)
|
||||||
|
clientOptions := options.Client().ApplyURI(mongoURI)
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
client, err = mongo.Connect(ctx, clientOptions)
|
||||||
|
if err == nil {
|
||||||
|
log.Println("Connected to MongoDB")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
log.Printf("Attempt %d: Failed to connect to MongoDB, retrying in 5 seconds...\n", i+1)
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatalf("Failed to create client: %v", err)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Verify connection
|
||||||
|
if err = client.Ping(ctx, readpref.Primary()); err != nil {
|
||||||
|
log.Println("Failed to connect to MongoDB!!!")
|
||||||
|
log.Fatalf("Failed to connect to database: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("Successfully connected to MongoDB!")
|
||||||
|
|
||||||
return &service{
|
return &service{
|
||||||
db: client,
|
client: client,
|
||||||
|
db: client.Database(database),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) Health() map[string]string {
|
func (s *service) Health() map[string]string {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
err := s.db.Ping(ctx, nil)
|
status := map[string]string{
|
||||||
if err != nil {
|
"status": "up",
|
||||||
log.Fatalf(fmt.Sprintf("db down: %v", err))
|
"message": "MongoDB connection is healthy",
|
||||||
}
|
}
|
||||||
|
|
||||||
return map[string]string{
|
if err := s.client.Ping(ctx, nil); err != nil {
|
||||||
"message": "It's healthy",
|
status["status"] = "down"
|
||||||
|
status["error"] = err.Error()
|
||||||
|
status["message"] = "Connection to MongoDB failed"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return status
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *service) GetDB() *mongo.Database {
|
||||||
|
return s.db
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,10 +20,11 @@ type Server struct {
|
|||||||
|
|
||||||
func NewServer() *http.Server {
|
func NewServer() *http.Server {
|
||||||
port, _ := strconv.Atoi(os.Getenv("PORT"))
|
port, _ := strconv.Atoi(os.Getenv("PORT"))
|
||||||
|
master := os.Getenv("BLUEPRINT_DB_MASTER")
|
||||||
NewServer := &Server{
|
NewServer := &Server{
|
||||||
port: port,
|
port: port,
|
||||||
|
|
||||||
db: database.New(),
|
db: database.New(master),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Declare Server config
|
// Declare Server config
|
||||||
|
|||||||
Reference in New Issue
Block a user