diff --git a/.gitignore b/.gitignore index b7c4c7b..0919105 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ logs/ __pycache__/ *.pyc .pytest_cache/ -backups/ \ No newline at end of file +backups/ +.env \ No newline at end of file diff --git a/README.md b/README.md index 0ab1c6e..e3f975d 100644 --- a/README.md +++ b/README.md @@ -87,16 +87,30 @@ This template utilizes PostgreSQL as the database backend. 2. **Create and activate the conda environment**: ```sh + # Windows conda env create -f environment.yml + # Mac + conda env create -f environment_mac.yml conda activate justin_rl_api ``` 3. **Configure the database**: - Setup a PostgreSQL database and create a new database for the application. - Update the database connection string in ```config.py```. For PostgreSQL, the string looks like: + Setup a PostgreSQL database ([download](https://www.postgresql.org/download/ )) and create a new database for the application. + Create a database ```sh - SQLALCHEMY_DATABASE_URI = "postgresql://:@:/" + # in shell + psql -U postgres + + # in psql + CREATE DATABASE your_db_name; + + ``` + + Create a file ".env" at the root folder of your project and add the database connection string. For PostgreSQL, the string looks like: + + ```sh + SQLALCHEMY_DATABASE_URI="postgresql://:@:/" ``` 4. **Initialize the database**: diff --git a/config.py b/config.py index d4ecd94..ab1a2e6 100644 --- a/config.py +++ b/config.py @@ -1,5 +1,7 @@ import os +from dotenv import load_dotenv +load_dotenv() class Config: # General Configuration @@ -25,7 +27,7 @@ class Config: # "DATABASE_URL", "postgresql://myuser:mypassword@localhost:5432/mydatabase" # ) SQLALCHEMY_TRACK_MODIFICATIONS = False - SQLALCHEMY_DATABASE_URI = "postgresql://zipingxu@localhost:5432/justin_rl_db" + SQLALCHEMY_DATABASE_URI = os.getenv('SQLALCHEMY_DATABASE_URI') or "postgresql://zipingxu@localhost:5432/justin_rl_db" SQLALCHEMY_ENGINE_OPTIONS = { "pool_recycle": 3600, # Recycle connections after 3600 seconds (1 hour) } diff --git a/environment.yml b/environment.yml index 916a8a8..6d7fcec 100644 --- a/environment.yml +++ b/environment.yml @@ -18,6 +18,7 @@ dependencies: - pip=24.2 - psycopg2=2.9.9 - python=3.11.11 + - python-dotenv=1.1.0 - setuptools=75.1.0 - sqlalchemy=2.0.34 - sqlite=3.45.3 diff --git a/environment_mac.yml b/environment_mac.yml index 7363c2a..aeb85ed 100644 --- a/environment_mac.yml +++ b/environment_mac.yml @@ -18,6 +18,7 @@ dependencies: - pip=24.2 - psycopg2=2.9.9 - python=3.11.11 + - python-dotenv=1.1.0 - setuptools=75.1.0 - sqlalchemy=2.0.34 - sqlite=3.45.3