diff options
Diffstat (limited to 'nexus/database')
| -rw-r--r-- | nexus/database/database.go | 49 | ||||
| -rw-r--r-- | nexus/database/defaults.go | 10 | ||||
| -rw-r--r-- | nexus/database/logger.go | 14 | ||||
| -rw-r--r-- | nexus/database/messages.go | 9 | ||||
| -rw-r--r-- | nexus/database/migration.go | 7 |
5 files changed, 89 insertions, 0 deletions
diff --git a/nexus/database/database.go b/nexus/database/database.go new file mode 100644 index 0000000..e00c494 --- /dev/null +++ b/nexus/database/database.go @@ -0,0 +1,49 @@ +package database
+
+import (
+ "fmt"
+ "nexus/config"
+ "nexus/utils/logger"
+
+ "gorm.io/driver/postgres"
+ "gorm.io/gorm"
+)
+
+var DB *gorm.DB
+
+func init() {
+ dsn := fmt.Sprintf(
+ "host=%s user=%s dbname=%s port=%d sslmode=%s",
+ config.Database.Host,
+ config.Database.User,
+ config.Database.Name,
+ config.Database.Port,
+ config.Database.SSLMode,
+ )
+
+ if config.Database.Password != "" {
+ dsn += fmt.Sprintf(" password=%s", config.Database.Password)
+ }
+
+ var connectionError error
+ DB, connectionError = gorm.Open(postgres.Open(dsn), &gorm.Config{
+ Logger: resolveGORMLogLevel(),
+ })
+
+ if connectionError != nil {
+ logger.Fatalf(LogPrefix, ConnectionFailed, connectionError)
+ }
+
+ sqlDB, poolError := DB.DB()
+ if poolError != nil {
+ logger.Fatalf(LogPrefix, PoolConfigFailed, poolError)
+ }
+
+ sqlDB.SetMaxOpenConns(MaxOpenConnections)
+ sqlDB.SetMaxIdleConns(MaxIdleConnections)
+ sqlDB.SetConnMaxLifetime(MaxConnectionLifetime)
+
+ logger.Successf(LogPrefix, Connected, config.Database.Name)
+
+ migrate()
+}
diff --git a/nexus/database/defaults.go b/nexus/database/defaults.go new file mode 100644 index 0000000..222493f --- /dev/null +++ b/nexus/database/defaults.go @@ -0,0 +1,10 @@ +package database
+
+import "time"
+
+const (
+ LogPrefix = "Database"
+ MaxOpenConnections = 25
+ MaxIdleConnections = 5
+ MaxConnectionLifetime = 5 * time.Minute
+)
diff --git a/nexus/database/logger.go b/nexus/database/logger.go new file mode 100644 index 0000000..a5eb704 --- /dev/null +++ b/nexus/database/logger.go @@ -0,0 +1,14 @@ +package database
+
+import (
+ "nexus/config"
+
+ "gorm.io/gorm/logger"
+)
+
+func resolveGORMLogLevel() logger.Interface {
+ if config.Server.Debug {
+ return logger.Default.LogMode(logger.Info)
+ }
+ return logger.Default.LogMode(logger.Silent)
+}
diff --git a/nexus/database/messages.go b/nexus/database/messages.go new file mode 100644 index 0000000..b2cb8e4 --- /dev/null +++ b/nexus/database/messages.go @@ -0,0 +1,9 @@ +package database
+
+const (
+ ConnectionFailed = "Failed to connect to database: %v"
+ PoolConfigFailed = "Failed to configure connection pool: %v"
+ Connected = "Connected to database: %s"
+ MigrationFailed = "Migration failed: %v"
+ MigrationDone = "Migrations complete"
+)
diff --git a/nexus/database/migration.go b/nexus/database/migration.go new file mode 100644 index 0000000..246dc39 --- /dev/null +++ b/nexus/database/migration.go @@ -0,0 +1,7 @@ +package database
+
+import "nexus/utils/logger"
+
+func migrate() {
+ logger.Successf(LogPrefix, MigrationDone)
+}
|
