diff --git a/README.md b/README.md index c48a5ca..9547a21 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ git clone https://github.com/uwblueprint/llsc.git cd llsc ``` -- Create a .env file in the root directory based on the .env.sample file. Update +- Create a .env file in `./backend` and `./frontend` based on the .env.sample file. Update the environment variables as needed. Consult the [Secrets](#secrets) section for detailed instructions. diff --git a/backend/Dockerfile.db b/backend/Dockerfile.db new file mode 100644 index 0000000..0dd522d --- /dev/null +++ b/backend/Dockerfile.db @@ -0,0 +1,13 @@ +FROM postgres:16-alpine + +# Set environment variables for PostgreSQL +ENV POSTGRES_USER=${POSTGRES_USER} +ENV POSTGRES_PASSWORD=${POSTGRES_PASSWORD} +ENV POSTGRES_DB=${POSTGRES_DATABASE} + +# Add a health check for the PostgreSQL server +HEALTHCHECK --interval=5s --timeout=5s --retries=5 \ + CMD pg_isready -U ${POSTGRES_USER} || exit 1 + +# Expose PostgreSQL port +EXPOSE 5432 diff --git a/backend/README.md b/backend/README.md index 4b8315e..914982a 100644 --- a/backend/README.md +++ b/backend/README.md @@ -17,6 +17,15 @@ Ensure you have the following installed on your machine: ``` - **Docker** + +- Create a `.env` file in `./backend` (the root directory, not in the backend folder) based on the .env.sample file. Update + the environment variables as needed. Consult the [Secrets](#secrets) section + for detailed instructions. + +```bash +cp .env.sample .env +``` + ## Installation Once PDM is installed, install the project dependencies by running: @@ -28,20 +37,25 @@ pdm install to install all the project dependancies listed in the `pyproject.toml` file. ## Running the Backend Locally -To start the backend locally, use the following command: - +To start up the database using docker, run the following command: ```bash -pdm run dev +cd backend +pdm run db ``` -Note: If you wish to run the backend outside of Docker (e.g., for local development), you'll need to set up a PostgreSQL database. Ensure your database configuration is set properly in the environment variables before running the project. For the time being, the recommended approach for local development using the database is to use the docker compose Postgres instance with your local dev backend. - To check if the database has been started up, type the following: ```bash - docker ps | grep llsc_db + docker ps | grep llsc_db_dev_local ``` -This checks the list of docker containers and searchs for the container name `llsc_db` +This checks the list of docker containers and searchs for the container name `llsc_db_dev_local` + +Note: If you wish to run the backend outside of Docker (e.g., for local development), you'll need to set up a PostgreSQL database. Ensure your database configuration is set properly in the environment variables before running the project. + +To start the backend locally, use the following command: +```bash +pdm run dev +``` ## Run Project Take advantage of the docker compose file in the LLSC root directory to run the backend alongside the frontend by simply running diff --git a/backend/pyproject.toml b/backend/pyproject.toml index 20833d9..5ade1fd 100644 --- a/backend/pyproject.toml +++ b/backend/pyproject.toml @@ -32,6 +32,7 @@ precommit = "pre-commit run" precommit-install = "pre-commit install" revision = "alembic revision --autogenerate" upgrade = "alembic upgrade head" +db = "bash start_db_local.sh" [tool.ruff] target-version = "py312" diff --git a/backend/start_db_local.sh b/backend/start_db_local.sh new file mode 100755 index 0000000..3344412 --- /dev/null +++ b/backend/start_db_local.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +envfile=".env" + +# Prioritize base dir .env over backend dir .env, as docker-compose should pull from there +if [ -s "../.env" ]; then + envfile="../.env" +fi + +docker build -t llsc_db_dev_local -f Dockerfile.db . +docker run -d \ + --name llsc_db_dev_local \ + --env-file $envfile \ + -p 5432:5432 \ + -v llsc_postgres_data:/var/lib/postgresql/data \ + llsc_db_dev_local