An Interest In:
Web News this Week
- April 28, 2024
- April 27, 2024
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
April 1, 2023 05:34 pm GMT
Original Link: https://dev.to/iamibi/how-to-handle-different-application-environments-like-prod-dev-test-etc-3h68
How to handle different application environments like Prod, Dev, Test, etc.?
Introduction
There are times when we are in a dilemma as to how to properly use application environments. It gets confusing really fast. I will try to explain the approach (that I see) makes sense.
I will be using Python in this blog, but this implementation is agnostic of the programming language.
Pre-requisites
- Python 3.8.x or higher
- PyYaml library - To read our YAML configuration files
Lets get started!
- Create a
yaml
file just like below
# settings.ymldefault: &default api_url: "https://example.com/dev" aws: &aws s3_bucket: - my-bucketdev: &dev <<: *defaulttest: &test <<: *default api_url: "https://example.com/test"prod: &prod <<: *default api_url: "https://prod.example.com/" aws: <<: *aws s3_bucket: - my-prod-bucket
In the above yaml, we are using the inheritance to override the configurations for individual environments.
- To pass the correct configuration, we can leverage
argparse
which parses command-line arguments for us. The following python script does that.
# main.pyimport argparsedef read_cli_arguments(): arg_parser = argparse.ArgumentParser() arg_parser.add_argument( "--app-env", required=True, action="store", choices=("prod", "dev", "test"), help="Application environment to run in" ) return arg_parser.parse_args()
- Next, we read the configuration file based on the environment
# main.pyfrom typing import Dictimport yamldef read_settings(app_env: str) -> Dict: try: with open("settings.yml", mode="r", encoding="utf-8") as config: configs: Dict = yaml.safe_load(config) except yaml.YAMLError as yaml_err: print(f"Error occurred while reading the file. Error: {yaml_err}") raise return configs[app_env]
- Reading the configuration dictionary is now as easy as
# main.pysettings_prod: Dict = read_settings("prod")print(f"Prod Configurations: {settings_prod}")settings_test: Dict = read_settings("test")print(f"Test Configurations: {settings_test}")
- Finally, we use this as part of running our
main.py
file from the command-line like this
$ lsmain.py settings.yml$ python3 main.py --app-env prod
Complete Code
# main.pyfrom typing import Dictimport argparseimport yamldef read_cli_arguments(): arg_parser = argparse.ArgumentParser() arg_parser.add_argument( "--app-env", required=True, action="store", choices=("prod", "dev", "test"), help="Application environment to run in" ) return arg_parser.parse_args()def read_settings(app_env: str) -> Dict: try: with open("settings.yml", mode="r", encoding="utf-8") as config: configs: Dict = yaml.safe_load(config) except yaml.YAMLError as yaml_err: print(f"Error occurred while reading the file. Error: {yaml_err}") raise return configs[app_env]# Program Execution Starts Hereif __name__ == "__main__": # Read CLI arguments args = read_cli_arguments() # Retrieve specific configurations settings: Dict = read_settings(app_env=args.app_env) print(f"Configs: {settings}")
Executing the above code on the terminal
$ python3 main.py --app-env prodConfigs: {"api_url": "https://prod.example.com/", "aws": {"s3_bucket": ["my-prod-bucket"]}}
Cheers!
Original Link: https://dev.to/iamibi/how-to-handle-different-application-environments-like-prod-dev-test-etc-3h68
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To