Skip to content

Commit bc344c0

Browse files
committed
Initial commit
0 parents  commit bc344c0

19 files changed

+307
-0
lines changed

.dockerignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.vercel
2+
venv
3+
.idea
4+
__pycache__

.github/script/test.ps1

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/pwsh
2+
3+
if (Get-Command pip -ErrorAction SilentlyContinue) {
4+
Write-Host "Installing Python dependencies..."
5+
pip install -r requirements.txt
6+
}
7+
else {
8+
Write-Host "pip is not installed"
9+
Write-Host "Installing pip"
10+
Invoke-WebRequest -Uri https://bootstrap.pypa.io/get-pip.py -OutFile get-pip.py
11+
python get-pip.py
12+
Write-Host "Installing Python dependencies..."
13+
pip install -r requirements.txt
14+
}
15+
16+
if (Get-Command pytest -ErrorAction SilentlyContinue) {
17+
Write-Host "Running tests..."
18+
pytest --version
19+
pytest
20+
}
21+
else {
22+
Write-Host "pytest is not installed"
23+
Write-Host "installing pytest"
24+
pip install pytest
25+
pytest --version
26+
pytest
27+
}

.github/script/test.sh

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/sh
2+
3+
if ! [ -x "$(command -v pip)" ]; then
4+
echo 'Error: pip is not installed.' >&2
5+
echo 'Installing pip...'
6+
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
7+
python get-pip.py
8+
rm get-pip.py
9+
echo 'pip installed.'
10+
fi
11+
12+
pip install -r requirements.txt
13+
14+
if ! [ -x "$(command -v pytest)" ]; then
15+
echo 'Error: pytest is not installed.' >&2
16+
echo 'Installing pytest'
17+
pip install pytest
18+
echo 'pytest installed.'
19+
fi
20+
21+
22+
pytest

.github/workflows/docker-image.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Docker Image CI
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
jobs:
10+
11+
build:
12+
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- uses: actions/checkout@v3
17+
- name: Build the Docker image
18+
run: docker build . --file Dockerfile --tag my-image-name:$(date +%s)

.github/workflows/python-app.yml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Python testing
2+
3+
on:
4+
push:
5+
branches: ["main"]
6+
pull_request:
7+
branches: ["main"]
8+
9+
permissions:
10+
contents: read
11+
12+
jobs:
13+
run_tests_on_ubuntu:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v2
17+
- name: Run Bash script file
18+
run: |
19+
chmod +x ./.github/script/test.sh
20+
./.github/script/test.sh
21+
shell: bash
22+
run_tests_on_windows:
23+
runs-on: windows-latest
24+
steps:
25+
- uses: actions/checkout@v2
26+
- name: Run PowerShell script file
27+
run: |
28+
.\.github/script/test.ps1
29+
shell: pwsh

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.vercel
2+
venv
3+
.idea
4+
__pycache__

.gitpod.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
tasks:
2+
- init: pip install -r requirements.txt
3+
4+

Dockerfile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM python:3.9
2+
3+
WORKDIR /app
4+
5+
COPY requirements.txt .
6+
7+
RUN pip install --upgrade pip && \
8+
pip install --no-cache-dir -r requirements.txt
9+
10+
COPY . .
11+
12+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

README.md

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<p align="center">
2+
<img width="100px" src="https://raw.githubusercontent.com/hebertcisco/vercel-typescript-express-api/cebd0c563141a4cc7d279997b8cb5dd9232d7591/.github/images/favicon512x512-vercel-typescript-express-api.png" align="center" alt=":package: deploy-python-fastapi-in-vercel" />
3+
<h2 align="center">:package: deploy-python-fastapi-in-vercel</h2>
4+
<p align="center">This example shows how to use FastApi on Vercel with Serverless Functions using the Python Runtime.</p>
5+
</p>
6+
7+
<p align="center">
8+
<a href="https://github.com/hebertcisco/deploy-python-fastapi-in-vercel/issues">
9+
<img alt="Issues" src="https://img.shields.io/github/issues/hebertcisco/deploy-python-fastapi-in-vercel?style=flat&color=336791" />
10+
</a>
11+
<a href="https://github.com/hebertcisco/deploy-python-fastapi-in-vercel/pulls">
12+
<img alt="GitHub pull requests" src="https://img.shields.io/github/issues-pr/hebertcisco/deploy-python-fastapi-in-vercel?style=flat&color=336791" />
13+
</a>
14+
<br />
15+
<a href="https://github.com/hebertcisco/deploy-python-fastapi-in-vercel/issues/new/choose">Report Bug</a>
16+
<a href="https://github.com/hebertcisco/deploy-python-fastapi-in-vercel/issues/new/choose">Request Feature</a>
17+
</p>
18+
<h3 align="center">Systems on which it has been tested.</h3>
19+
<p align="center">
20+
<a href="https://ubuntu.com/download">
21+
<img alt="Ubuntu" src="https://img.shields.io/badge/Ubuntu-E95420?style=flat&logo=ubuntu&logoColor=white" />
22+
</a>
23+
<a href="https://www.microsoft.com/pt-br/software-download/windows10">
24+
<img alt="Windows" src="https://img.shields.io/badge/Windows-0078D6?style=flat&logo=windows&logoColor=white" />
25+
</a>
26+
</p>
27+
<p align="center">Did you like the project? Please, considerate <a href="https://www.buymeacoffee.com/hebertcisco">a donation</a> to help improve!</p>
28+
29+
<p align="center"><strong></strong>✨</p>
30+
31+
# Getting started
32+
33+
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fhebertcisco%2Fdeploy-python-fastapi-in-vercel%2Ftree%2Fmain%2Fpython%2FFastApi&demo-title=FastApi%20%2B%20Vercel&demo-description=Use%20FastApi%202%20on%20Vercel%20with%20Serverless%20Functions%20using%20the%20Python%20Runtime.&demo-url=https%3A%2F%2FFastApi-python-template.vercel.app%2F&demo-image=https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png)
34+
35+
# FastApi + Vercel
36+
37+
This example shows how to use FastApi 0.88.0 on Vercel with Serverless Functions using the [Python Runtime](https://vercel.com/docs/concepts/functions/serverless-functions/runtimes/python).
38+
39+
[![Python testing](https://github.com/hebertcisco/deploy-python-fastapi-in-vercel/actions/workflows/python-app.yml/badge.svg?branch=main)](https://github.com/hebertcisco/deploy-python-fastapi-in-vercel/actions/workflows/python-app.yml)
40+
[![Docker Image CI](https://github.com/hebertcisco/deploy-python-fastapi-in-vercel/actions/workflows/docker-image.yml/badge.svg)](https://github.com/hebertcisco/deploy-python-fastapi-in-vercel/actions/workflows/docker-image.yml)
41+
42+
## Demo
43+
44+
[deploy-python-fastapi-in-vercel.vercel.app](https://deploy-python-fastapi-in-vercel.vercel.app)
45+
46+
## How it Works
47+
48+
This example uses the Web Server Gateway Interface (WSGI) with FastApi to enable handling requests on Vercel with Serverless Functions.
49+
50+
## Running Locally
51+
52+
### With Docker
53+
```bash
54+
# Build the Docker image
55+
docker build -t deploy-python-fastapi-in-vercel .
56+
57+
# Run the Docker container
58+
docker run -p 8000:8000 deploy-python-fastapi-in-vercel
59+
60+
```
61+
62+
### With uvicorn
63+
64+
#### Install dependencies
65+
66+
```bash
67+
pip install -r requirements.txt
68+
```
69+
```bash
70+
uvicorn main:app --host 0.0.0.0 --port 8000
71+
```
72+
73+
Your FastApi application is now available at `http://localhost:8000`.
74+
75+
## One-Click Deploy
76+
77+
Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=vercel-examples):
78+
79+
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fhebertcisco%2Fdeploy-python-fastapi-in-vercel%2Ftree%2Fmain%2Fpython%2FFastApi&demo-title=FastApi%20%2B%20Vercel&demo-description=Use%20FastApi%202%20on%20Vercel%20with%20Serverless%20Functions%20using%20the%20Python%20Runtime.&demo-url=https%3A%2F%2FFastApi-python-template.vercel.app%2F&demo-image=https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png)
80+
81+
## 🤝 Contributing
82+
83+
Contributions, issues and feature requests are welcome!<br />Feel free to check [issues page](issues).
84+
85+
## Show your support
86+
87+
Give a ⭐️ if this project helped you!
88+
89+
Or buy me a coffee 🙌🏾
90+
91+
<a href="https://www.buymeacoffee.com/hebertcisco">
92+
<img src="https://img.buymeacoffee.com/button-api/?text=Buy me a coffee&emoji=&slug=hebertcisco&button_colour=FFDD00&font_colour=000000&font_family=Inter&outline_colour=000000&coffee_colour=ffffff" />
93+
</a>
94+
95+
## 📝 License
96+
97+
Copyright © 2023 [Hebert F Barros](https://github.com/hebertcisco).<br />
98+
This project is [MIT](LICENSE) licensed.

api/index.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from src.index import app

docker-compose.yml

Whitespace-only changes.

main.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from api.index import app

requirements.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fastapi==0.88.0
2+
pytest==7.1.3
3+
pytest-asyncio==0.20.3
4+
uvicorn==0.20.0
5+
pydantic~=1.10.4

src/__main__.py

Whitespace-only changes.

src/dtos/ISayHelloDto.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from pydantic import BaseModel
2+
3+
class ISayHelloDto(BaseModel):
4+
message: str

src/index.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from fastapi import FastAPI
2+
3+
from src.dtos.ISayHelloDto import ISayHelloDto
4+
5+
app = FastAPI()
6+
7+
8+
@app.get("/")
9+
async def root():
10+
return {"message": "Hello World"}
11+
12+
13+
@app.get("/hello/{name}")
14+
async def say_hello(name: str):
15+
return {"message": f"Hello {name}"}
16+
17+
18+
@app.post("/hello")
19+
async def hello_message(dto: ISayHelloDto):
20+
return {"message": f"Hello {dto.message}"}

test_index.http

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Test your FastAPI endpoints
2+
3+
GET http://127.0.0.1:8000/
4+
Accept: application/json
5+
6+
###
7+
8+
GET http://127.0.0.1:8000/hello/User
9+
Accept: application/json
10+
11+
###
12+
13+
POST /hello HTTP/1.1
14+
Host: 127.0.0.1:8000
15+
Content-Type: application/json
16+
Content-Length: 28
17+
18+
{
19+
"message": "Fulano!"
20+
}

test_index.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import pytest
2+
3+
from src.dtos.ISayHelloDto import ISayHelloDto
4+
from src.index import root, say_hello, hello_message
5+
6+
7+
@pytest.mark.asyncio
8+
async def test_root():
9+
result = await root()
10+
assert result == {'message': 'Hello World'}
11+
12+
13+
@pytest.mark.asyncio
14+
async def test_say_hello():
15+
result = await say_hello("John")
16+
assert result == {'message': 'Hello John'}
17+
18+
19+
@pytest.mark.asyncio
20+
async def test_hello_message():
21+
dto = ISayHelloDto(message="Alice")
22+
result = await hello_message(dto)
23+
assert result == {'message': 'Hello Alice'}

vercel.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"devCommand": "uvicorn main:app --host 0.0.0.0 --port 3000",
3+
"builds": [
4+
{
5+
"src": "api/index.py",
6+
"use": "@vercel/python"
7+
}
8+
],
9+
"routes": [
10+
{
11+
"src": "/(.*)",
12+
"dest": "api/index.py"
13+
}
14+
]
15+
}

0 commit comments

Comments
 (0)