# 从.yaml文件中加载配置

# 简单使用-从.yaml文件中加载配置并返回一个对应的字典

# !/usr/bin/python3
# -*- coding: utf-8 -*-
import logging
import os

import yaml

class Config:
    _instance = None

    def __new__(cls):
        if cls._instance is None:
            cls._instance = self = super().__new__(cls)
        else:
            return cls._instance
        cur_path = os.path.dirname(os.path.abspath(__file__))
        env = os.getenv('ENV', 'dev')
        with open(f"{cur_path}/../common/config/{env}/config.yaml", "r", encoding="UTF-8") as f_config:
            try:
                # 读取配置文件
                _config = yaml.load(f_config, Loader=yaml.SafeLoader)
                self._config = _config
            except Exception as exp:
                #logging.error("parse config.yaml failed, err: {}".format(exp))
                exit(-1)

        return cls._instance

    def server_config(self):
        """
        :return: 服务的配置数据
        """
        return self._config


CONFIG = Config().server_config()


if __name__ == "__main__":
    print(CONFIG)
    print(type(CONFIG))

SERVER:
  host: 0.0.0.0
  port: 8080

REDIS:
  host: 192.168.50.27
  port: 6379
  db: 1
  password: Dw@123654

MYSQL:
  host: 192.168.50.26
  port: 3306
  user: app_store
  password: Appstore@sdfz@2022
  db: app_store

{'SERVER': {'host': '0.0.0.0', 'port': 8080}, 'REDIS': {'host': '192.168.50.27', 'port': 6379, 'db': 1, 'password': 'Dw@123654'}, 'MYSQL': {'host': '192.168.50.26', 'port': 3306, 'user': 'app_store', 'password': 'Appstore@sdfz@2022', 'db': 'app_store'}}
<class 'dict'>