Skip to content

Commit

Permalink
Merge pull request #1 from AndreasThinks/feature/google-deployment
Browse files Browse the repository at this point in the history
Feature/google deployment
  • Loading branch information
AndreasThinks authored Feb 6, 2025
2 parents bf3a41d + f23f144 commit 72b373c
Show file tree
Hide file tree
Showing 16 changed files with 877 additions and 369 deletions.
14 changes: 12 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Application Settings
MIN_PEERS=3
MIN_SUPERVISORS=1
MAGIC_LINK_EXPIRY_DAYS=14
MIN_REPORTS=0

MAGIC_LINK_EXPIRY_DAYS=30
FEEDBACK_QUALITIES="Communication,Leadership,Technical Skills,Teamwork,Problem Solving"

# Logging level: DEBUG, INFO, WARNING, ERROR, or CRITICAL
Expand All @@ -18,4 +20,12 @@ DATABASE_PATH=data/feedback.db

# LLM keys
GEMINI_API_KEY=your-gemini-key
SENGRIP_API_KEY=api_key
SMTP2GO_API_KEY=api-key
SMTP2GO_EMAIL_ENDPOINT=https://eu-api.smtp2go.com/v3/

STARTING_CREDITS=5
COST_PER_CREDIT_USD=3
STRIPE_SECRET_KEY=sk_test_key

DEV_MODE=True
BASE_URL=http://localhost:8080
26 changes: 26 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM python:3.12-slim-bookworm
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/

# Sync the project into a new environment, using the frozen lockfile
WORKDIR /app

COPY pyproject.toml ./

COPY uv.lock ./

RUN uv sync --frozen

# Copy the environment variables file (if exists)

# Install dependencies using uv
RUN uv sync

# Copy the rest of the application code
COPY . .

# Set environment variables for Cloud Run
ENV PYTHONUNBUFFERED=1
ENV PATH="/app/.venv/bin:$PATH"

# Run the app (uses .env automatically)
CMD ["uv", "run", "main.py"]
29 changes: 29 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.PHONY: build run stop help create-confirmed-user

# Default target when just running 'make'
.DEFAULT_GOAL := help

# Colors for help message
GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
RESET := $(shell tput -Txterm sgr0)

help: ## Show this help
@echo 'Usage:'
@echo ' ${YELLOW}make${RESET} ${GREEN}<target>${RESET}'
@echo ''
@echo 'Targets:'
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf " ${YELLOW}%-15s${RESET} %s\n", $$1, $$2}' $(MAKEFILE_LIST)

build: ## Build Docker image
docker build -t feedback2me-local .

run: ## Run Docker container with environment variables
docker run --rm --env-file .env -p 8080:8080 --name feedback2me feedback2me-local

stop: ## Stop all running containers for this project
docker ps -q --filter ancestor=feedback2me-local | xargs -r docker stop

rebuild: ## Rebuild and restart the container
$(MAKE) build
$(MAKE) run
160 changes: 117 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,130 @@
# 360 Feedback Application
# Feedback to Me

A web application for collecting peer feedback using FastHTML and SQLite.
Feedback to Me is a web application built with FastHTML, designed to collect 360° feedback and provide dynamic functionality including user registration, feedback processing, and an integrated Stripe payment flow for purchasing additional credits.

This README has been updated to reflect the latest features and to guide developers on how to run and test the application locally.

---

## Features
- Magic link authentication
- Anonymous feedback submission
- Feedback report generation
- Role-based access control

## Installation
```bash
pip install -r requirements.txt
```
- **User Registration & Confirmation:**
Users can register for the app, receive an email with a confirmation token, and then log in. When running in development mode (DEV_MODE enabled), new users are automatically confirmed.

## Configuration
1. Copy `.env.example` to `.env`:
```bash
cp .env.example .env
```
2. Generate a secure secret key:
```bash
echo "SECRET_KEY=$(openssl rand -hex 32)" >> .env
```
3. Initialize database directory:
```bash
mkdir -p data
```
- **Feedback Collection:**
The app allows users to start new feedback processes by entering emails for peers, supervisors, and reports. It manages feedback requests, submissions, and calculates summaries based on quality ratings and themes.

- **Dashboard:**
After logging in, users are presented with a dashboard that shows active feedback processes, ready items for review, completed reports, and their remaining credits.

- **Stripe Payment Integration:**
Users are allocated a start amount of credits. They can purchase additional credits via a "Buy Credits" page. The payment flow uses Stripe Checkout in test mode so that you can simulate transactions without spending real money.

- **Dynamic URL Generation with BASE_URL Override:**
A custom helper `base_uri` is implemented which uses an environment variable `BASE_URL` (e.g., an ngrok URL) so that all generated links and redirects use a secure public URL when testing locally.

---

## Setup

1. **Clone the Repository:**
Clone the project to your local machine.

2. **Environment Variables:**
Create a `.env` file in the project root and configure the following variables:

- `DEV_MODE` (set to `true` for development if you want automatic confirmation of users)
- `LOG_LEVEL` (e.g., `DEBUG` or `INFO`)
- `STRIPE_SECRET_KEY` (your Stripe test secret key)
- `BASE_URL` (set this to your public URL when using ngrok, for example: `https://abcdef.ngrok.io`)
- Stripe and SMTP2GO specific variables:
- `SMTP2GO_EMAIL_ENDPOINT`
- `SMTP2GO_API_KEY`
- Other configuration values can be set in `config.py` via environment variables such as:
- `MIN_PEERS`, `MIN_SUPERVISORS`, `MIN_REPORTS`
- `STARTING_CREDITS`
- `COST_PER_CREDIT_USD`
- `MAGIC_LINK_EXPIRY_DAYS`
- `FEEDBACK_QUALITIES`

3. **Dependencies:**
Install required packages:
```bash
pip install -r requirements.txt
```
_Note: If there is no requirements.txt file, ensure you have installed packages like `python-fasthtml`, `uvicorn`, `bcrypt`, and `stripe`._

4. **Database Setup:**
The app uses a MiniDataAPI compliant database. By default, the sample setup uses SQLite. Make sure the necessary tables are created, or check the logging for any errors on initial run.

---

## Running the Application

To run the application locally, execute:

## Running
```bash
python main.py
```

## API Endpoints
| Endpoint | Method | Description |
|-----------------------|--------|------------------------------|
| `/register` | POST | User registration |
| `/magic/link` | GET | Generate magic link |
| `/feedback/submit/{token}` | GET | Feedback form |
| `/report/{user_id}` | GET | Generate feedback report |
This will start the FastHTML server on the default port (8080). You can view the application in your browser at [http://localhost:8080](http://localhost:8080).

## Testing
```bash
pytest tests/ -v
```
---

## Deployment
Production deployment with Gunicorn:
```bash
pip install gunicorn
gunicorn main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker
```
## Testing Locally with ngrok

Since Stripe requires HTTPS for payment redirection, you can test your integration locally with ngrok:

1. **Install ngrok:**
Download ngrok from [ngrok.com](https://ngrok.com/download) and install it on your system.

2. **Start Your Local Server:**
Run your FastHTML server on port 8080:
```bash
python main.py
```

3. **Launch ngrok Tunnel:**
Open a separate terminal, and run:
```bash
ngrok http 8080
```
ngrok will generate a public HTTPS URL (e.g., `https://abcdef.ngrok.io`).

4. **Configure BASE_URL:**
In your `.env` file, set:
```
BASE_URL=https://abcdef.ngrok.io
```
This ensures that all generated URLs in your app (including those used in the payment flow) use the ngrok URL.

5. **Test Payment Flow:**
- Navigate to the ngrok URL using your browser.
- Log in and go to the "Buy Credits" page.
- Initiate a purchase using Stripe’s test card details (e.g., card number `4242 4242 4242 4242`, any future expiry date, and any CVC).
- Complete the transaction and check that the payment-success route updates your credits appropriately.

---

## Stripe Integration in Test Mode

The app uses Stripe Checkout for processing credit purchases:
- When a user submits the "Buy Credits" form, the `/create-checkout-session` route creates a Stripe Checkout session.
- The success and cancel URLs are generated using the `base_uri` helper, ensuring that if `BASE_URL` is set, they point to your ngrok URL.
- On a successful payment, Stripe redirects the user to the `/payment-success` route, where the app retrieves the session information, updates the user's credits, and shows a success message.
- Ensure you are using Stripe's test keys and test card numbers for testing.

---

## Further Development

- **Routes and Beforeware:**
The app uses FastHTML’s routing mechanism and beforeware to manage authentication and dynamic content rendering.
- **Feedback Processing:**
The feedback module supports creating new feedback processes, sending out magic link emails, and generating detailed feedback reports.

For more details, please refer to the documentation in the `docs/` directory and additional inline comments in `main.py` and `config.py`.

---

## License
MIT
Happy coding!
3 changes: 3 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
MIN_SUPERVISORS = int(os.getenv("MIN_SUPERVISORS", "1"))
MIN_REPORTS = int(os.getenv("MIN_REPORTS", "0"))

STARTING_CREDITS = int(os.getenv("STARTING_CREDITS", "5"))
COST_PER_CREDIT_USD = int(os.getenv("COST_PER_CREDIT_USD", "3"))

MAGIC_LINK_EXPIRY_DAYS = int(os.getenv("MAGIC_LINK_EXPIRY_DAYS", "30"))
# Number of reports (people who report) defaults to 0
FEEDBACK_QUALITIES = os.getenv("FEEDBACK_QUALITIES", "Communication,Leadership,Technical Skills,Teamwork,Problem Solving").split(",")
7 changes: 7 additions & 0 deletions confirmation_email_template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Hello {recipient_first_name},

Please confirm your address by clicking the link below.
{link}

Thank you,
The Feedback to Me Team
7 changes: 7 additions & 0 deletions feedback_email_template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Hello {recipient_first_name},

Your company {recipient_company} has requested your feedback. Please fill out the form at the following link:
{link}

Thank you,
The Feedback to Me Team
Loading

0 comments on commit 72b373c

Please sign in to comment.