setting.go 6.49 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
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"`
wangp's avatar
wangp committed
27 28
	PayUrl          *PayUrlDetail           `mapstructure:"payurl"`
	Lakala          *Lakala                 `mapstructure:"lakala"`
wangp's avatar
wangp committed
29 30 31 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 62
	//SmsInternational *SmsInternationalConfig `mapstructure:"newsms"`
	//GateWay          *GateWayDetail          `mapstructure:"gateway"`
	//GateWayDev       *GateWayDevDetail       `mapstructure:"gatewaydev"`
	//Mongo            *MongoConfig            `mapstructure:"mongo"`
}

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
63
	SystemDB  string `mapstructure:"systemdb"`
wangp's avatar
wangp committed
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"`
//}
//
wangp's avatar
wangp committed
113
// PayUrlDetail 网关
wangp's avatar
wangp committed
114 115 116 117 118
type PayUrlDetail struct {
	DomainName string `mapstructure:"domainname"`
	//CheckOrder string `mapstructure:"checkorder"`
	//OrderState string `mapstructure:"orderstate"`
}
wangp's avatar
wangp committed
119

wangp's avatar
wangp committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
// 拉卡拉支付
type Lakala struct {
	// 通用
	Version string `mapstructure:"version"`
	Appid string `mapstructure:"appid"`
	SerialNo string `mapstructure:"serial_no"`

	// 1.聚合收银台(微信H5、支付宝H5)
	MerchantNo1 string `mapstructure:"merchant_no1"`
	TermNo1 string `mapstructure:"term_no1"`
	// 2.聚合收银台(微信扫码、支付宝扫码)
	MerchantNo2 string `mapstructure:"merchant_no2"`
	TermNo2 string `mapstructure:"term_no2"`
	// 3.聚合主扫(微信JSAPI、微信小程序)
	MerchantNo3 string `mapstructure:"merchant_no3"`
	TermNo3 string `mapstructure:"term_no3"`
	SubAppid3 string `mapstructure:"sub_appid3"`
	UserId3 string `mapstructure:"user_id3"`
	// 4.扫码枪
	MerchantNo4 string `mapstructure:"merchant_no4"`
	TermNo4 string `mapstructure:"term_no4"`
}

wangp's avatar
wangp committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
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
173
	viper.SetConfigFile("conf/dev/config.yaml") // 指定配置文件路径
wangp's avatar
wangp committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
	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
}