setting.go 5.65 KB
Newer Older
wangp's avatar
wangp committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
package setting

import (
	"fmt"
	"strconv"
	"time"

	"github.com/fsnotify/fsnotify"
	"github.com/spf13/viper"
)

var Conf = new(Config)

type Config struct {
	ServerSetting   *Server                 `mapstructure:"server"`
	AppSetting      *App                    `mapstructure:"app"`
	DatabaseSetting *Database               `mapstructure:"database"`
	Redis           *RedisConfig            `mapstructure:"redis"`
	LogSetting      *Log                    `mapstructure:"log"`
	Sms             *SmsInternationalConfig `mapstructure:"sms"`
	UploadImage     *UploadImage            `mapstructure:"uploadimage"`
	Oss             *Oss                    `mapstructure:"oss"`
	AliCloud        *AliCloud               `mapstructure:"alicloud"`
	Esign           *Esign                  `mapstructure:"esign"`
	JwtSecret       string                  `mapstructure:"jwtsecret"`
	DesKey          string                  `mapstructure:"deskey"`
	//SmsInternational *SmsInternationalConfig `mapstructure:"newsms"`
	//GateWay          *GateWayDetail          `mapstructure:"gateway"`
	//GateWayDev       *GateWayDevDetail       `mapstructure:"gatewaydev"`
	//Mongo            *MongoConfig            `mapstructure:"mongo"`
wangp's avatar
wangp committed
31
	PayUrl           *PayUrlDetail           `mapstructure:"payurl"`
wangp's avatar
wangp committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
}

type App struct {
	Name       string `mapstructure:"name"`
	Version    string `mapstructure:"version"`
	TimeFormat string `mapstructure:"timeformat""`
}

type Log struct {
	LogSavePath string `mapstructure:"logsavepath"`
	LogSaveName string `mapstructure:"logsavename"`
	LogFileExt  string `mapstructure:"logfileext"`
	Level       string `mapstructure:"level"`
	MaxSize     int    `mapstructure:"max_size"`
	MaxAge      int    `mapstructure:"max_age"`
	MaxBackups  int    `mapstructure:"max_backups"`
}

// Server 服务
type Server struct {
	RunMode      string        `mapstructure:"runmode"`
	HttpPort     int           `mapstructure:"httpport"`
	ReadTimeout  time.Duration `mapstructure:"readtimeout"`
	WriteTimeout time.Duration `mapstructure:"writetimeout"`
}

// Database DB
type Database struct {
	Type      string `mapstructure:"type""`
	ShopDB    string `mapstructure:"shopdb"`
wangp's avatar
wangp committed
62
	SystemDB  string `mapstructure:"systemdb"`
wangp's avatar
wangp committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
	UserDB    string `mapstructure:"userdb"`
	SecondDB  string `mapstructure:"seconddb"`
	AccountDB string `mapstructure:"accountdb"`
	PayDB     string `mapstructure:"paydb"`
	//MaxOpenConns int    `mapstructure:"max_open_conns"`
	//MaxIdleConns int    `mapstructure:"max_idle_conns"`
	//Host         string `mapstructure:"host"`
	//User         string `mapstructure:"user""`
	//Password     string `mapstructure:"password""`
	//Port         int    `mapstructure:"port"`
	//DB           string `mapstructure:"dbname"`
}

// RedisConfig redis
type RedisConfig struct {
	Host         string `mapstructure:"host"`
	Password     string `mapstructure:"password"`
	Port         int    `mapstructure:"port"`
	DB           int    `mapstructure:"db"`
	PoolSize     int    `mapstructure:"pool_size"`
	MinIdleConns int    `mapstructure:"min_idle_conns"`
}

// MongoConfig mongo设置
//type MongoConfig struct {
//	DBUrl        string `mapstructure:"dburl"`
//	MaxOpenConns uint64 `mapstructure:"max_open_conns"`
//}

// SmsInternationalConfig 国际化短信
type SmsInternationalConfig struct {
	SmsKey     string `mapstructure:"smskey"`
	SendSmsUrl string `mapstructure:"sendsmsurl"`
	SmsContent string `mapstructure:"smscontent"`
}

//
//// GateWayDetail 网关
//type GateWayDetail struct {
//	URL  string `mapstructure:"url"`
//	Port string `mapstructure:"port"`
//}
//
//// GateWayDevDetail 网关
//type GateWayDevDetail struct {
//	URL  string `mapstructure:"url"`
//	Port string `mapstructure:"port"`
//}
//
//// PayUrlDetail 网关
wangp's avatar
wangp committed
113 114 115 116 117
type PayUrlDetail struct {
	DomainName string `mapstructure:"domainname"`
	//CheckOrder string `mapstructure:"checkorder"`
	//OrderState string `mapstructure:"orderstate"`
}
wangp's avatar
wangp committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

type UploadImage struct {
	UploadDir       string `mapstructure:"upload_dir"`
	MaxFileSize     int    `mapstructure:"max_file_size"`
	ImageTypes      string `mapstructure:"image_types""`
	AcceptFileTypes string `mapstructure:"accept_file_types""`
}

type Oss struct {
	OssUrl          string `mapstructure:"oss_url"`
	AccessKeyID     string `mapstructure:"accesskeyid"`
	AccessKeySecret string `mapstructure:"accesskeysecret""`
	EndPoint        string `mapstructure:"endpoint""`
	Bucket          string `mapstructure:"bucket""`
}

type AliCloud struct {
	AppCode      string `mapstructure:"appcode"`
	TimeDuration int    `mapstructure:"time_duration"`
}

//e签宝
type Esign struct {
	ProjectId     string `mapstructure:"project_id"`
	ProjectSecret string `mapstructure:"project_secret"`
	OrganAuthUrl  string `mapstructure:"organ_auth_url"`
	InfoAuthUrl   string `mapstructure:"info_auth_url"`
}

// Init 支持热修改的viper设置
func Init() error {
wangp's avatar
wangp committed
149
	viper.SetConfigFile("conf/dev/config.yaml") // 指定配置文件路径
wangp's avatar
wangp committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
	err := viper.ReadInConfig()             // 读取配置信息
	if err != nil {                         // 读取配置信息失败
		fmt.Printf("viper.ReadInConfig failed, new_error:%v\n", err)
		return err
	}
	// 将读取的配置信息保存至全局变量Conf
	if err = viper.Unmarshal(Conf); err != nil {
		fmt.Printf("viper.Unmarshal failed, new_error:%v\n", err)
	}
	// 监控配置文件变化
	viper.WatchConfig()
	// 注意!!!配置文件发生变化后要同步到全局变量Conf
	viper.OnConfigChange(func(in fsnotify.Event) {
		fmt.Println("配置文件被修改啦...")
		if err = viper.Unmarshal(Conf); err != nil {
			fmt.Printf("viper.Unmarshal failed, new_error:%v\n", err)
		}
		//fmt.Println(Conf)
	})

	return err

}

func GetPort() string {
	s := strconv.Itoa(Conf.ServerSetting.HttpPort)
	return ":" + s
}