Skip to content

Commit 2a54cc3

Browse files
committed
merge, synchronize AZD template/steps, add redis to dev container
2 parents aeb2104 + 88e1d10 commit 2a54cc3

File tree

18 files changed

+264
-132
lines changed

18 files changed

+264
-132
lines changed

.devcontainer/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# .devcontainer directory
2+
3+
This `.devcontainer` directory contains the configuration for a [dev container](https://docs.github.com/codespaces/setting-up-your-project-for-codespaces/adding-a-dev-container-configuration/introduction-to-dev-containers) and isn't used by the sample application.
4+
5+
The dev container configuration lets you open the repository in a [GitHub codespace](https://docs.github.com/codespaces/overview) or a dev container in Visual Studio Code. For your convenience, the dev container is configured with the following:
6+
7+
- Python
8+
- PostgreSQL
9+
- [Azure Developer CLI](https://learn.microsoft.com/azure/developer/azure-developer-cli/overview) (so you can run `azd` commands directly).

.devcontainer/devcontainer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
2-
"name": "msdocs-django-postgresql-sample-app",
2+
"name": "msdocs-flask-postgresql-sample-app",
33
"dockerComposeFile": "docker-compose.yml",
44
"service": "app",
55
"workspaceFolder": "/workspace",
66
"features": {
77
"ghcr.io/azure/azure-dev/azd:latest": {}
8-
},
8+
},
99
"customizations": {
1010
"vscode": {
1111
// Add the IDs of extensions you want installed when the container is created.
@@ -51,9 +51,9 @@
5151
}
5252
}
5353
},
54-
// Use 'forwardPorts' to make a list of ports inside the container available locally.
54+
// Use 'forwardPorts' to make a list of ports inside the container available locally. 5000 is for Flask, and 5432 is for PostgreSQL.
5555
"forwardPorts": [
56-
8000, 5432
56+
5000, 5432
5757
],
5858
// Use 'postCreateCommand' to run commands after the container is created.
5959
// "postCreateCommand": "",

.devcontainer/docker-compose.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@ services:
2121
volumes:
2222
- postgres-data:/var/lib/postgresql/data
2323
environment:
24-
POSTGRES_USER: app_user
2524
POSTGRES_DB: app
25+
POSTGRES_USER: app_user
2626
POSTGRES_PASSWORD: app_password
2727

2828
# Add "forwardPorts": ["5432"] to **devcontainer.json** to forward PostgreSQL locally.
2929
# (Adding the "ports" property to this file will not forward from a Codespace.)
3030

31+
redis:
32+
image: redis
33+
restart: unless-stopped
34+
3135
volumes:
3236
postgres-data:

.env.sample

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
DBNAME=<database name>
1+
DBNAME=<database-name>
22
DBHOST=<database-hostname>
33
DBUSER=<db-user-name>
44
DBPASS=<db-password>
5-
SECRET_KEY=<secret key>
5+
CACHELOCATION=<redis-cache-url>
6+
SECRET_KEY=<secret-key>

.env.sample.devcontainer

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ DBNAME=app
22
DBHOST=localhost
33
DBUSER=app_user
44
DBPASS=app_password
5-
SECRET_KEY=secret_key
5+
CACHELOCATION=redis://redis:6379/0
6+
SECRET_KEY=secret_key

.github/workflows/azure-dev.yaml

Lines changed: 0 additions & 61 deletions
This file was deleted.

.vscode/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# .vscode directory
2+
3+
This `.vscode` directory contains configuration that lets you launch and debug in Visual Studio Code and isn't used by the sample application.

README.md

Lines changed: 22 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,70 +2,61 @@
22

33
This is a Python web app using the Django framework and the Azure Database for PostgreSQL relational database service. The Django app is hosted in a fully managed Azure App Service. This app is designed to be be run locally and then deployed to Azure. You can either deploy this project by following the tutorial [*Deploy a Python (Django or Flask) web app with PostgreSQL in Azure*](https://docs.microsoft.com/azure/app-service/tutorial-python-postgresql-app) or by using the [Azure Developer CLI (azd)](https://learn.microsoft.com/azure/developer/azure-developer-cli/overview) according to the instructions below.
44

5+
Additionally, the sample application demonstrates Azure Redis Cache access by caching the restaurant details page for 60 seconds. You can add the Azure Redis Cache integration in the [secure-by-default web app + database creation wizard](https://portal.azure.com/?feature.customportal=false#create/Microsoft.AppServiceWebAppDatabaseV3), and it's also included in the [AZD template](https://github.com/Azure-Samples/python-app-service-postgresql-infra).
6+
57
## Requirements
68

7-
The [requirements.txt](./requirements.txt) has the following packages:
9+
The [requirements.txt](./requirements.txt) has the following packages, all used by a typical data-driven Django application:
810

911
| Package | Description |
1012
| ------- | ----------- |
1113
| [Django](https://pypi.org/project/Django/) | Web application framework. |
1214
| [pyscopg2-binary](https://pypi.org/project/psycopg-binary/) | PostgreSQL database adapter for Python. |
1315
| [python-dotenv](https://pypi.org/project/python-dotenv/) | Read key-value pairs from .env file and set them as environment variables. In this sample app, those variables describe how to connect to the database locally. <br><br> This package is used in the [manage.py](./manage.py) file to load environment variables. |
1416
| [whitenoise](https://pypi.org/project/whitenoise/) | Static file serving for WSGI applications, used in the deployed app. <br><br> This package is used in the [azureproject/production.py](./azureproject/production.py) file, which configures production settings. |
17+
| [django-redis](https://pypi.org/project/django-redis/) | Redis cache backend for Django. |
1518

16-
## Using this project with the Azure Developer CLI (azd)
19+
## Run the sample
1720

18-
This project is designed to work well with the [Azure Developer CLI](https://learn.microsoft.com/azure/developer/azure-developer-cli/overview),
19-
which makes it easier to develop apps locally, deploy them to Azure, and monitor them.
21+
This project has a [dev container configuration](.devcontainer/), which makes it easier to develop apps locally, deploy them to Azure, and monitor them. The easiest way to run this sample application is inside a GitHub codespace. Follow these steps:
2022

21-
### Local development
23+
1. Fork this repository to your account. For instructions, see [Fork a repo](https://docs.github.com/get-started/quickstart/fork-a-repo).
2224

23-
This project has Dev Container support, so you can open it in Github Codespaces or local VS Code with the Dev Containers extension. If you're unable to open the Dev Container,
24-
then it's best to first [create a Python virtual environment](https://docs.python.org/3/tutorial/venv.html#creating-virtual-environments) and activate that.
25+
1. From the repository root of your fork, select **Code** > **Codespaces** > **+**.
2526

26-
1. Install the requirements:
27+
1. In the codespace terminal, run the following commands:
2728

2829
```shell
30+
# Install requirements
2931
python3 -m pip install -r requirements.txt
30-
```
31-
32-
2. Create an `.env` file using `.env.sample` as a guide. Set the value of `DBNAME` to the name of an existing database in your local PostgreSQL instance. Set the values of `DBHOST`, `DBUSER`, and `DBPASS` as appropriate for your local PostgreSQL instance. If you're in the Dev Container, copy the values from `.env.sample.devcontainer`.
33-
34-
3. In the `.env` file, fill in a secret value for `SECRET_KEY`. You can use this command to generate an appropriate value:
35-
36-
```shell
37-
python -c 'import secrets; print(secrets.token_hex())'
38-
```
39-
40-
4. Run the migrations: (or use VS Code "Run" button and select "Migrate")
41-
42-
```shell
32+
# Create .env with environment variables
33+
cp .env.sample.devcontainer .env
34+
# Run database migrations
4335
python3 manage.py migrate
44-
```
45-
46-
5. Run the local server: (or use VS Code "Run" button and select "Run server")
47-
48-
```shell
36+
# Start the development server
4937
python3 manage.py runserver
5038
```
5139

52-
### Deployment
40+
1. When you see the message `Your application running on port 8000 is available.`, click **Open in Browser**.
5341

54-
This repo is set up for deployment on Azure App Service (w/PostGreSQL server) using the configuration files in the `infra` folder.
42+
### Quick deploy
5543

56-
🎥 Watch a deployment of the code in [this screencast](https://www.youtube.com/watch?v=JDlZ4TgPKYc).
44+
This project is designed to work well with the [Azure Developer CLI](https://learn.microsoft.com/azure/developer/azure-developer-cli/overview), which makes it easier to develop apps locally, deploy them to Azure, and monitor them.
45+
46+
🎥 Watch a deployment of the code in [this screencast](https://www
47+
.youtube.com/watch?v=JDlZ4TgPKYc).
5748

5849
Steps for deployment:
5950

6051
1. Sign up for a [free Azure account](https://azure.microsoft.com/free/)
61-
2. Install the [Azure Dev CLI](https://learn.microsoft.com/azure/developer/azure-developer-cli/install-azd). (If you opened this repository in a Dev Container, that part will be done for you.)
52+
2. Install the [Azure Dev CLI](https://learn.microsoft.com/azure/developer/azure-developer-cli/install-azd). (If you opened this repository in a Dev Container, it's already installed for you.)
6253
3. Initialize a new `azd` environment:
6354
6455
```shell
6556
azd init
6657
```
6758
68-
It will prompt you to provide a name (like "django-app") that will later be used in the name of the deployed resources.
59+
It will prompt you to provide a name (like "django-app"), which will later be used in the name of the deployed resources.
6960
7061
4. Provision and deploy all the resources:
7162
@@ -91,25 +82,6 @@ Steps for deployment:
9182
azd deploy
9283
```
9384
94-
### CI/CD pipeline
95-
96-
This project includes a Github workflow for deploying the resources to Azure
97-
on every push. That workflow requires several Azure-related authentication secrets to be stored as Github action secrets. To set that up, run:
98-
99-
```shell
100-
azd pipeline config
101-
```
102-
103-
### Monitoring
104-
105-
The deployed resources include a Log Analytics workspace with an Application Insights dashboard to measure metrics like server response time.
106-
107-
To open that dashboard, just run:
108-
109-
```shell
110-
azd monitor --overview
111-
```
112-
11385
## Getting help
11486
11587
If you're working with this project and running into issues, please post in [Issues](/issues).

azure.yaml

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
11
# yaml-language-server: $schema=https://raw.githubusercontent.com/Azure/azure-dev/main/schemas/v1.0/azure.yaml.json
2+
# azure.yaml is an azd configuration file and isn't used by the sample application.
23

3-
name: django-postgresql-sample-app
4+
name: python-app-service-postgresql-redis-infra
45
metadata:
5-
template: django-postgresql-sample-app@0.0.1-beta
6+
template: python-app-service-postgresql-redis-infra@0.0.1-beta
67
services:
78
web:
89
project: .
910
language: py
1011
host: appservice
12+
hooks:
13+
postprovision:
14+
posix:
15+
shell: sh
16+
run: echo $'\n\nApp Service app has the following settings:\n' && echo "$WEB_APP_SETTINGS" | jq -r '.[]' | sed 's/\(.*\)/\t- \1/' && echo -e $"\nSee the settings in the portal:\033[1;36m $WEB_APP_CONFIG"
17+
interactive: true
18+
continueOnError: true
19+
windows:
20+
shell: pwsh
21+
run: Write-Host "`n`nApp Service app has the following settings:`n" $WEB_APP_SETTINGS | ConvertFrom-Json | ForEach-Object { Write-Host "\t- $_" }
22+
interactive: true
23+
continueOnError: true
24+
postdeploy:
25+
posix:
26+
shell: sh
27+
run: echo -e $"\n\nOpen SSH session to App Service container at:\033[1;36m $WEB_APP_SSH\033[0m" && echo -e $"Stream App Service logs at:\033[1;36m $WEB_APP_LOG_STREAM"
28+
interactive: true
29+
continueOnError: true
30+
windows:
31+
shell: pwsh
32+
run: Write-Host "`n`nOpen SSH session to App Service container at:`n" $WEB_APP_SSH; Write-Host "Stream App Service logs at:`n" $WEB_APP_LOG_STREAM
33+
interactive: true
34+
continueOnError: true

azureproject/production.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
'django.middleware.clickjacking.XFrameOptionsMiddleware',
2323
]
2424

25+
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
2526
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
2627
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
2728

@@ -38,3 +39,14 @@
3839
'PASSWORD': conn_str_params['password'],
3940
}
4041
}
42+
43+
CACHES = {
44+
"default": {
45+
"BACKEND": "django_redis.cache.RedisCache",
46+
"LOCATION": os.environ.get('AZURE_REDIS_CONNECTIONSTRING'),
47+
"OPTIONS": {
48+
"CLIENT_CLASS": "django_redis.client.DefaultClient",
49+
"COMPRESSOR": "django_redis.compressors.zlib.ZlibCompressor",
50+
},
51+
}
52+
}

0 commit comments

Comments
 (0)