120 lines
3.7 KiB
Go
120 lines
3.7 KiB
Go
package initialize
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"reflect"
|
|
"strings"
|
|
|
|
"github.com/dungnt11/senflow_app/global"
|
|
"github.com/fatih/color"
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
func LoadConfig() error {
|
|
err := godotenv.Load()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Cấu hình server
|
|
global.Config.Server.Port = os.Getenv("PORT")
|
|
global.Config.Server.AppEnv = os.Getenv("APP_ENV")
|
|
|
|
// Cấu hình database
|
|
global.Config.Database.Host = os.Getenv("BLUEPRINT_DB_HOST")
|
|
global.Config.Database.Port = os.Getenv("BLUEPRINT_DB_PORT")
|
|
global.Config.Database.Database = os.Getenv("BLUEPRINT_DB_DATABASE")
|
|
global.Config.Database.Username = os.Getenv("BLUEPRINT_DB_USERNAME")
|
|
global.Config.Database.Password = os.Getenv("BLUEPRINT_DB_PASSWORD")
|
|
global.Config.Database.RootPassword = os.Getenv("BLUEPRINT_DB_ROOT_PASSWORD")
|
|
|
|
// Chuyển đổi các giá trị cấu hình database từ string sang int
|
|
if maxIdleConns := os.Getenv("BLUEPRINT_DB_MAX_IDLE_CONNS"); maxIdleConns != "" {
|
|
fmt.Sscanf(maxIdleConns, "%d", &global.Config.Database.MaxIdleConns)
|
|
}
|
|
|
|
if maxOpenConns := os.Getenv("BLUEPRINT_DB_MAX_OPEN_CONNS"); maxOpenConns != "" {
|
|
fmt.Sscanf(maxOpenConns, "%d", &global.Config.Database.MaxOpenConns)
|
|
}
|
|
|
|
if connMaxLifetime := os.Getenv("BLUEPRINT_DB_CONN_MAX_LIFETIME"); connMaxLifetime != "" {
|
|
fmt.Sscanf(connMaxLifetime, "%d", &global.Config.Database.ConnMaxLifetime)
|
|
}
|
|
|
|
// Cấu hình logger
|
|
global.Config.Logger.LogLevel = os.Getenv("LOGGER_LOG_LEVEL")
|
|
global.Config.Logger.FileLogName = os.Getenv("LOGGER_FILE_LOG_NAME")
|
|
|
|
// Chuyển đổi từ string sang int và bool
|
|
if maxSize := os.Getenv("LOGGER_MAX_SIZE"); maxSize != "" {
|
|
fmt.Sscanf(maxSize, "%d", &global.Config.Logger.MaxSize)
|
|
}
|
|
|
|
if maxBackups := os.Getenv("LOGGER_MAX_BACKUPS"); maxBackups != "" {
|
|
fmt.Sscanf(maxBackups, "%d", &global.Config.Logger.MaxBackups)
|
|
}
|
|
|
|
if maxAge := os.Getenv("LOGGER_MAX_AGE"); maxAge != "" {
|
|
fmt.Sscanf(maxAge, "%d", &global.Config.Logger.MaxAge)
|
|
}
|
|
|
|
if compress := os.Getenv("LOGGER_COMPRESS"); compress != "" {
|
|
global.Config.Logger.Compress = strings.ToLower(compress) == "true"
|
|
}
|
|
|
|
printConfig()
|
|
|
|
return nil
|
|
}
|
|
|
|
func printConfig() {
|
|
// Tạo các đối tượng màu sắc
|
|
titleColor := color.New(color.FgHiCyan, color.Bold)
|
|
sectionColor := color.New(color.FgHiYellow, color.Bold)
|
|
keyColor := color.New(color.FgHiGreen)
|
|
valueColor := color.New(color.FgHiWhite)
|
|
|
|
fmt.Println() // Thêm dòng trống ở đầu
|
|
|
|
// In tiêu đề
|
|
titleColor.Println("✨✨✨ CẤU HÌNH ỨNG DỤNG ✨✨✨")
|
|
fmt.Println()
|
|
|
|
// Sử dụng reflection để tự động in tất cả các cấu hình
|
|
configValue := reflect.ValueOf(global.Config)
|
|
configType := configValue.Type()
|
|
|
|
// Duyệt qua tất cả các trường của cấu hình
|
|
for i := 0; i < configValue.NumField(); i++ {
|
|
sectionName := configType.Field(i).Name
|
|
sectionValue := configValue.Field(i)
|
|
sectionType := sectionValue.Type()
|
|
|
|
// In tên section
|
|
sectionColor.Printf("[%s]\n", strings.ToUpper(sectionName))
|
|
|
|
// Duyệt qua tất cả các trường của section
|
|
for j := 0; j < sectionValue.NumField(); j++ {
|
|
fieldName := sectionType.Field(j).Name
|
|
fieldValue := sectionValue.Field(j).Interface()
|
|
|
|
// Ẩn mật khẩu
|
|
displayValue := fmt.Sprintf("%v", fieldValue)
|
|
if strings.Contains(strings.ToLower(fieldName), "password") {
|
|
displayValue = "******** (ẩn)"
|
|
}
|
|
|
|
// In tên trường và giá trị
|
|
keyColor.Printf(" %-15s: ", fieldName)
|
|
valueColor.Printf("%s\n", displayValue)
|
|
}
|
|
|
|
fmt.Println() // Thêm dòng trống giữa các section
|
|
}
|
|
|
|
// In thông báo thành công
|
|
titleColor.Println("✅ Cấu hình đã được tải thành công!")
|
|
fmt.Println() // Thêm dòng trống ở cuối
|
|
}
|