package config

import (
	"gopkg.in/yaml.v2"
	"os"
	"sync"
)

var (
	instance *config
	once     sync.Once
)

func init() {
	once.Do(func() {
		var conf config
		filePath := "./edge/config/config.yaml"

		if f, err := os.Open(filePath); err != nil {
			panic(err)
		} else {
			err := yaml.NewDecoder(f).Decode(&conf)
			if err != nil {
				panic(err)
			}
		}
		instance = &conf
	})
}

// 获取配置文档实例
func Instance() *config {
	return instance
}

type config struct {
	Logger logger `yaml:"logger"`
	Mqtt   mqtt   `yaml:"mqtt"`
}

type logger struct {
	Path  string `yaml:"path"`
	Name  string `yaml:"name"`
	Level string `yaml:"level"`
}

type mqtt struct {
	Server   string `yaml:"server"`
	Id       string `yaml:"id"`
	User     string `yaml:"user"`
	Password string `yaml:"password"`
}
