|
1 | | -# Project |
| 1 | +This repository includes a simple Python Flask API with a single route that returns JSON. |
| 2 | +You can use this project as a starting point for your own APIs. |
2 | 3 |
|
3 | | -> This repo has been populated by an initial template to help get you started. Please |
4 | | -> make sure to update the content to build a great experience for community-building. |
| 4 | +The repository is designed for use with [Docker containers](https://www.docker.com/), both for local development and deployment, and includes infrastructure files for deployment to [Azure Container Apps](https://learn.microsoft.com/azure/container-apps/overview). 🐳 |
5 | 5 |
|
6 | | -As the maintainer of this project, please make a few updates: |
| 6 | +The code is organized using [Flask Blueprints](https://flask.palletsprojects.com/en/2.2.x/blueprints/), |
| 7 | +tested with [pytest](https://docs.pytest.org/en/7.2.x/), |
| 8 | +linted with [ruff](https://github.com/charliermarsh/ruff), and formatted with [black](https://black.readthedocs.io/en/stable/). |
| 9 | +Code quality issues are all checked with both [pre-commit](https://pre-commit.com/) and Github actions. |
7 | 10 |
|
8 | | -- Improving this README.MD file to provide a great experience |
9 | | -- Updating SUPPORT.MD with content about this project's support experience |
10 | | -- Understanding the security reporting process in SECURITY.MD |
11 | | -- Remove this section from the README |
| 11 | +## Opening the project |
12 | 12 |
|
13 | | -## Contributing |
| 13 | +This project has [Dev Container support](https://code.visualstudio.com/docs/devcontainers/containers), so it will be be setup automatically if you open it in Github Codespaces or in local VS Code with the [Dev Containers extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers). |
14 | 14 |
|
15 | | -This project welcomes contributions and suggestions. Most contributions require you to agree to a |
16 | | -Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us |
17 | | -the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com. |
| 15 | +If you're not using one of those options for opening the project, then you'll need to: |
18 | 16 |
|
19 | | -When you submit a pull request, a CLA bot will automatically determine whether you need to provide |
20 | | -a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions |
21 | | -provided by the bot. You will only need to do this once across all repos using our CLA. |
| 17 | +1. Create a [Python virtual environment](https://docs.python.org/3/tutorial/venv.html#creating-virtual-environments) and activate it. |
22 | 18 |
|
23 | | -This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). |
24 | | -For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or |
25 | | -contact [[email protected]](mailto:[email protected]) with any additional questions or comments. |
| 19 | +2. Install the requirements: |
26 | 20 |
|
27 | | -## Trademarks |
| 21 | + ```shell |
| 22 | + python3 -m pip install -r src/requirements-dev.txt |
| 23 | + ``` |
28 | 24 |
|
29 | | -This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft |
30 | | -trademarks or logos is subject to and must follow |
31 | | -[Microsoft's Trademark & Brand Guidelines](https://www.microsoft.com/en-us/legal/intellectualproperty/trademarks/usage/general). |
32 | | -Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. |
33 | | -Any use of third-party trademarks or logos are subject to those third-party's policies. |
| 25 | +3. Install the pre-commit hooks: |
| 26 | + |
| 27 | + ```shell |
| 28 | + pre-commit install |
| 29 | + ``` |
| 30 | + |
| 31 | +## Local development |
| 32 | + |
| 33 | +1. Run the local server: |
| 34 | + |
| 35 | + ```shell |
| 36 | + python3 -m flask --debug --app src/app:app run --port 5000 |
| 37 | + ``` |
| 38 | + |
| 39 | +3. Click 'http://127.0.0.1:5000' in the terminal, which should open a new tab in the browser. |
| 40 | + |
| 41 | +4. Try the API at '/generate_name' and try passing in a parameter at the end of the URL, like '/generate_name?start_with=N'. |
| 42 | + |
| 43 | +### Local development with Docker |
| 44 | + |
| 45 | +You can also run this app with Docker, thanks to the `Dockerfile`. |
| 46 | +You need to either have Docker Desktop installed or have this open in Github Codespaces for these commands to work. |
| 47 | + |
| 48 | +1. Build the image: |
| 49 | + |
| 50 | + ``` |
| 51 | + docker build --tag flask-app src/ |
| 52 | + ``` |
| 53 | + |
| 54 | +2. Run the image: |
| 55 | + |
| 56 | + ``` |
| 57 | + docker run --publish 5000:5000 flask-app |
| 58 | + ``` |
| 59 | + |
| 60 | +### Deployment |
| 61 | + |
| 62 | +This repo is set up for deployment on Azure Container Apps using the configuration files in the `infra` folder. |
| 63 | + |
| 64 | +This diagram shows the architecture of the deployment: |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +Steps for deployment: |
| 69 | + |
| 70 | +1. Sign up for a [free Azure account](https://azure.microsoft.com/free/) and create an Azure Subscription. |
| 71 | +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.) |
| 72 | +3. Login to Azure: |
| 73 | + |
| 74 | + ```shell |
| 75 | + azd auth login |
| 76 | + ``` |
| 77 | + |
| 78 | +4. Provision and deploy all the resources: |
| 79 | + |
| 80 | + ```shell |
| 81 | + azd up |
| 82 | + ``` |
| 83 | + It will prompt you to provide an `azd` environment name (like "flask-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. |
| 84 | + |
| 85 | +5. When `azd` has finished deploying, you'll see an endpoint URI in the command output. Visit that URI, and you should see the API output! 🎉 |
| 86 | +6. When you've made any changes to the app code, you can just run: |
| 87 | + |
| 88 | + ```shell |
| 89 | + azd deploy |
| 90 | + ``` |
| 91 | + |
| 92 | +### Costs |
| 93 | + |
| 94 | +Pricing varies per region and usage, so it isn't possible to predict exact costs for your usage. |
| 95 | +The majority of the Azure resources used in this infrastructure are on usage-based pricing tiers. |
| 96 | +However, Azure Container Registry has a fixed cost per registry per day. |
| 97 | +
|
| 98 | +You can try the [Azure pricing calculator](https://azure.com/e/9f8185b239d240b398e201078d0c4e7a) for the resources: |
| 99 | +
|
| 100 | +- Azure Container App: Consumption tier with 0.5 CPU, 1GiB memory/storage. Pricing is based on resource allocation, and each month allows for a certain amount of free usage. [Pricing](https://azure.microsoft.com/pricing/details/container-apps/) |
| 101 | +- Azure Container Registry: Basic tier. [Pricing](https://azure.microsoft.com/pricing/details/container-registry/) |
| 102 | +- Log analytics: Pay-as-you-go tier. Costs based on data ingested. [Pricing](https://azure.microsoft.com/pricing/details/monitor/) |
| 103 | +
|
| 104 | +⚠️ To avoid unnecessary costs, remember to take down your app if it's no longer in use, |
| 105 | +either by deleting the resource group in the Portal or running `azd down`. |
| 106 | + |
| 107 | + |
| 108 | +## Getting help |
| 109 | + |
| 110 | +If you're working with this project and running into issues, please post in **Discussions**. |
0 commit comments