Skip to content

Commit 1023a1a

Browse files
remove check to ensure compose files have names
we dont need these anymore, we can use stacks
0 parents  commit 1023a1a

6 files changed

Lines changed: 259 additions & 0 deletions

File tree

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Python & REST API
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-python-rest-api-template%26template_owner%3DDefangSamples)
4+
5+
This Flask application fetches average interest rates from the Fiscal Data Treasury API. It provides endpoints to check the status of the API and to retrieve the latest average interest rates. Note that alognside your .py file, include a requirements.txt so that the Dockerfile can install the necessary packages with pip.
6+
7+
## Features
8+
9+
Endpoint to check API status.
10+
Endpoint to fetch average interest rates from the Fiscal Data Treasury API.
11+
12+
## Essential Setup Files
13+
14+
1. A [Dockerfile](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/).
15+
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). This is optional
16+
17+
## Prerequisite
18+
19+
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
20+
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)
21+
22+
## A Step-by-Step Guide
23+
24+
1. Open the terminal and type `defang login`
25+
2. Type `defang compose up` in the CLI
26+
3. Your app should be up and running with Defang in minutes!
27+
28+
---
29+
30+
Title: Python & REST API
31+
32+
Short Description: A Flask application that fetches average interest rates from the Fiscal Data Treasury API.
33+
34+
Tags: Flask, REST API, Python
35+
36+
Languages: python

app/.gitignore

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
2+
# --- Python ---
3+
# Byte-compiled / optimized / DLL files
4+
__pycache__/
5+
*.py[cod]
6+
*$py.class
7+
8+
# C extensions
9+
*.so
10+
11+
# Distribution / packaging
12+
.Python
13+
build/
14+
develop-eggs/
15+
dist/
16+
downloads/
17+
eggs/
18+
.eggs/
19+
lib/
20+
lib64/
21+
parts/
22+
sdist/
23+
var/
24+
wheels/
25+
share/python-wheels/
26+
*.egg-info/
27+
.installed.cfg
28+
*.egg
29+
MANIFEST
30+
31+
# PyInstaller
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pdm
86+
.pdm.toml
87+
.pdm-python
88+
.pdm-build/
89+
90+
# PEP 582
91+
__pypackages__/
92+
93+
# Celery stuff
94+
celerybeat-schedule
95+
celerybeat.pid
96+
97+
# SageMath parsed files
98+
*.sage.py
99+
100+
# Environments
101+
.env
102+
.venv
103+
env/
104+
venv/
105+
ENV/
106+
env.bak/
107+
venv.bak/
108+
109+
# Spyder project settings
110+
.spyderproject
111+
.spyproject
112+
113+
# Rope project settings
114+
.ropeproject
115+
116+
# mkdocs documentation
117+
/site
118+
119+
# mypy
120+
.mypy_cache/
121+
.dmypy.json
122+
dmypy.json
123+
124+
# Pyre type checker
125+
.pyre/
126+
127+
# pytype static type analyzer
128+
.pytype/
129+
130+
# Cython debug symbols
131+
cython_debug/
132+
133+
# PyCharm
134+
.idea/

app/Dockerfile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Use an official Python runtime as a parent image
2+
FROM python:3.11-slim
3+
4+
# Set the working directory to /app
5+
WORKDIR /app
6+
7+
# Install required C++11 libraries and ca-certificates
8+
RUN apt-get update -qq \
9+
&& apt-get install -y \
10+
build-essential \
11+
python3-dev \
12+
ca-certificates \
13+
curl \
14+
&& apt-get clean \
15+
&& rm -rf /var/lib/apt/lists/*
16+
17+
# Install any needed packages specified in requirements.txt
18+
COPY requirements.txt /app/
19+
RUN pip install --no-cache-dir -r requirements.txt
20+
21+
# Copy the current directory contents into the container at /app
22+
COPY . /app
23+
24+
# Make port 5000 available to the world outside this container
25+
EXPOSE 5000
26+
27+
# Run main when the container launches
28+
ENTRYPOINT ["uwsgi", "--http", "0.0.0.0:5000", "--master", "-p", "2", "-w", "main:app"]
29+
USER nobody

app/main.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
import requests
3+
from flask import Flask
4+
from flask import request
5+
6+
7+
app = Flask(__name__)
8+
9+
10+
@app.route("/")
11+
def index():
12+
return { "status": "ok" }
13+
14+
15+
@app.route("/rates")
16+
def getQuote():
17+
# Declare string with the URL
18+
url = "https://api.fiscaldata.treasury.gov/services/api/fiscal_service/v2/accounting/od/avg_interest_rates?page[number]=1&page[size]=10"
19+
20+
headers = {
21+
"Content-Type": "application/json",
22+
}
23+
24+
# Fetch the data
25+
response = requests.get(url, headers=headers)
26+
27+
if (response.status_code != 200):
28+
return { "status": "error" }
29+
30+
# Convert the response to JSON
31+
data = response.json()
32+
33+
# Return the data
34+
return data
35+
36+
37+
if __name__ == '__main__':
38+
app.run(debug=True, host='0.0.0.0')

app/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
uwsgi
2+
flask
3+
requests

compose.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
services:
2+
app:
3+
restart: unless-stopped
4+
build:
5+
context: ./app
6+
dockerfile: Dockerfile
7+
ports:
8+
- mode: ingress
9+
target: 5000
10+
deploy:
11+
resources:
12+
reservations:
13+
memory: 256M
14+
healthcheck:
15+
test:
16+
- CMD
17+
- curl
18+
- -f
19+
- http://localhost:5000/

0 commit comments

Comments
 (0)