123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- package config
- import (
- "fmt"
- "regexp"
- "github.com/spf13/viper"
- )
- /*
- Config 配置结构体,包含导入和导出相关的配置
- */
- type Config struct {
- ImportConfig ImportConfig // 导入配置
- ExportConfig ExportConfig // 导出配置
- }
- /*
- ImportConfig 导入配置结构体
- 包含数据库配置、导入目录、分隔符、分页大小以及文件名匹配模式等配置
- */
- type ImportConfig struct {
- DBConfig DBConfig // 数据库配置
- Delimiter rune // 导入文件的分隔符
- PageSize int // 分页读取的大小
- FilenamePattern *regexp.Regexp // 文件名匹配的正则表达式
- }
- /*
- ExportConfig 导出配置结构体
- 包含数据库配置、导出表名、文件路径、分隔符、文件大小上限以及分页大小等配置
- */
- type ExportConfig struct {
- DBConfig DBConfig // 数据库配置
- BaseFilePath string // 导出文件的基本路径
- Delimiter rune // CSV 的分隔符
- MaxFileSize int64 // 单个文件的最大大小 (字节)
- PageSize int // 每次分页读取的行数
- }
- /*
- DBConfig 数据库配置结构体
- 包含数据库类型、名称、用户名、密码、IP 地址和端口等信息
- */
- type DBConfig struct {
- DBType string // 数据库类型 (如 MySQL, PostgreSQL 等)
- DBName string // 数据库名称
- User string // 数据库用户名
- Password string // 数据库密码
- IP string // 数据库 IP 地址
- Port int // 数据库端口号
- }
- func LoadConfig() (*Config, error) {
- viper.SetConfigName("config") // 配置文件名(不带扩展名)
- viper.SetConfigType("toml") // 配置文件类型
- viper.AddConfigPath(".") // 查找配置文件的路径
- if err := viper.ReadInConfig(); err != nil {
- return nil, fmt.Errorf("error reading config file: %v", err)
- }
- // 解析 Import 配置
- importPattern, err := regexp.Compile(viper.GetString("import.cfg.filename_pattern"))
- if err != nil {
- return nil, fmt.Errorf("error compiling filename pattern: %v", err)
- }
- importConfig := ImportConfig{
- DBConfig: DBConfig{
- DBType: viper.GetString("import.db_type"),
- DBName: viper.GetString("import.dbname"),
- User: viper.GetString("import.user"),
- Password: viper.GetString("import.password"),
- IP: viper.GetString("import.ip"),
- Port: viper.GetInt("import.port"),
- },
- Delimiter: rune(viper.GetString("import.cfg.delimiter")[0]),
- PageSize: viper.GetInt("import.cfg.page_size"),
- FilenamePattern: importPattern,
- }
- // 解析 Export 配置
- exportConfig := ExportConfig{
- DBConfig: DBConfig{
- DBType: viper.GetString("export.db_type"),
- DBName: viper.GetString("export.dbname"),
- User: viper.GetString("export.user"),
- Password: viper.GetString("export.password"),
- IP: viper.GetString("export.ip"),
- Port: viper.GetInt("export.port"),
- },
- BaseFilePath: viper.GetString("export.cfg.base_file_path"),
- Delimiter: rune(viper.GetString("export.cfg.delimiter")[0]),
- MaxFileSize: viper.GetInt64("export.cfg.max_file_size"),
- PageSize: viper.GetInt("export.cfg.page_size"),
- }
- return &Config{
- ImportConfig: importConfig,
- ExportConfig: exportConfig,
- }, nil
- }
- func (dbConfig *DBConfig) GetDSN() string {
- switch dbConfig.DBType {
- case "mysql":
- return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", dbConfig.User, dbConfig.Password, dbConfig.IP, dbConfig.Port, dbConfig.DBName)
- case "xugu":
- return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", dbConfig.User, dbConfig.Password, dbConfig.IP, dbConfig.Port, dbConfig.DBName)
- }
- return ""
- }
|