From 914beea614ccadc71e6fca859041f588a919e7b7 Mon Sep 17 00:00:00 2001 From: Raphael Titsworth-Morin Date: Wed, 29 May 2024 16:25:36 -0700 Subject: [PATCH 1/2] please, please, work. --- .github/workflows/deploy.yaml | 30 +++++++++++++++++++++ README.md | 30 +++++++++++++++++++++ compose.yaml | 19 ++++++++++++++ flask/Dockerfile | 25 ++++++++++++++++++ flask/app.py | 49 +++++++++++++++++++++++++++++++++++ flask/requirements.txt | 3 +++ 6 files changed, 156 insertions(+) create mode 100644 .github/workflows/deploy.yaml create mode 100644 README.md create mode 100644 compose.yaml create mode 100644 flask/Dockerfile create mode 100644 flask/app.py create mode 100644 flask/requirements.txt diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml new file mode 100644 index 0000000..5adedef --- /dev/null +++ b/.github/workflows/deploy.yaml @@ -0,0 +1,30 @@ +name: deploy + +on: + push: + branches: + - main + - test-* + +concurrency: + group: deploy + cancel-in-progress: false + +jobs: + deploy: + runs-on: ubuntu-latest + permissions: + id-token: write + + steps: + - name: Checkout Repo + uses: actions/checkout@v4 + + - name: Install defang + run: . <(curl -Ls https://s.defang.io/install) + + - name: Login to Defang + run: defang login + + - name: Deploy + run: defang compose up -v --detach \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..8fb343f --- /dev/null +++ b/README.md @@ -0,0 +1,30 @@ +# A Simple Flask App + +This is a sample of a basic Flask TODO app. The items are stored in memory and are lost when the server is restarted, but it should give you a basic idea of how to get started with Flask on Defang. Note that alognside your .py file, include a requirements.txt so that the Dockerfile can install the necessary packages with pip. + +### NOTE: +This sample is a simple Flask app that demonstrates how to create a TODO app using Flask. The items are stored in memory and are lost when the server is restarted. This sample is intended to provide a basic understanding of how to get started with Flask on Defang. **it is not intended for production use**. If you need something production ready, you should use a managed database like Postgres or MySQL. + +## Essential Setup Files +1. A [Dockerfile](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/). +2. A [compose file](https://docs.defang.io/docs/concepts/compose) to define and run multi-container Docker applications (this is how Defang identifies services to be deployed). +3. A [.dockerignore](https://docs.docker.com/build/building/context/#dockerignore-files) to ignore files that are not needed in the Docker image or will be generated during the build process. + +## Prerequisite +1. Download [Defang CLI](https://github.com/DefangLabs/defang) +2. If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc), make sure you have properly [authenticated your AWS account (optional)](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html) + +## A Step-by-Step Guide +1. Open the terminal and type `defang login` +2. Type `defang compose up` in the CLI +3. Your app should be up and running with Defang in minutes! + +--- + +Title: Simple Flask App + +Short Description: A basic Flask todo app + +Tags: flask, python + +Languages: python \ No newline at end of file diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..df73828 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,19 @@ +version: "3.9" +name: flask +services: + flask: + restart: unless-stopped + build: + context: ./flask + dockerfile: Dockerfile + deploy: + resources: + reservations: + cpus: "1.0" + memory: 512M + ports: + - mode: ingress + target: 5000 + published: 5000 + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost:5000/"] diff --git a/flask/Dockerfile b/flask/Dockerfile new file mode 100644 index 0000000..66dfded --- /dev/null +++ b/flask/Dockerfile @@ -0,0 +1,25 @@ +# Use an official Python runtime as a base image +FROM python:3.9-slim + +# Set the working directory to /app +WORKDIR /app + +# Install required packages +RUN apt-get update \ + && apt-get install -y --no-install-recommends \ + build-essential \ + python3-dev \ + && rm -rf /var/lib/apt/lists/* + +# Copy the current directory contents into the container at /app +COPY . /app + +# Install any needed packages specified in requirements.txt +COPY requirements.txt /app/ +RUN pip install --no-cache-dir -r requirements.txt + +# Make port 5000 available to the world outside this container +EXPOSE 5000 + +# Run app.py when the container launches +CMD ["python", "app.py"] diff --git a/flask/app.py b/flask/app.py new file mode 100644 index 0000000..416ed04 --- /dev/null +++ b/flask/app.py @@ -0,0 +1,49 @@ +from flask import Flask, jsonify, request, render_template_string + +app = Flask(__name__) + +# A simple in-memory structure to store tasks +tasks = [] + +@app.route('/', methods=['GET']) +def home(): + # Display existing tasks and a form to add a new task + html = ''' + + + + Todo List + + +

Todo List

+
+ + +
+ + + +''' + return render_template_string(html, tasks=tasks) + +@app.route('/add', methods=['POST']) +def add_task(): + # Add a new task from the form data + task = request.form.get('task') + if task: + tasks.append(task) + return home() + +@app.route('/delete/', methods=['GET']) +def delete_task(index): + # Delete a task based on its index + if index < len(tasks): + tasks.pop(index) + return home() + +if __name__ == '__main__': + app.run(debug=True, host='0.0.0.0') diff --git a/flask/requirements.txt b/flask/requirements.txt new file mode 100644 index 0000000..0c81b4a --- /dev/null +++ b/flask/requirements.txt @@ -0,0 +1,3 @@ +Flask==2.2.5 +Werkzeug==2.2.2 +MarkupSafe==2.1.1 From 2b449e7f82dcd7584584f9b9b4ef6a03b74d169a Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 29 May 2024 23:26:22 +0000 Subject: [PATCH 2/2] Bump werkzeug from 2.2.2 to 3.0.3 in /flask Bumps [werkzeug](https://github.com/pallets/werkzeug) from 2.2.2 to 3.0.3. - [Release notes](https://github.com/pallets/werkzeug/releases) - [Changelog](https://github.com/pallets/werkzeug/blob/main/CHANGES.rst) - [Commits](https://github.com/pallets/werkzeug/compare/2.2.2...3.0.3) --- updated-dependencies: - dependency-name: werkzeug dependency-type: direct:production ... Signed-off-by: dependabot[bot] --- flask/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flask/requirements.txt b/flask/requirements.txt index 0c81b4a..21cfd76 100644 --- a/flask/requirements.txt +++ b/flask/requirements.txt @@ -1,3 +1,3 @@ Flask==2.2.5 -Werkzeug==2.2.2 +Werkzeug==3.0.3 MarkupSafe==2.1.1