commit doble databse

This commit is contained in:
2025-05-28 08:10:10 +07:00
parent 1ead64f86e
commit 158b612993
33 changed files with 408 additions and 4454 deletions

View File

@@ -3,38 +3,100 @@ package database
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/mongo/readpref"
"log"
"os"
"time"
_ "github.com/jackc/pgx/v5/stdlib"
_ "github.com/joho/godotenv/autoload"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"log"
"os"
"strconv"
"strings"
"time"
)
// Service represents a service that interacts with a database.
type Service interface {
Health() map[string]string
GetDB() *mongo.Database
Close() error
GetDB(database string) *gorm.DB
GetMongoDB() *mongo.Database
}
type service struct {
client *mongo.Client
db *mongo.Database
simrsDB *gorm.DB
satuDataDB *gorm.DB
client *mongo.Client
mongoDB *mongo.Database
}
var (
host = os.Getenv("MONGODB_DEV_HOST")
port = os.Getenv("MONGODB_DEV_PORT")
user = os.Getenv("MONGODB_DEV_USER")
pass = os.Getenv("MONGODB_DEV_PASS")
hostSimrs = os.Getenv("SIMRS_STAG_HOST")
userNameSimrs = os.Getenv("SIMRS_STAG_USER")
passwordSimrs = os.Getenv("SIMRS_STAG_PASS")
dbNameSimrs = os.Getenv("SIMRS_STAG_NAME")
portSimrs = os.Getenv("SIMRS_STAG_PORT")
hostSatudata = os.Getenv("SATUDATA_HOST")
userNameSatudata = os.Getenv("SATUDATA_USER")
passwordSatudata = os.Getenv("SATUDATA_PASS")
dbNameSatudata = os.Getenv("SATUDATA_NAME")
portSatudata = os.Getenv("SATUDATA_PORT")
hostMongo = os.Getenv("MONGODB_DEV_HOST")
portMongo = os.Getenv("MONGODB_DEV_PORT")
userMongo = os.Getenv("MONGODB_DEV_USER")
passMongo = os.Getenv("MONGODB_DEV_PASS")
dbInstance *service
)
func New(database string) Service {
mongoURI := fmt.Sprintf("mongodb://%s:%s@%s:%s/%s?authSource=admin",
user, pass, host, port, database)
func New(database ...string) Service {
mongoDBName := os.Getenv("MONGODB_DEV_MASTER")
if len(database) > 0 {
mongoDBName = database[0]
}
if dbInstance != nil {
if database != nil {
mongoURI := fmt.Sprintf("mongodb://%s:%s@%s:%s/%s?authSource=admin",
userMongo, passMongo, hostMongo, portMongo, mongoDBName)
log.Println("Connecting to MongoDB...")
log.Println(mongoURI)
clientOptions := options.Client().ApplyURI(mongoURI)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, clientOptions)
if err == nil {
log.Println("Connected to MongoDB")
}
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)
}
dbInstance.mongoDB = client.Database(mongoDBName)
}
return dbInstance
}
simrs := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Jakarta", hostSimrs, userNameSimrs, passwordSimrs, dbNameSimrs, portSimrs)
SimrsDB, err := gorm.Open(postgres.Open(simrs), &gorm.Config{})
if err != nil {
log.Fatal("Failed to connect to SIM database: ", err)
} else {
log.Println("Successfully connected to the database")
}
satudata := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Jakarta", hostSatudata, userNameSatudata, passwordSatudata, dbNameSatudata, portSatudata)
SatudataDB, err := gorm.Open(postgres.Open(satudata), &gorm.Config{})
if err != nil {
log.Fatal("Failed to connect to SIM database: ", err)
} else {
log.Println("Successfully connected to the database")
}
mongoURI := fmt.Sprintf("mongodb://%s:%s@%s:%s/%s?authSource=admin",
userMongo, passMongo, hostMongo, portMongo, database)
var client *mongo.Client
var err error
log.Println("Connecting to MongoDB...")
log.Println(mongoURI)
clientOptions := options.Client().ApplyURI(mongoURI)
@@ -63,31 +125,104 @@ func New(database string) Service {
}
log.Println("Successfully connected to MongoDB!")
return &service{
client: client,
db: client.Database(database),
dbInstance = &service{
simrsDB: SimrsDB,
satuDataDB: SatudataDB,
mongoDB: client.Database(mongoDBName),
}
return dbInstance
}
// Health checks the health of the database connection by pinging the database.
// It returns a map with keys indicating various health statistics.
func (s *service) Health() map[string]string {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
status := map[string]string{
"status": "up",
"message": "MongoDB connection is healthy",
stats := make(map[string]string)
// Ping the database using GORM
db, err := s.simrsDB.DB() // Retrieve the underlying sql.DB instance from GORM
if err != nil {
stats["status"] = "down"
stats["error"] = fmt.Sprintf("failed to get sql.DB from GORM: %v", err)
log.Fatalf("failed to get sql.DB from GORM: %v", err) // Log the error and terminate the program
return stats
}
if err := s.client.Ping(ctx, nil); err != nil {
status["status"] = "down"
status["error"] = err.Error()
status["message"] = "Connection to MongoDB failed"
err = db.PingContext(ctx)
if err != nil {
stats["status"] = "down"
stats["error"] = fmt.Sprintf("db down: %v", err)
log.Fatalf("db down: %v", err) // Log the error and terminate the program
return stats
}
return status
// Database is up, add more statistics
stats["status"] = "up"
stats["message"] = "It's healthy"
// Get database stats
dbStats := db.Stats()
stats["open_connections"] = strconv.Itoa(dbStats.OpenConnections)
stats["in_use"] = strconv.Itoa(dbStats.InUse)
stats["idle"] = strconv.Itoa(dbStats.Idle)
stats["wait_count"] = strconv.FormatInt(dbStats.WaitCount, 10)
stats["wait_duration"] = dbStats.WaitDuration.String()
stats["max_idle_closed"] = strconv.FormatInt(dbStats.MaxIdleClosed, 10)
stats["max_lifetime_closed"] = strconv.FormatInt(dbStats.MaxLifetimeClosed, 10)
// Evaluate stats to provide a health message
if dbStats.OpenConnections > 40 { // Assuming 50 is the max for this example
stats["message"] = "The database is experiencing heavy load."
}
if dbStats.WaitCount > 1000 {
stats["message"] = "The database has a high number of wait events, indicating potential bottlenecks."
}
if dbStats.MaxIdleClosed > int64(dbStats.OpenConnections)/2 {
stats["message"] = "Many idle connections are being closed, consider revising the connection pool settings."
}
if dbStats.MaxLifetimeClosed > int64(dbStats.OpenConnections)/2 {
stats["message"] = "Many connections are being closed due to max lifetime, consider increasing max lifetime or revising the connection usage pattern."
}
return stats
}
func (s *service) GetDB() *mongo.Database {
return s.db
// Close closes the database connection.
// It logs a message indicating the disconnection from the specific database.
// If the connection is successfully closed, it returns nil.
// If an error occurs while closing the connection, it returns the error.
func (s *service) Close() error {
db, err := s.simrsDB.DB() // Retrieve the underlying sql.DB instance from GORM
if err != nil {
log.Printf("Failed to retrieve sql.DB from GORM: %v", err)
return err
}
err = db.Close()
if err != nil {
log.Printf("Error closing the database connection: %v", err)
return err
}
log.Printf("Disconnected from database successfully")
return nil
}
func (s *service) GetDB(database string) *gorm.DB {
if strings.ToLower(database) == "simrs" {
return s.simrsDB
} else if strings.ToLower(database) == "satudata" {
return s.satuDataDB
}
log.Println("Database tidak ditemukan")
return nil
}
func (s *service) GetMongoDB() *mongo.Database {
return s.mongoDB
}

View File

@@ -1,22 +1,14 @@
package server
import (
"net/http"
"fmt"
"log"
"time"
"github.com/gin-gonic/gin"
"github.com/a-h/templ"
"io/fs"
"template_blueprint/cmd/web"
"github.com/coder/websocket"
"github.com/gin-contrib/cors"
patientHandler "template_blueprint/pkg/handlers/patient"
"github.com/gin-gonic/gin"
"log"
"net/http"
LoginHandler "template_blueprint/pkg/handlers/login"
"time"
)
func (s *Server) RegisterRoutes() http.Handler {
@@ -26,23 +18,13 @@ func (s *Server) RegisterRoutes() http.Handler {
r.GET("/websocket", s.websocketHandler)
staticFiles, _ := fs.Sub(web.Files, "assets")
r.StaticFS("/assets", http.FS(staticFiles))
r.GET("/web", func(c *gin.Context) {
templ.Handler(web.HelloForm()).ServeHTTP(c.Writer, c.Request)
})
r.POST("/hello", func(c *gin.Context) {
web.HelloWebHandler(c.Writer, c.Request)
})
api := r.Group("/api")
patient := api.Group("/patient")
login := api.Group("/login")
{
patient.POST("/insertpatient", patientHandler.InsertPatient)
patient.GET("/getallpatient", patientHandler.GetAllPatient)
login.GET("/get", LoginHandler.Getlogin)
login.GET("/:id", LoginHandler.GetloginbyID)
}
r.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"}, // or specific domains like "http://example.com"
AllowMethods: []string{"GET", "POST", "PUT", "DELETE"},