Skip to content

Commit 61bd697

Browse files
committed
Switch from Gunicorn to Uvicorn with ASGI wrapper for Azure deployment
1 parent 8882301 commit 61bd697

3 files changed

Lines changed: 21 additions & 5 deletions

File tree

infra/main.bicep

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ resource appService 'Microsoft.Web/sites@2024-04-01' = {
6262
httpsOnly: true
6363
siteConfig: {
6464
linuxFxVersion: 'PYTHON|3.12'
65-
appCommandLine: 'python -m gunicorn --bind=0.0.0.0:8000 --workers=2 --timeout=600 main:app'
65+
appCommandLine: 'python -m uvicorn main:app --host 0.0.0.0 --port 8000'
6666
appSettings: [
6767
{
6868
name: 'SECRET_KEY'

main.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,26 @@
1111
from src.app import create_app # noqa: E402
1212
from src.models import db # noqa: E402
1313

14-
app = create_app()
14+
# Create Flask app
15+
flask_app = create_app()
1516

1617
# Initialize database tables
17-
with app.app_context():
18+
with flask_app.app_context():
1819
db.create_all()
1920

21+
# Create ASGI app for Uvicorn
22+
from werkzeug.middleware.proxy_fix import ProxyFix # noqa: E402
23+
flask_app.wsgi_app = ProxyFix(
24+
flask_app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_prefix=1
25+
)
26+
27+
# For Uvicorn
28+
try:
29+
from asgiref.wsgi import WsgiToAsgi # noqa: E402
30+
app = WsgiToAsgi(flask_app)
31+
except ImportError:
32+
# Fallback: use Flask directly
33+
app = flask_app
34+
2035
if __name__ == '__main__':
21-
app.run(debug=False, host='0.0.0.0', port=8000)
36+
flask_app.run(debug=False, host='0.0.0.0', port=8000)

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ Flask==3.0.0
22
Flask-CORS==4.0.0
33
Flask-SQLAlchemy==3.1.1
44
SQLAlchemy==2.0.23
5-
gunicorn==21.2.0
5+
uvicorn==0.24.0
6+
asgiref==3.7.2
67
python-dotenv==1.0.0
78
azure-identity==1.15.0
89
azure-keyvault-secrets==4.8.0

0 commit comments

Comments
 (0)