Skip to content

Commit 789cdcf

Browse files
authored
Apply suggestions from code review
0 parents  commit 789cdcf

File tree

5 files changed

+144
-0
lines changed

5 files changed

+144
-0
lines changed

.github/workflows/deploy.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Deploy
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
deploy:
10+
environment: playground
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: read
14+
id-token: write
15+
16+
steps:
17+
- name: Checkout Repo
18+
uses: actions/checkout@v4
19+
20+
- name: Deploy
21+
uses: DefangLabs/[email protected]

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Flask-Railpack
2+
3+
[![1-click-deploy](https://raw.githubusercontent.com/DefangLabs/defang-assets/main/Logos/Buttons/SVG/deploy-with-defang.svg)](https://portal.defang.dev/redirect?url=https%3A%2F%2Fgithub.com%2Fnew%3Ftemplate_name%3Dsample-flask-template%26template_owner%3DDefangSamples)
4+
5+
This is a basic Flask to-do app that can be deployed with Defang. Note that alongside your `.py` file, include a `requirements.txt` so that the [Railpack](https://railpack.com/) can install the necessary packages with pip.
6+
7+
This project is intended to provide a basic understanding of how to get started with Flask on Defang. The items are stored in memory and are lost when the server is restarted. **It is not intended for production use**. If you need something production ready, you should use a managed database like Postgres or MySQL.
8+
9+
## Prerequisites
10+
11+
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
12+
2. (Optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) authenticate with your cloud provider account
13+
14+
## Configuration
15+
16+
For this sample, you will not need to provide [configuration](https://docs.defang.io/docs/concepts/configuration).
17+
18+
If you wish to provide configuration, see below for an example of setting a configuration for a value named `API_KEY`.
19+
20+
```bash
21+
defang config set API_KEY
22+
```
23+
24+
## Deployment
25+
26+
> [!NOTE]
27+
> Download [Defang CLI](https://github.com/DefangLabs/defang)
28+
29+
### Defang Playground
30+
31+
Deploy your application to the Defang Playground by opening up your terminal and typing:
32+
33+
```bash
34+
defang compose up
35+
```
36+
37+
### BYOC (AWS)
38+
39+
If you want to deploy to your own cloud account, you can use Defang BYOC:
40+
41+
1. [Authenticate your AWS account](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html), and check that you have properly set your environment variables like `AWS_PROFILE`, `AWS_REGION`, `AWS_ACCESS_KEY_ID`, and `AWS_SECRET_ACCESS_KEY`.
42+
2. Run in a terminal that has access to your AWS environment variables:
43+
```bash
44+
defang --provider=aws compose up
45+
```
46+
47+
---
48+
49+
Title: Flask
50+
51+
Short Description: A basic Flask to-do app.
52+
53+
Tags: Flask, Python, Railpack
54+
55+
Languages: python

compose.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
services:
2+
flask:
3+
restart: unless-stopped
4+
build:
5+
context: ./flask
6+
deploy:
7+
resources:
8+
reservations:
9+
cpus: "1.0"
10+
memory: 512M
11+
ports:
12+
- mode: ingress
13+
target: 5000
14+
published: 5000
15+
healthcheck:
16+
test: ["CMD", "python3", "-c", "import sys, urllib.request; urllib.request.urlopen(sys.argv[1]).read()", "http://localhost:5000/"]

flask/app.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from flask import Flask, jsonify, request, render_template_string
2+
3+
app = Flask(__name__)
4+
5+
# A simple in-memory structure to store tasks
6+
tasks = []
7+
8+
@app.route('/', methods=['GET'])
9+
def home():
10+
# Display existing tasks and a form to add a new task
11+
html = '''
12+
<!DOCTYPE html>
13+
<html>
14+
<head>
15+
<title>Todo List</title>
16+
</head>
17+
<body>
18+
<h1>Todo List</h1>
19+
<form action="/add" method="POST">
20+
<input type="text" name="task" placeholder="Enter a new task">
21+
<input type="submit" value="Add Task">
22+
</form>
23+
<ul>
24+
{% for task in tasks %}
25+
<li>{{ task }} <a href="/delete/{{ loop.index0 }}">x</a></li>
26+
{% endfor %}
27+
</ul>
28+
</body>
29+
</html>
30+
'''
31+
return render_template_string(html, tasks=tasks)
32+
33+
@app.route('/add', methods=['POST'])
34+
def add_task():
35+
# Add a new task from the form data
36+
task = request.form.get('task')
37+
if task:
38+
tasks.append(task)
39+
return home()
40+
41+
@app.route('/delete/<int:index>', methods=['GET'])
42+
def delete_task(index):
43+
# Delete a task based on its index
44+
if index < len(tasks):
45+
tasks.pop(index)
46+
return home()
47+
48+
if __name__ == '__main__':
49+
app.run(debug=True, host='0.0.0.0')

flask/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Flask==2.2.5
2+
Werkzeug==2.2.2
3+
MarkupSafe==2.1.1

0 commit comments

Comments
 (0)