Skip to content

Commit

Permalink
improved configurations with env override support
Browse files Browse the repository at this point in the history
  • Loading branch information
alfredfrancis committed Jan 12, 2025
1 parent c9d9782 commit 4f1b045
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
8 changes: 2 additions & 6 deletions app/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
import logging
import os
from config import config

env = os.environ.get('APPLICATION_ENV', 'Development')
app_config = config[env]
from config import from_envvar
app_config = from_envvar()
55 changes: 30 additions & 25 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,46 @@
import os
import dotenv
from pydantic import BaseModel

class BaseConfig(object):
DEBUG = False
Development = False
MONGODB_HOST = "mongodb://127.0.0.1:27017/iky-ai"
dotenv.load_dotenv()

class BaseConfig(BaseModel):
DEBUG: bool = False
Development: bool = False
MONGODB_HOST: str = "mongodb://127.0.0.1:27017/iky-ai"

# Intent Classifier model details
MODELS_DIR = "model_files/"
INTENT_MODEL_NAME = "intent.model"
DEFAULT_FALLBACK_INTENT_NAME = "fallback"
DEFAULT_WELCOME_INTENT_NAME = "init_conversation"
USE_WORD_VECTORS = True
SPACY_LANG_MODEL = "en_core_web_md"
MODELS_DIR: str = "model_files/"
INTENT_MODEL_NAME: str = "intent.model"
DEFAULT_FALLBACK_INTENT_NAME: str = "fallback"
DEFAULT_WELCOME_INTENT_NAME: str = "init_conversation"
USE_WORD_VECTORS: bool = True
SPACY_LANG_MODEL: str = "en_core_web_md"

class DevelopmentConfig(BaseConfig):
DEBUG = True
Development = True
TEMPLATES_AUTO_RELOAD=True
DEBUG: bool = True
Development: bool = True
TEMPLATES_AUTO_RELOAD: bool = True

class TestingConfig(BaseConfig):
DEBUG = True
TESTING = True
DEBUG: bool = True
TESTING: bool = True

class ProductionConfig(BaseConfig):
# MongoDB Database Details
MONGODB_HOST = "mongodb://mongodb:27017/iky-ai"

class HerokuConfig(ProductionConfig):
MONGODB_HOST = os.environ.get('MONGO_URL')

class HelmConfig(ProductionConfig):
MONGODB_HOST = os.environ.get('MONGO_URL')
MONGODB_HOST: str = "mongodb://mongodb:27017/iky-ai"

config = {
'Development': DevelopmentConfig,
'Testing': TestingConfig,
'Production': ProductionConfig,
'Heroku': HerokuConfig,
'Helm': HelmConfig
}
}

def from_envvar():
"""Get configuration class from environment variable."""
choice = os.environ.get('APPLICATION_ENV', 'Development')
if choice not in config:
msg = "APPLICATION_ENV={} is not valid, must be one of {}".format(choice, set(config))
raise ValueError(msg)
loaded_config = config[choice](**os.environ)
return loaded_config
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ spec:
imagePullPolicy: {{ .Values.image.pullPolicy }}
command: ["python", "manage.py", "migrate"]
env:
- name: MONGO_URL
- name: MONGODB_HOST
value: {{.Values.mongodb.uri}}
- name: APPLICATION_ENV
value: 'Helm'
Expand Down

0 comments on commit 4f1b045

Please sign in to comment.