forked from free-lancers/electricity_bill_calc_service
		
	build(init):创建基本项目结构。
This commit is contained in:
		
							
								
								
									
										54
									
								
								config/configuration.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								config/configuration.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,54 @@ | ||||
| package config | ||||
|  | ||||
| import ( | ||||
| 	"github.com/spf13/viper" | ||||
| ) | ||||
|  | ||||
| type ConfigurationFile struct { | ||||
| 	vp *viper.Viper | ||||
| } | ||||
|  | ||||
| var ( | ||||
| 	sections = make(map[string]interface{}) | ||||
| ) | ||||
|  | ||||
| func NewConfigurationFile() (*ConfigurationFile, error) { | ||||
| 	vp := viper.New() | ||||
| 	vp.SetConfigType("yaml") | ||||
| 	vp.SetConfigName("settings") | ||||
| 	vp.AddConfigPath(".") | ||||
| 	err := vp.ReadInConfig() | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	s := &ConfigurationFile{vp} | ||||
| 	vp.SetConfigName("settings.local") | ||||
| 	err = vp.MergeInConfig() | ||||
| 	if err != nil { | ||||
| 		return s, nil | ||||
| 	} | ||||
| 	return s, nil | ||||
| } | ||||
|  | ||||
| func (s *ConfigurationFile) ReadSection(k string, v interface{}) error { | ||||
| 	err := s.vp.UnmarshalKey(k, v) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	if _, ok := sections[k]; !ok { | ||||
| 		sections[k] = v | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (s *ConfigurationFile) ReloadAllSection() error { | ||||
| 	for k, v := range sections { | ||||
| 		err := s.ReadSection(k, v) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
							
								
								
									
										60
									
								
								config/settings.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								config/settings.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,60 @@ | ||||
| package config | ||||
|  | ||||
| import ( | ||||
| 	"time" | ||||
| ) | ||||
|  | ||||
| //服务器配置 | ||||
| type ServerSetting struct { | ||||
| 	RunMode      string | ||||
| 	HttpPort     int | ||||
| 	ReadTimeout  time.Duration | ||||
| 	WriteTimeout time.Duration | ||||
| } | ||||
|  | ||||
| //数据库配置 | ||||
| type DatabaseSetting struct { | ||||
| 	User         string | ||||
| 	Pass         string | ||||
| 	Host         string | ||||
| 	Port         int | ||||
| 	DB           string | ||||
| 	MaxIdleConns int | ||||
| 	MaxOpenConns int | ||||
| } | ||||
| type RedisSetting struct { | ||||
| 	Host     string | ||||
| 	Port     int | ||||
| 	Password string | ||||
| 	DB       int | ||||
| } | ||||
|  | ||||
| //定义全局变量 | ||||
| var ( | ||||
| 	ServerSettings   *ServerSetting | ||||
| 	DatabaseSettings *DatabaseSetting | ||||
| 	RedisSettings    *RedisSetting | ||||
| ) | ||||
|  | ||||
| //读取配置到全局变量 | ||||
| func SetupSetting() error { | ||||
| 	s, err := NewConfigurationFile() | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	err = s.ReadSection("Database", &DatabaseSettings) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	err = s.ReadSection("Server", &ServerSettings) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	err = s.ReadSection("Redis", &RedisSettings) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
		Reference in New Issue
	
	Block a user