add Database type to cli and server config
This commit is contained in:
@@ -11,14 +11,15 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
CfgFile string
|
||||
Verbose int
|
||||
Overwrite bool
|
||||
ServerName string
|
||||
Mediatype MediaType
|
||||
Config CliConfig
|
||||
DryRun bool
|
||||
FileName string
|
||||
CfgFile string
|
||||
Verbose int
|
||||
Overwrite bool
|
||||
ServerName string
|
||||
Mediatype MediaType
|
||||
Config CliConfig
|
||||
ServerConfig ServerConfiguration
|
||||
DryRun bool
|
||||
FileName string
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -61,9 +62,31 @@ type Server struct {
|
||||
Password string `mapstructure:"password"`
|
||||
}
|
||||
|
||||
type Database struct {
|
||||
Type string `mapstructure:"type" validate:"required"`
|
||||
Path string `mapstructure:"path" validate:"required"`
|
||||
Host string `mapstructure:"host" validate:"required"`
|
||||
Name string `mapstructure:"name" validate:"required"`
|
||||
Port int `mapstructure:"port" validate:"required"`
|
||||
Password string `mapstructure:"password" validate:"required"`
|
||||
User string `mapstructure:"user" validate:"required"`
|
||||
}
|
||||
|
||||
type CliConfig struct {
|
||||
ApiVersion string
|
||||
Server map[string]Server `mapstructure:"server"`
|
||||
DB Database `mapstructure:"db"`
|
||||
}
|
||||
|
||||
type ServerConfiguration struct {
|
||||
Server struct {
|
||||
Port int `mapstructure:"port" validate:"required"`
|
||||
Host string `mapstructure:"host"`
|
||||
} `mapstructure:"server"`
|
||||
DB Database `mapstructure:"db"`
|
||||
Gin struct {
|
||||
Mode string `mapstructure:"mode" validate:"required"`
|
||||
} `mapstructure:"gin"`
|
||||
}
|
||||
|
||||
func InitCliConfig() {
|
||||
@@ -90,7 +113,7 @@ func InitCliConfig() {
|
||||
slog.Info("error reading config file", "error", err.Error())
|
||||
}
|
||||
} else {
|
||||
slog.Info("initConfig", "Using config file: ", viper.ConfigFileUsed())
|
||||
slog.Info("InitCliConfig", "Using config file: ", viper.ConfigFileUsed())
|
||||
if err := viper.Unmarshal(&Config); err != nil {
|
||||
slog.Info("parsing of config file failed", "error", err.Error())
|
||||
}
|
||||
@@ -98,13 +121,6 @@ func InitCliConfig() {
|
||||
}
|
||||
|
||||
func InitConfig() error {
|
||||
viper.SetDefault("db.host", "localhost")
|
||||
viper.SetDefault("db.port", 5432)
|
||||
viper.SetDefault("db.name", "kontor")
|
||||
viper.SetDefault("db.user", "kontor")
|
||||
viper.SetDefault("db.password", "kontor")
|
||||
viper.SetDefault("port", 8550)
|
||||
viper.SetDefault("gin.mode", "release")
|
||||
|
||||
viper.SetConfigName("kontor-gin")
|
||||
viper.SetConfigType("yaml")
|
||||
@@ -114,6 +130,15 @@ func InitConfig() error {
|
||||
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||
viper.AutomaticEnv()
|
||||
|
||||
viper.SetDefault("db.type", "postgres")
|
||||
viper.SetDefault("db.host", "localhost")
|
||||
viper.SetDefault("db.port", 5432)
|
||||
viper.SetDefault("db.name", "kontor")
|
||||
viper.SetDefault("db.user", "kontor")
|
||||
viper.SetDefault("db.password", "kontor")
|
||||
viper.SetDefault("db.path", "data/kontor.db")
|
||||
viper.SetDefault("server.port", 8550)
|
||||
viper.SetDefault("gin.mode", "release")
|
||||
// Read the config file
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
// Handle the case where config file is not found
|
||||
@@ -127,7 +152,10 @@ func InitConfig() error {
|
||||
return fmt.Errorf("error reading config file: %w", err)
|
||||
}
|
||||
} else {
|
||||
slog.Info("initConfig", "Using config file: ", viper.ConfigFileUsed())
|
||||
slog.Info("InitConfig", "Using config file: ", viper.ConfigFileUsed())
|
||||
if err := viper.Unmarshal(&ServerConfig); err != nil {
|
||||
slog.Info("parsing of config file failed", "error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -1,17 +1,42 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/viper"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
func NewDB(dsn string) (*gorm.DB, error) {
|
||||
func NewDB() (*gorm.DB, error) {
|
||||
db_type := ServerConfig.DB.Type
|
||||
slog.Info("using database", "type", db_type)
|
||||
|
||||
switch db_type {
|
||||
case "postgres":
|
||||
dsn := fmt.Sprintf(
|
||||
"host=%s user=%s password=%s dbname=%s port=%d sslmode=disable",
|
||||
viper.GetString("db.host"),
|
||||
viper.GetString("db.user"),
|
||||
viper.GetString("db.password"),
|
||||
viper.GetString("db.name"),
|
||||
viper.GetInt("db.port"),
|
||||
)
|
||||
|
||||
return gorm.Open(postgres.Open(dsn), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Info),
|
||||
})
|
||||
case "sqlite":
|
||||
return gorm.Open(sqlite.Open(viper.GetString("db.path")), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Info),
|
||||
})
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown db.type=%s", db_type)
|
||||
}
|
||||
// Configure GORM with custom settings
|
||||
// The logger helps with debugging SQL queries during development
|
||||
return gorm.Open(postgres.Open(dsn), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Info),
|
||||
})
|
||||
// if err != nil {
|
||||
// log.Fatal("Failed to connect to database:", err)
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user