diff --git a/.github/workflows/deploy_emoji-caller.yml b/.github/workflows/deploy_emoji-caller.yml new file mode 100644 index 0000000..d6c7900 --- /dev/null +++ b/.github/workflows/deploy_emoji-caller.yml @@ -0,0 +1,78 @@ +# Docs for the Azure Web Apps Deploy action: https://github.com/Azure/webapps-deploy +# More GitHub Actions for Azure: https://github.com/Azure/actions +# More info on Python, GitHub Actions, and Azure App Service: https://aka.ms/python-webapps-actions + +name: Build and deploy Python app to Azure Web App - emoji-caller + +on: + push: + branches: + - deploy + workflow_dispatch: + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Set up Python version + uses: actions/setup-python@v5 + with: + python-version: '3.12' + + - name: Create and start virtual environment + run: | + python -m venv venv + source venv/bin/activate + + - name: Install dependencies + run: pip install -r requirements.txt + + # Optional: Add step to run tests here (PyTest, Django test suites, etc.) + + - name: Zip artifact for deployment + run: zip release.zip ./* -r + + - name: Upload artifact for deployment jobs + uses: actions/upload-artifact@v4 + with: + name: python-app + path: | + release.zip + !venv/ + + deploy: + runs-on: ubuntu-latest + needs: build + environment: + name: 'Production' + url: ${{ steps.deploy-to-webapp.outputs.webapp-url }} + permissions: + id-token: write #This is required for requesting the JWT + + steps: + - name: Download artifact from build job + uses: actions/download-artifact@v4 + with: + name: python-app + + - name: Unzip artifact for deployment + run: unzip release.zip + + + - name: Login to Azure + uses: azure/login@v2 + with: + client-id: ${{ secrets.AZUREAPPSERVICE_CLIENTID_971F3F46F7FC456E8D4597FC800C658C }} + tenant-id: ${{ secrets.AZUREAPPSERVICE_TENANTID_0E984A4F42AF487BA0D671B798A6E912 }} + subscription-id: ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID_CFF2F24C3CF64DC9B73A22E137C47DD7 }} + + - name: 'Deploy to Azure Web App' + uses: azure/webapps-deploy@v3 + id: deploy-to-webapp + with: + app-name: 'emoji-caller' + slot-name: 'Production' + \ No newline at end of file diff --git a/app.py b/app.py index 1a5f9a2..0011b45 100644 --- a/app.py +++ b/app.py @@ -1,12 +1,23 @@ from flask import Flask, render_template, jsonify import sqlite3 from datetime import datetime +import os + app = Flask(__name__) +# Use Azure's writable directory if available, otherwise use local path +if 'WEBSITE_SITE_NAME' in os.environ: + DB_PATH = os.path.join('/home/data', 'emoji_stats.db') +else: + DB_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'emoji_stats.db') + +# Ensure the directory exists +os.makedirs(os.path.dirname(DB_PATH), exist_ok=True) + # Database initialization def init_db(): - conn = sqlite3.connect('emoji_stats.db') + conn = sqlite3.connect(DB_PATH) c = conn.cursor() # Create table for emoji usage counts c.execute(''' @@ -34,7 +45,7 @@ def init_db(): conn.close() def get_emoji_counts(): - conn = sqlite3.connect('emoji_stats.db') + conn = sqlite3.connect(DB_PATH) c = conn.cursor() c.execute('SELECT emoji_name, copy_count + api_count as total_count FROM emoji_counts') counts = {row[0]: row[1] for row in c.fetchall()} @@ -42,7 +53,7 @@ def get_emoji_counts(): return counts def increment_count(emoji_name, usage_type): - conn = sqlite3.connect('emoji_stats.db') + conn = sqlite3.connect(DB_PATH) c = conn.cursor() if usage_type == 'copy': c.execute('UPDATE emoji_counts SET copy_count = copy_count + 1, last_used = ? WHERE emoji_name = ?', @@ -239,7 +250,7 @@ def increment_emoji_count(emoji_name): @app.route('/api/stats') def get_stats(): - conn = sqlite3.connect('emoji_stats.db') + conn = sqlite3.connect(DB_PATH) c = conn.cursor() c.execute(''' SELECT diff --git a/requirements.txt b/requirements.txt index 01307ad..82f7d72 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ Flask==3.0.0 -python-dateutil==2.8.2 \ No newline at end of file +python-dateutil==2.8.2 +gunicorn==21.2.0 \ No newline at end of file diff --git a/startup.txt b/startup.txt new file mode 100644 index 0000000..bf62770 --- /dev/null +++ b/startup.txt @@ -0,0 +1 @@ +gunicorn --bind=0.0.0.0 --timeout 600 app:app \ No newline at end of file