diff options
| author | Bobby <[email protected]> | 2026-03-06 23:01:58 +0530 |
|---|---|---|
| committer | Bobby <[email protected]> | 2026-03-06 23:01:58 +0530 |
| commit | ed2a033d7c08e448f5c6fd035e2de8f51431b597 (patch) | |
| tree | abe46463fc761b2e4266aadedb6b88c5493ce29b /database | |
| parent | 3f07a4b6c745707f135a7a97e93b0fa770b67873 (diff) | |
| download | dove-ed2a033d7c08e448f5c6fd035e2de8f51431b597.tar.xz dove-ed2a033d7c08e448f5c6fd035e2de8f51431b597.zip | |
Implement database connection and routing system with HTTP method support
Diffstat (limited to 'database')
| -rw-r--r-- | database/constants.go | 11 | ||||
| -rw-r--r-- | database/database.go | 36 | ||||
| -rw-r--r-- | database/functions.go | 22 |
3 files changed, 69 insertions, 0 deletions
diff --git a/database/constants.go b/database/constants.go new file mode 100644 index 0000000..e16dcde --- /dev/null +++ b/database/constants.go @@ -0,0 +1,11 @@ +package database + +import "time" + +const ( + DATABASE_FILE_NAME = "dove.db" + LOG_PREFIX = "Database" + MAX_IDLE_CONNECTIONS = 5 + MAX_OPEN_CONNECTIONS = 25 + MAX_CONNECTION_LIFETIME = time.Hour +) diff --git a/database/database.go b/database/database.go new file mode 100644 index 0000000..27b99eb --- /dev/null +++ b/database/database.go @@ -0,0 +1,36 @@ +package database + +import ( + "dove/messages" + "dove/utils/logger" + + "gorm.io/driver/sqlite" + "gorm.io/gorm" +) + +var DB *gorm.DB + +func init() { + databaseDSN := resolveDatabasePath() + dialector := sqlite.Open(databaseDSN) + + var connectionError error + DB, connectionError = gorm.Open(dialector, &gorm.Config{ + Logger: resolveGORMLogLevel(), + }) + + if connectionError != nil { + logger.Fatalf(LOG_PREFIX, messages.DatabaseConnectionFailed, connectionError) + } + + sqlDB, poolError := DB.DB() + if poolError != nil { + logger.Fatalf(LOG_PREFIX, messages.DatabasePoolConfigFailed, poolError) + } + + sqlDB.SetMaxOpenConns(MAX_OPEN_CONNECTIONS) + sqlDB.SetMaxIdleConns(MAX_IDLE_CONNECTIONS) + sqlDB.SetConnMaxLifetime(MAX_CONNECTION_LIFETIME) + + logger.Successf(LOG_PREFIX, messages.DatabaseConnected, databaseDSN) +} diff --git a/database/functions.go b/database/functions.go new file mode 100644 index 0000000..71723ca --- /dev/null +++ b/database/functions.go @@ -0,0 +1,22 @@ +package database + +import ( + "path/filepath" + + "dove/config" + + "gorm.io/gorm/logger" +) + +func resolveDatabasePath() string { + return filepath.Join(config.DataDir, DATABASE_FILE_NAME) +} + +func resolveGORMLogLevel() logger.Interface { + switch config.Server.Debug { + case true: + return logger.Default.LogMode(logger.Info) + default: + return logger.Default.LogMode(logger.Silent) + } +} |
