2023-04-29 17:38:21 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"git.lumeweb.com/LumeWeb/portal/model"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"gorm.io/driver/mysql"
|
|
|
|
"gorm.io/driver/sqlite"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Declare a global variable to hold the database connection.
|
|
|
|
var db *gorm.DB
|
|
|
|
|
|
|
|
// Init initializes the database connection based on the app's configuration settings.
|
|
|
|
func Init() {
|
|
|
|
// If the database connection has already been initialized, panic.
|
|
|
|
if db != nil {
|
|
|
|
panic("DB already initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve database connection settings from the app's configuration using the viper library.
|
|
|
|
dbType := viper.GetString("database.type")
|
|
|
|
dbHost := viper.GetString("database.host")
|
|
|
|
dbPort := viper.GetInt("database.port")
|
|
|
|
dbSocket := viper.GetString("database.socket")
|
|
|
|
dbUser := viper.GetString("database.user")
|
|
|
|
dbPassword := viper.GetString("database.password")
|
|
|
|
dbName := viper.GetString("database.name")
|
2023-04-30 06:15:57 +00:00
|
|
|
dbPath := viper.GetString("database.path")
|
2023-04-29 17:38:21 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
var dsn string
|
|
|
|
switch dbType {
|
|
|
|
// Connect to a MySQL database.
|
|
|
|
case "mysql":
|
|
|
|
if dbSocket != "" {
|
2023-06-29 06:54:31 +00:00
|
|
|
dsn = fmt.Sprintf("%s:%s@unix(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", dbUser, dbPassword, dbSocket, dbName)
|
2023-04-29 17:38:21 +00:00
|
|
|
} else {
|
2023-06-29 06:54:31 +00:00
|
|
|
dsn = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", dbUser, dbPassword, dbHost, dbPort, dbName)
|
2023-04-29 17:38:21 +00:00
|
|
|
}
|
|
|
|
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
|
|
|
|
// Connect to a SQLite database.
|
|
|
|
case "sqlite":
|
2023-04-30 06:15:57 +00:00
|
|
|
db, err = gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
|
2023-04-29 17:38:21 +00:00
|
|
|
// If the database type is unsupported, panic.
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("Unsupported database type: %s \n", dbType))
|
|
|
|
}
|
|
|
|
// If there was an error connecting to the database, panic.
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("Failed to connect to database: %s \n", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Automatically migrate the database schema based on the model definitions.
|
2023-08-15 06:11:55 +00:00
|
|
|
err = db.Migrator().AutoMigrate(&model.Account{}, &model.Key{}, &model.KeyChallenge{}, &model.LoginSession{}, &model.Upload{}, &model.Pin{}, &model.Tus{}, &model.Dnslink{})
|
2023-04-29 17:38:21 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("Database setup failed database type: %s \n", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns the database connection instance.
|
|
|
|
func Get() *gorm.DB {
|
|
|
|
return db
|
|
|
|
}
|
2023-08-03 12:48:49 +00:00
|
|
|
func Close() error {
|
|
|
|
|
|
|
|
instance, _ := db.DB()
|
|
|
|
|
|
|
|
return instance.Close()
|
|
|
|
}
|