Teddy Xinyuan Chen
commited on
Commit
·
c376d00
1
Parent(s):
ce884e7
2024-10-08T20-55-52Z
Browse files
README.md
CHANGED
@@ -1,151 +1,40 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
[](https://vscode.dev/redirect?url=vscode://ms-vscode-remote.remote-containers/cloneInVolume?url=https://github.com%2Fpamelafox%2Fdjango-quiz-app)
|
5 |
|
6 |
-
|
7 |
|
8 |
-
|
9 |
|
10 |
-
|
11 |
|
12 |
-
|
13 |
|
14 |
-
The
|
15 |
|
16 |
-
##
|
17 |
|
18 |
-
|
19 |
|
20 |
-
|
21 |
|
22 |
-
|
|
|
|
|
23 |
|
24 |
-
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
pre-commit install
|
34 |
-
```
|
35 |
-
|
36 |
-
## Local development
|
37 |
-
|
38 |
-
1. 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 devcontainer, copy the values exactly from `.env.sample`.
|
39 |
-
|
40 |
-
2. Run the migrations: (Also available as a VS Code task)
|
41 |
-
|
42 |
-
```shell
|
43 |
-
python3 src/manage.py migrate
|
44 |
-
```
|
45 |
-
|
46 |
-
3. Run the local server at port 8000: (Also available as a VS Code task)
|
47 |
-
|
48 |
-
```shell
|
49 |
-
python3 src/manage.py runserver 8000
|
50 |
-
```
|
51 |
-
|
52 |
-
4. Navigate to the displayed URL to verify the website is working.
|
53 |
-
|
54 |
-
### Admin
|
55 |
-
|
56 |
-
This app comes with the built-in Django admin interface.
|
57 |
-
|
58 |
-
1. Create a superuser:
|
59 |
-
|
60 |
-
```shell
|
61 |
-
python3 src/manage.py createsuperuser
|
62 |
-
```
|
63 |
-
|
64 |
-
2. Restart the server and navigate to "/admin"
|
65 |
-
|
66 |
-
3. Login with the superuser credentials.
|
67 |
-
|
68 |
-
### Testing
|
69 |
-
|
70 |
-
Run tests:
|
71 |
-
|
72 |
-
```shell
|
73 |
-
python3 src/manage.py collectstatic
|
74 |
-
coverage run --source='.' src/manage.py test quizzes
|
75 |
-
coverage report
|
76 |
-
```
|
77 |
-
|
78 |
-
The same tests are also run as a Github action.
|
79 |
-
|
80 |
-
## Deployment
|
81 |
-
|
82 |
-
This repository is set up for deployment on Azure App Service (w/PostgreSQL flexible server) using the configuration files in the `infra` folder.
|
83 |
-
|
84 |
-
1. Sign up for a [free Azure account](https://azure.microsoft.com/free/) and create an Azure Subscription.
|
85 |
-
2. Install the [Azure Developer CLI](https://learn.microsoft.com/azure/developer/azure-developer-cli/install-azd). (If you open this repository in Codespaces or with the VS Code Dev Containers extension, that part will be done for you.)
|
86 |
-
3. Login to Azure:
|
87 |
-
|
88 |
-
```shell
|
89 |
-
azd auth login
|
90 |
-
```
|
91 |
-
|
92 |
-
4. Provision and deploy all the resources:
|
93 |
-
|
94 |
-
```shell
|
95 |
-
azd up
|
96 |
-
```
|
97 |
-
|
98 |
-
It will prompt you to provide an `azd` environment name (like "django-app"), select a subscription from your Azure account, and select a location (like "eastus"). Then it will provision the resources in your account and deploy the latest code. If you get an error with deployment, changing the location can help, as there may be availability constraints for some of the resources.
|
99 |
-
|
100 |
-
5. To be able to access `/admin`, you'll need a Django superuser. Navigate to the Azure Portal for the App Service, select SSH, and run this command:
|
101 |
-
|
102 |
-
```shell
|
103 |
-
python manage.py createsuperuser
|
104 |
-
```
|
105 |
-
|
106 |
-
6. When you've made any changes to the app code, you can just run:
|
107 |
-
|
108 |
-
```shell
|
109 |
-
azd deploy
|
110 |
-
```
|
111 |
-
|
112 |
-
### CI/CD pipeline
|
113 |
-
|
114 |
-
This project includes a Github workflow for deploying the resources to Azure
|
115 |
-
on every push to main. That workflow requires several Azure-related authentication secrets
|
116 |
-
to be stored as Github action secrets. To set that up, run:
|
117 |
-
|
118 |
-
```shell
|
119 |
-
azd pipeline config
|
120 |
-
```
|
121 |
-
|
122 |
-
## Security
|
123 |
-
|
124 |
-
It is important to secure the databases in web applications to prevent unwanted data access.
|
125 |
-
This infrastructure uses the following mechanisms to secure the PostgreSQL database:
|
126 |
-
|
127 |
-
* Azure Firewall: The database is accessible from all IPs. Once you've set up the database, you can restrict access to only Azure IPs by running `azd env set POSTGRES_ALLOW_ALL_IPS false` followed by `azd provision`.
|
128 |
-
* PostgreSQL Authentication: Passwordless connection using [Microsoft Entra Managed Identity](https://learn.microsoft.com/azure/postgresql/flexible-server/how-to-connect-with-managed-identity).
|
129 |
-
* PostgreSQL Version: Latest available on Azure, version 16, which includes security improvements.
|
130 |
-
* Django Admin Interface: The URL (typically "/admin") is a unique string generated based on the App Service Plan ID.
|
131 |
-
|
132 |
-
⚠️ For even more security, consider using an Azure Virtual Network to connect the Web App to the Database.
|
133 |
-
See [the Django-on-Azure project](https://github.com/tonybaloney/django-on-azure) for example infrastructure files.
|
134 |
-
|
135 |
-
### Costs
|
136 |
-
|
137 |
-
Pricing varies per region and usage, so it isn't possible to predict exact costs for your usage.
|
138 |
-
|
139 |
-
You can try the [Azure pricing calculator](https://azure.com/e/560b5f259111424daa7eb23c6848d164) for the resources:
|
140 |
-
|
141 |
-
* Azure App Service: Basic Tier with 1 CPU core, 1.75GB RAM. Pricing is hourly. [Pricing](https://azure.microsoft.com/pricing/details/app-service/linux/)
|
142 |
-
* PostgreSQL Flexible Server: Burstable Tier with 1 CPU core, 32GB storage. Pricing is hourly. [Pricing](https://azure.microsoft.com/pricing/details/postgresql/flexible-server/)
|
143 |
-
* Key Vault: Standard tier. Costs are per transaction, a few transactions are used on each deploy. [Pricing](https://azure.microsoft.com/pricing/details/key-vault/)
|
144 |
-
* Log analytics: Pay-as-you-go tier. Costs based on data ingested. [Pricing](https://azure.microsoft.com/pricing/details/monitor/)
|
145 |
-
|
146 |
-
⚠️ To avoid unnecessary costs, remember to take down your app if it's no longer in use,
|
147 |
-
either by deleting the resource group in the Portal or running `azd down`.
|
148 |
-
|
149 |
-
## Getting help
|
150 |
-
|
151 |
-
If you're working with this project and running into issues, please post in **Discussions**.
|
|
|
1 |
+
---
|
2 |
+
title: Django Quiz App
|
3 |
+
emoji: 🐳
|
4 |
+
colorFrom: purple
|
5 |
+
colorTo: gray
|
6 |
+
sdk: docker
|
7 |
+
app_port: 8000
|
8 |
+
---
|
9 |
|
10 |
+
# Django Quiz App
|
|
|
11 |
|
12 |
+
This is a Dockerized Django application designed to run on Hugging Face Spaces. The application is a quiz site that uses various environment variables and secrets for configuration.
|
13 |
|
14 |
+
## Model Description
|
15 |
|
16 |
+
This Dockerized Django application serves a quiz site. It is configured to connect to a PostgreSQL database hosted on Neon.tech and uses OpenAI's API for additional functionalities.
|
17 |
|
18 |
+
## Intended Use
|
19 |
|
20 |
+
The application is intended to be used as a quiz platform. It is configured to run in a development environment with debugging enabled.
|
21 |
|
22 |
+
## How to Use
|
23 |
|
24 |
+
To run the application, ensure that the following environment variables and secrets are set:
|
25 |
|
26 |
+
### Environment Variables
|
27 |
|
28 |
+
- `DJANGO_SETTINGS_MODULE`: The settings module for the Django application.
|
29 |
+
- `STATIC_BACKEND`: The backend for serving static files.
|
30 |
+
- `DEBUG`: Set to `True` for development.
|
31 |
|
32 |
+
### Secrets
|
33 |
|
34 |
+
- `DBHOST`: The hostname of the PostgreSQL database.
|
35 |
+
- `DBNAME`: The name of the PostgreSQL database.
|
36 |
+
- `DBUSER`: The username for the PostgreSQL database.
|
37 |
+
- `DBPASS`: The password for the PostgreSQL database.
|
38 |
+
- `DBSSL`: SSL mode for the PostgreSQL database.
|
39 |
+
- `SECRET_KEY`: The secret key for the Django application.
|
40 |
+
- `OPENAI_API_KEY`: The API key for OpenAI.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|