jarodtaylor / chatterly

0 stars 0 forks source link

Setting Up Local Development (Without Docker)

[!TIP] If you would prefer to run your local development in a Docker container, jump to the Docker Setup section

Prerequisites


1. Install pyenv for Python Version Management

We use pyenv to manage different versions of Python across machines and projects.

  1. Install pyenv via Homebrew:

    brew install pyenv
  2. Add pyenv to your shell configuration file (.zshrc or .bash_profile):

    echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc  # For zsh shell
    echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
    echo 'eval "$(pyenv init --path)"' >> ~/.zshrc
    echo 'eval "$(pyenv init -)"' >> ~/.zshrc
  3. Apply the changes by restarting your shell or running:

    source ~/.zshrc

2. Install the Required Python Version

  1. Install the version of Python used in this project (e.g., 3.12.0):

    pyenv install 3.12.0
  2. Set the Python version for the project locally:

    pyenv local 3.12.0

    This creates a .python-version file in your project that automatically uses Python 3.12.0 for this project.

  3. (Optional) Set Python 3.12.0 as the global version if you want it for all projects:

    pyenv global 3.12.0

3. Create and Activate a Virtual Environment

  1. Create a virtual environment named venv (or choose another name):

    python -m venv venv
  2. Activate the virtual environment:

    • macOS/Linux:
      source venv/bin/activate
    • Windows:
      venv\Scripts\activate

    After activation, you should see (venv) in your terminal prompt.


4. Install Project Dependencies

  1. Install the project’s dependencies from the requirements.txt file:

    pip install -r requirements.txt
  2. Verify the installation: Run the following command to see the installed dependencies:

    pip freeze

5. Verify Python Installation

  1. Check that Python 3.12.0 is being used in this project:

    python --version

    You should see Python 3.12.0 (or the version you installed).

  2. Run the Flask application (if you'd like to test the setup):

    flask run

6. Local PostgreSQL Database Setup

To ensure consistency across different machines, we will use a generic chatterly_dev PostgreSQL user for local development.

1. Create the PostgreSQL User and Database

  1. Access PostgreSQL (if it's installed locally):

    psql postgres
  2. Create the chatterly_dev user with a password:

    CREATE USER chatterly_dev WITH PASSWORD 'chatterly_dev_password';
  3. Create the chatterly_db database:

    CREATE DATABASE chatterly_db;
  4. Grant privileges on the database to the chatterly_dev user:

    GRANT ALL PRIVILEGES ON DATABASE chatterly_db TO chatterly_dev;

2. Update Your .env File

Ensure your .env file contains the following environment variables for local development:

DB_USER=chatterly_dev
DB_PASSWORD=chatterly_dev_password
DB_HOST=localhost
DB_PORT=5432
DB_NAME=chatterly_db

This setup will allow you to work with PostgreSQL in local development.

3. Future Automation (Optional)

Later, we will add a Flask CLI command to automate the database setup using:

flask db-setup

For now, follow the manual steps above.

Docker Setup

For those who prefer to run the app using Docker, follow the steps below.

1. Prerequisites

Make sure you have Docker installed. You can download it from Docker's official website.


2. Running the App with Docker

  1. Build and run the containers:

    docker-compose up --build

    This will:

    • Build the Docker image for the app.
    • Set up the PostgreSQL database inside a Docker container.
    • Start the Flask app on port 8000.
  2. Access the app in your browser:


3. Stopping the Containers

To stop the containers, press CTRL + C in the terminal where Docker is running.

Alternatively, you can stop the containers with:

docker-compose down

This command will stop and remove the containers.


4. Docker Ports