__init__.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # vim:fenc=utf-8
  4. import json
  5. import os
  6. import yaml
  7. import pyapollos
  8. APOLLO_PROJECT_NAME = 'ai-agent'
  9. _config_cache = None
  10. def get():
  11. global _config_cache
  12. if _config_cache is None:
  13. dir_name = os.path.dirname(os.path.abspath(__file__))
  14. env = os.environ.get('AI_AGENT_ENV', 'dev')
  15. if env not in ('dev', 'pre', 'prod'):
  16. raise ValueError(f"Invalid environment: {env}. Expected one of ('dev', 'pre', 'prod').")
  17. with open(f'{dir_name}/{env}.yaml', 'r') as f:
  18. _config_cache = yaml.safe_load(f.read())
  19. return _config_cache
  20. def get_env():
  21. env = os.environ.get('AI_AGENT_ENV', 'dev')
  22. return env
  23. class ApolloConfig(object):
  24. def __init__(self, env):
  25. match env:
  26. case "dev":
  27. self.apollo_connection = pyapollos.ApolloClient(
  28. app_id=APOLLO_PROJECT_NAME,
  29. config_server_url="http://devapolloconfig-internal.piaoquantv.com/",
  30. timeout=10
  31. )
  32. case "pre":
  33. self.apollo_connection = pyapollos.ApolloClient(
  34. app_id=APOLLO_PROJECT_NAME,
  35. config_server_url="http://preapolloconfig-internal.piaoquantv.com/",
  36. timeout=10
  37. )
  38. case "prod":
  39. self.apollo_connection = pyapollos.ApolloClient(
  40. app_id=APOLLO_PROJECT_NAME,
  41. config_server_url="https://apolloconfig-internal.piaoquantv.com/",
  42. timeout=10
  43. )
  44. def get_value(self, key, default=None):
  45. response = self.apollo_connection.get_value(key, default)
  46. return response
  47. def get_json_value(self, key, default=None):
  48. value = self.get_value(key)
  49. if not value:
  50. return default
  51. else:
  52. return json.loads(value)
  53. apollo_config = ApolloConfig(get_env())