diff --git a/.env.example b/.env.example index f812831..6a9a918 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,4 @@ APP_ID=your_app_id_here WEBHOOK_SECRET=your_webhook_secret_here -OLLAMA_MODEL=your_chosen_llm_model_here \ No newline at end of file +OLLAMA_MODEL=your_chosen_llm_model_here +NGROK_DOMAIN=your_ngrok_domain_here diff --git a/.gitignore b/.gitignore index 279e2ea..84ccf42 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ volumes/ venv/ .env *.pem +tmp/ diff --git a/README.md b/README.md index 38d3dea..f75564a 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ Open the newly created `.env` file and update the following variables with your \* `APP_ID`: Replace `your_app_id_here` with your actual app ID. \* `WEBHOOK_SECRET`: Replace `your_webhook_secret_here` with your actual webhook secret. \* `OLLAMA_MODEL`: Replace `your_chosen_llm_model_here` with your chosen LLM model (e.g. "llama3.2"). Note: it must be an Ollama supported model (see: https://ollama.com/library for supported models) - +\* `NGROK_DOMAIN`: Replace `your_ngrok_domain_here` with your ngrok domain if you have one 4. Place the downloaded private key in the project root and name it `rsa.pem`. 5. Run the application locally @@ -77,10 +77,11 @@ Start the Flask application: The application will start running on http://localhost:4000 -### 3. Deploy the Application (ngrok instructions) +### 3. Prepare Dependencies and Deploy (ngrok and Ollama instructions) -1. We will use ngrok for its simplicity +We will use ngrok for its simplicity +**Option 1: generated public URL** In a new terminal window, start ngrok to create a secure tunnel to your local server: ```bash @@ -91,6 +92,18 @@ ngrok will generate a public URL (e.g., https://abc123.ngrok.io) Append `/webhook` to the url, e.g. https://abc123.ngrok.io -> https://abc123.ngrok.io/webhook +In another terminal window, start Ollama + +```bash +ollama run +``` +**Option 2: Using Shell Script with your own ngrok domain** + +Ensure environment variables are all set. +```bash +./run-dev.sh +``` + ### 4. Update GitHub App Settings 1. Go back to your GitHub App settings. diff --git a/requirements.txt b/requirements.txt index 3b02376..e174ef2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,3 +10,4 @@ requests~=2.32.3 ruff~=0.6.7 black~=24.4.2 ollama~=0.3.3 +pyngrok~=7.2.0 diff --git a/run-dev.sh b/run-dev.sh new file mode 100644 index 0000000..37132e9 --- /dev/null +++ b/run-dev.sh @@ -0,0 +1,110 @@ +#!/bin/bash +set -E +set -m + +# Array to store subshell PIDs +declare -a subshell_pids=() + +# Log file for debugging +LOG_FILE="tmp/run-dev.log" + +# Function to log messages +log_message() { + local message="$1" + echo "[$(date '+%Y-%m-%d %H:%M:%S')] $message" | tee -a "$LOG_FILE" +} + +# Function to check if a process is running +is_process_running() { + local pid=$1 + if kill -0 "$pid"; then + return 0 + else + return 1 + fi +} + +# Define a function to handle cleanup +cleanup() { + log_message "Current Processes:" + eval ps >> $LOG_FILE + + log_message "Initiating cleanup..." + + # Kill all registered subshell processes by group process ID + for pid in "${subshell_pids[@]}"; do + if is_process_running "$pid"; then + log_message "Terminating process $pid" + kill -- -"$pid" >>$LOG_FILE 2>&1 + wait -- -"$pid" >/dev/null 2>&1 + fi + done + + log_message "Cleanup complete" + exit 0 +} + +trap cleanup SIGINT SIGTERM EXIT + +# Function to start a process and register its PID +start_process() { + local process_name="$1" + local command="$2" + + # Start the process in a subshell + ( + eval "$command" + ) > /dev/null 2>&1 & + + local pid=$! + + # Wait briefly to check if the process started successfully + sleep 1 + if is_process_running "$pid"; then + subshell_pids+=("$pid") + log_message "Started $process_name (PID: $pid)" + return 0 + else + log_message "Failed to start $process_name" + return 1 + fi +} + +# Main process +main() { + # clear log file + : > "$LOG_FILE" + + log_message "Starting run-dev.sh..." + + # Source environment variables + if [[ -f .env ]]; then + source .env + else + log_message "Warning: .env file not found" + fi + + # Start Ollama if configured + if [[ -n "${OLLAMA_MODEL:-}" ]]; then + start_process "Ollama" "ollama run '${OLLAMA_MODEL}'" || \ + log_message "Failed to start Ollama" + fi + + # Start Ngrok if configured. Using Port 4000 to match app.py + if [[ -n "${NGROK_DOMAIN:-}" ]]; then + start_process "Ngrok" "ngrok http 4000 --url '${NGROK_DOMAIN}'" || \ + log_message "Failed to start Ngrok" + fi + + # Wait for exit command. NOTE: If stuck in console, close the terminal + log_message "All processes started. Type 'exit' or CTRL+C to quit" + while read -r -p "> " input; do + if [[ "${input,,}" == "exit" ]]; then + log_message "Exit command received" + break + fi + done +} + +# Run main function +main