Skip to content
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
78 changes: 78 additions & 0 deletions .github/workflows/deploy_emoji-caller.yml
Original file line number Diff line number Diff line change
@@ -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'

19 changes: 15 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
@@ -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('''
Expand Down Expand Up @@ -34,15 +45,15 @@ 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()}
conn.close()
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 = ?',
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Flask==3.0.0
python-dateutil==2.8.2
python-dateutil==2.8.2
gunicorn==21.2.0
1 change: 1 addition & 0 deletions startup.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gunicorn --bind=0.0.0.0 --timeout 600 app:app
Loading