configworker/config.go

56 lines
1.0 KiB
Go
Raw Permalink Normal View History

2020-01-02 18:12:31 +00:00
package main
import (
"log"
"os"
"sync"
"gopkg.in/yaml.v2"
)
// GlobalConfig 初始化后的全局配置
var GlobalConfig *Config = loadConfig()
// User 用户结构
// type User struct {
// // Name 用户名
// Name string `yaml:"name"`
// // Password 密码
// Password string `yaml:"pwd"`
// }
// Config 配置结构
type Config struct {
// Users 账号密码集
2020-01-05 18:38:55 +00:00
Users map[string]string `yaml:"users"`
Paths []string `yaml:"paths"`
Permit map[string][]string `yaml:"permit"`
2020-01-02 18:12:31 +00:00
configLock *sync.Mutex
}
// GetUser Get return user map[string]string
func (config *Config) GetUser(key string) (string, bool) {
config.configLock.Lock()
value, ok := config.Users[key]
config.configLock.Unlock()
return value, ok
}
func loadConfig() *Config {
f, err := os.Open("config.yaml")
if err != nil {
log.Panic(err)
}
config := &Config{}
config.configLock = &sync.Mutex{}
dec := yaml.NewDecoder(f)
if err = dec.Decode(config); err != nil {
log.Panic(err)
}
return config
// log.Panic(spew.Sdump(config))
}