first
This commit is contained in:
9
config/cache.go
Normal file
9
config/cache.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package config
|
||||
|
||||
type Cache struct {
|
||||
Type string
|
||||
RedisAddr string `mapstructure:"redis-addr"`
|
||||
RedisPwd string `mapstructure:"redis-pwd"`
|
||||
RedisDb int `mapstructure:"redis-db"`
|
||||
FileDir string `mapstructure:"file-dir"`
|
||||
}
|
||||
56
config/config.go
Normal file
56
config/config.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
const (
|
||||
DebugMode = "debug"
|
||||
ReleaseMode = "release"
|
||||
DefaultConfig = "conf/config.yaml"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Gorm Gorm
|
||||
Mysql Mysql
|
||||
Gin Gin
|
||||
Logger Logger
|
||||
Redis Redis
|
||||
Cache Cache
|
||||
Oss Oss
|
||||
Jwt Jwt
|
||||
Rustdesk Rustdesk
|
||||
}
|
||||
|
||||
// Init 初始化配置
|
||||
func Init(rowVal interface{}, cb func()) *viper.Viper {
|
||||
var config string
|
||||
flag.StringVar(&config, "c", "", "choose config file.")
|
||||
flag.Parse()
|
||||
if config == "" { // 优先级: 命令行 > 默认值
|
||||
config = DefaultConfig
|
||||
}
|
||||
v := viper.New()
|
||||
v.SetConfigFile(config)
|
||||
v.SetConfigType("yaml")
|
||||
err := v.ReadInConfig()
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("Fatal error config file: %s \n", err))
|
||||
}
|
||||
v.WatchConfig()
|
||||
v.OnConfigChange(func(e fsnotify.Event) {
|
||||
//配置文件修改监听
|
||||
fmt.Println("config file changed:", e.Name)
|
||||
if err2 := v.Unmarshal(rowVal); err2 != nil {
|
||||
fmt.Println(err2)
|
||||
}
|
||||
cb()
|
||||
})
|
||||
if err := v.Unmarshal(rowVal); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
8
config/gin.go
Normal file
8
config/gin.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package config
|
||||
|
||||
type Gin struct {
|
||||
ApiAddr string `mapstructure:"api-addr"`
|
||||
AdminAddr string `mapstructure:"admin-addr"`
|
||||
Mode string
|
||||
ResourcesPath string `mapstructure:"resources-path"`
|
||||
}
|
||||
19
config/gorm.go
Normal file
19
config/gorm.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package config
|
||||
|
||||
const (
|
||||
TypeSqlite = "sqlite"
|
||||
TypeMysql = "mysql"
|
||||
)
|
||||
|
||||
type Gorm struct {
|
||||
Type string `mapstructure:"type"`
|
||||
MaxIdleConns int `mapstructure:"max-idle-conns"`
|
||||
MaxOpenConns int `mapstructure:"max-open-conns"`
|
||||
}
|
||||
|
||||
type Mysql struct {
|
||||
Addr string `mapstructure:"addr"`
|
||||
Username string `mapstructure:"username"`
|
||||
Password string `mapstructure:"password"`
|
||||
Dbname string `mapstructure:"dbname"`
|
||||
}
|
||||
8
config/jwt.go
Normal file
8
config/jwt.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package config
|
||||
|
||||
import "time"
|
||||
|
||||
type Jwt struct {
|
||||
PrivateKey string `mapstructure:"private-key"`
|
||||
ExpireDuration time.Duration `mapstructure:"expire-duration"`
|
||||
}
|
||||
7
config/logger.go
Normal file
7
config/logger.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package config
|
||||
|
||||
type Logger struct {
|
||||
Path string
|
||||
Level string
|
||||
ReportCaller bool `mapstructure:"report-caller"`
|
||||
}
|
||||
10
config/oss.go
Normal file
10
config/oss.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package config
|
||||
|
||||
type Oss struct {
|
||||
AccessKeyId string `mapstructure:"access-key-id"`
|
||||
AccessKeySecret string `mapstructure:"access-key-secret"`
|
||||
Host string `mapstructure:"host"`
|
||||
CallbackUrl string `mapstructure:"callback-url"`
|
||||
ExpireTime int64 `mapstructure:"expire-time"`
|
||||
MaxByte int64 `mapstructure:"max-byte"`
|
||||
}
|
||||
7
config/redis.go
Normal file
7
config/redis.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package config
|
||||
|
||||
type Redis struct {
|
||||
Addr string
|
||||
Password string
|
||||
Db int
|
||||
}
|
||||
8
config/rustdesk.go
Normal file
8
config/rustdesk.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package config
|
||||
|
||||
type Rustdesk struct {
|
||||
IdServer string `mapstructure:"id-server"`
|
||||
RelayServer string `mapstructure:"relay-server"`
|
||||
ApiServer string `mapstructure:"api-server"`
|
||||
Key string `mapstructure:"key"`
|
||||
}
|
||||
Reference in New Issue
Block a user