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
53 changes: 53 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# .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:
# Run the linter.
- id: ruff
args: [--fix]
# Run the formatter.
- id: ruff-format
args: []

# - repo: https://github.com/gitleaks/gitleaks
# rev: v8.19.0
# hooks:
# - id: gitleaks

# - repo: https://github.com/pycqa/isort
Mayank808 marked this conversation as resolved.
Show resolved Hide resolved
# rev: 5.11.2
# hooks:
# - id: isort
# name: isort (python)

# - repo: local
# hooks:
# - id: docker-compose-check
# exclude: '(^|/)(frontend|hooks|.github)/'
# name: Docker Compose up check
# entry: docker-compose
# args: ['up', '--build', '.', '--exit-code-from', 'backend']
# language: system
73 changes: 73 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,74 @@
# 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.

## 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

Once PDM is installed, install the project dependencies by running:

```bash
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:

```bash
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.
Mayank808 marked this conversation as resolved.
Show resolved Hide resolved
Mayank808 marked this conversation as resolved.
Show resolved Hide resolved

## 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.

## Environment Variables
Mayank808 marked this conversation as resolved.
Show resolved Hide resolved
Mayank808 marked this conversation as resolved.
Show resolved Hide resolved
28 changes: 16 additions & 12 deletions setup.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
#!/bin/bash

vault_path_replacement_str="s|^vault_path=.*|vault_path=\"$1\"|g"
default_branch_replacement_str="s|^default_branch=.*|default_branch=\"$2\"|g"

# MacOS
if [[ $OSTYPE =~ darwin.* ]]; then
sed -i "" -e $vault_path_replacement_str ./hooks/post-merge
sed -i "" -e $default_branch_replacement_str ./hooks/post-merge
else
sed -i $vault_path_replacement_str ./hooks/post-merge
sed -i $default_branch_replacement_str ./hooks/post-merge
fi
cp ./hooks/post-merge ./.git/hooks/post-merge
# vault_path_replacement_str="s|^vault_path=.*|vault_path=\"$1\"|g"
# default_branch_replacement_str="s|^default_branch=.*|default_branch=\"$2\"|g"

# # MacOS
# if [[ $OSTYPE =~ darwin.* ]]; then
# sed -i "" -e $vault_path_replacement_str ./hooks/post-merge
# sed -i "" -e $default_branch_replacement_str ./hooks/post-merge
# else
# sed -i $vault_path_replacement_str ./hooks/post-merge
# sed -i $default_branch_replacement_str ./hooks/post-merge
# fi
# cp ./hooks/post-merge ./.git/hooks/post-merge

pip install pre-commit
Mayank808 marked this conversation as resolved.
Show resolved Hide resolved

pre-commit install
76 changes: 45 additions & 31 deletions update_secret_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,67 @@
# Open secret.config file
configFileNotFound = False
try:
configFile = open('secret.config')
Mayank808 marked this conversation as resolved.
Show resolved Hide resolved
configFile = open("secret.config")
except Exception as e:
print("File secret.config could not be opened in current directory.")
print(e)
configFileNotFound = True
# Script will exit after checking if the Vault request is valid
print("File secret.config could not be opened in current directory.")
print(e)
configFileNotFound = True
# Script will exit after checking if the Vault request is valid

# Decode json result
try:
rawInput = ''.join(sys.stdin.readlines())
decodedJson = json.loads(rawInput)
except Exception as e:
print("Unable to retrieve secrets from Vault and obtain valid json result.")
print("Please ensure you are authenticated and have supplied the correct path argument.")
exit()
rawInput = "".join(sys.stdin.readlines())
decodedJson = json.loads(rawInput)
except Exception:
print("Unable to retrieve secrets from Vault and obtain valid json result.")
print(
"Please ensure you are authenticated and have supplied the correct path argument."
)
exit()

# Extract the data field containting the secrets
if "data" in decodedJson and "data" in decodedJson["data"]:
data = decodedJson["data"]["data"]
data = decodedJson["data"]["data"]
else:
print("Unable to access the field data:{data:{}} from result which should contain the secrets.")
print("Please ensure you are authenticated and have supplied the correct path argument.")
exit()
print(
"Unable to access the field data:{data:{}} from result which should contain the secrets."
)
print(
"Please ensure you are authenticated and have supplied the correct path argument."
)
exit()

# Even if the config file is not found, it is useful to still indicate if the Vault request has any problems before exiting
if configFileNotFound:
exit()
exit()

# Read all the secret file locations from secret.config
locations = {}
for line in configFile:
key, val = line.rstrip().partition('=')[::2]
if key in locations:
print("Key <{keyName}> appeared more than once on configuration file. Ignoring second instance of the key.".format(keyName=key))
else:
locations[key] = val
key, val = line.rstrip().partition("=")[::2]
if key in locations:
print(
"Key <{keyName}> appeared more than once on configuration file. Ignoring second instance of the key.".format(
keyName=key
)
)
else:
locations[key] = val
configFile.close()

# Write values to the secret file corresponding to their keys
for key in data:
if key in locations:
try:
f = open(locations[key], 'w')
f.write(data[key])
f.close()
except Exception as e:
print("Could not write the values for key <{keyName}> to location <{locName}>".format(keyName=key, locName=locations[key]))
print(e)
else:
print("File location for key <{keyName}> was not found.".format(keyName=key))
if key in locations:
try:
f = open(locations[key], "w")
f.write(data[key])
f.close()
except Exception as e:
print(
"Could not write the values for key <{keyName}> to location <{locName}>".format(
keyName=key, locName=locations[key]
)
)
print(e)
else:
print("File location for key <{keyName}> was not found.".format(keyName=key))