Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LLSC-29] Add basic readme and pre-commit #3

Merged
merged 10 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions backend/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# .pre-commit-config.yaml

# Other usable hooks https://pre-commit.com/hooks.html

default_language_version:
python: python3.12

default_stages: [pre-commit, pre-push]

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-added-large-files
- id: check-merge-conflict
- id: check-json
- id: check-toml
- id: debug-statements
- id: detect-private-key
- id: pretty-format-json
args: [--autofix]

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.6.7
hooks:
- id: ruff
args: [--fix]
- id: ruff-format

- repo: https://github.com/gitleaks/gitleaks
rev: v8.18.0
hooks:
- id: gitleaks
89 changes: 76 additions & 13 deletions backend/README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,97 @@
# llsc-backend
This repository contains the backend for the Leukemia & Lymphoma Society of Canada (LLSC) application. Below is a guide to help you set up the environment, install dependencies using PDM, and run the backend using Docker.

## Setup (mirrors [base README](../README.md#setup))
- Install pdm (this is a global installation, so location doesn't matter)
On macOS:
```bash
brew install pdm
```
Otherwise, feel free to follow install instructions [here](https://pdm-project.org/latest/#installation)
## Prerequisites

Ensure you have the following installed on your machine:

- **Python 3.12+**
- **[PDM](https://pdm.fming.dev/latest/#installation)** (Python Dependency Manager)
- Install PDM using:
```bash
pip install pdm
```
Alternatively, if you're using Homebrew:
```bash
brew install pdm
```
- **Docker**

## Installation

You will then need to go into each directory individually to install dependencies.
Once PDM is installed, install the project dependencies by running:

FastAPI backend
```bash
cd backend
pdm install
```

To run the backend server locally (recommended for development), run the following command:
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:

```bash
cd backend
pdm run dev
```

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
```
This checks the list of docker containers and searchs for the container name `llsc_db`

## Run Project

Take advantage of the docker compose file in the LLSC root directory to run the backend alongside the frontend by simply running

```bash
docker-compose up --build
```

<!--
## Setup Docker Image For Backend

Ensure Docker is installed on your machine. To build the Docker image, run:

```bash
docker build -t <image-name> .
```

Replace <image-name> with a name of your choice.

### Running the Docker Container

To run the image, execute the following command:

```bash
docker run -p 8080:8080 <image-name>

# Add Flags if needed
-d # Runs container in background
```

You can adjust the ports as needed. For example, 8080:8080 maps the container’s port 8080 to your local machine's port 8080. Once running, you should be able to access the backend locally via: -->

The backend runs at http://localhost:8080 and the frontend runs at http://localhost:3000.

## Formatting and Linting (mirrors [formatting in base README](../README.md#formatting-and-linting))

### Pre-commit Hook

We have added the pre-commit hook package and defined the config file in `backend/.pre-commit-config.yaml`. This should automatically get installed when you run `pdm install` and should work whenever you run any `git commit` or `git push` commands in the repo.

You can also manually run the pre-commit hooks prior to pushing/commiting code by running the following:

```
pdm run precommit
```

If the above command doesn't work please run `pdm run precommit-install` prior to running above.

Note after the pre-commit hooks run you may need to stage the changed files again. Please look over the changes before you push the code again.

### Ruff

We use Ruff for code linting and formatting in the backend. To check for and automatically fix linting issues:
Expand All @@ -46,6 +107,9 @@ cd backend
pdm run ruff format .
```

## Environment Variables
Mayank808 marked this conversation as resolved.
Show resolved Hide resolved
Mayank808 marked this conversation as resolved.
Show resolved Hide resolved
Environment variables are currently stored in an .env file within the base repository (not the backend folder). You will need to copy the local environment variables stored in the following notion [page](https://www.notion.so/uwblueprintexecs/Environment-Variables-11910f3fb1dc80e4bc67d35c3d65d073?pvs=4) to get the database working.


## Adding a new model
When adding a new model, make sure to add it to `app/models/__init__.py` so that the migration script can pick it up when autogenerating the new migration.
Expand All @@ -60,7 +124,6 @@ __all__ = ["Base", ... , "<new_model_name>"]
```
Then run the steps found in the [Migrations](#migrations) section to create a new migration.


## Migrations

We use Alembic for database schema migrations. We mainly use migration scripts to keep track of the incremental and in theory revertible changes that have occurred on the database. To create a new migration, run the following command after adding or editing models in `backend/app/models.py`:
Expand Down
91 changes: 90 additions & 1 deletion backend/pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies = [
"firebase-admin>=6.5.0",
"pytest>=8.3.3",
"inflection>=0.5.1",
"pre-commit>=4.0.0",
"psycopg2>=2.9.9",
]
requires-python = "==3.12.*"
Expand All @@ -27,6 +28,8 @@ distribution = false

[tool.pdm.scripts]
dev = "fastapi dev app/server.py"
precommit = "pre-commit run"
precommit-install = "pre-commit install"
revision = "alembic revision --autogenerate"
upgrade = "alembic upgrade head"

Expand Down
Loading