Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
FormunaGit authored Jul 23, 2024
0 parents commit 95f7a21
Show file tree
Hide file tree
Showing 20 changed files with 339 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.vercel
venv
.idea
__pycache__
27 changes: 27 additions & 0 deletions .github/script/test.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/pwsh

if (Get-Command pip -ErrorAction SilentlyContinue) {
Write-Host "Installing Python dependencies..."
pip install -r requirements.txt
}
else {
Write-Host "pip is not installed"
Write-Host "Installing pip"
Invoke-WebRequest -Uri https://bootstrap.pypa.io/get-pip.py -OutFile get-pip.py
python get-pip.py
Write-Host "Installing Python dependencies..."
pip install -r requirements.txt
}

if (Get-Command pytest -ErrorAction SilentlyContinue) {
Write-Host "Running tests..."
pytest --version
pytest
}
else {
Write-Host "pytest is not installed"
Write-Host "installing pytest"
pip install pytest
pytest --version
pytest
}
22 changes: 22 additions & 0 deletions .github/script/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/sh

if ! [ -x "$(command -v pip)" ]; then
echo 'Error: pip is not installed.' >&2
echo 'Installing pip...'
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
rm get-pip.py
echo 'pip installed.'
fi

pip install -r requirements.txt

if ! [ -x "$(command -v pytest)" ]; then
echo 'Error: pytest is not installed.' >&2
echo 'Installing pytest'
pip install pytest
echo 'pytest installed.'
fi


pytest
18 changes: 18 additions & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Docker Image CI

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:

build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Build the Docker image
run: docker build . --file Dockerfile --tag my-image-name:$(date +%s)
29 changes: 29 additions & 0 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Python testing

on:
push:
branches: ["main"]
pull_request:
branches: ["main"]

permissions:
contents: read

jobs:
run_tests_on_ubuntu:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run Bash script file
run: |
chmod +x ./.github/script/test.sh
./.github/script/test.sh
shell: bash
run_tests_on_windows:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- name: Run PowerShell script file
run: |
.\.github/script/test.ps1
shell: pwsh
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.vercel
venv
.idea
__pycache__
2 changes: 2 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tasks:
- init: docker-compose up
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM python:3.9

WORKDIR /app

COPY requirements.txt .

RUN pip install --upgrade pip && \
pip install --no-cache-dir -r requirements.txt

COPY . .

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Hebert F. Barros

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
106 changes: 106 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<p align="center">
<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" />
<h2 align="center">:package: deploy-python-fastapi-in-vercel</h2>
<p align="center">This example shows how to use FastApi on Vercel with Serverless Functions using the Python Runtime.</p>
</p>

<p align="center">
<a href="https://github.com/hebertcisco/deploy-python-fastapi-in-vercel/issues">
<img alt="Issues" src="https://img.shields.io/github/issues/hebertcisco/deploy-python-fastapi-in-vercel?style=flat&color=336791" />
</a>
<a href="https://github.com/hebertcisco/deploy-python-fastapi-in-vercel/pulls">
<img alt="GitHub pull requests" src="https://img.shields.io/github/issues-pr/hebertcisco/deploy-python-fastapi-in-vercel?style=flat&color=336791" />
</a>
<br />
<a href="https://github.com/hebertcisco/deploy-python-fastapi-in-vercel/issues/new/choose">Report Bug</a>
<a href="https://github.com/hebertcisco/deploy-python-fastapi-in-vercel/issues/new/choose">Request Feature</a>
</p>
<h3 align="center">Systems on which it has been tested.</h3>
<p align="center">
<a href="https://ubuntu.com/download">
<img alt="Ubuntu" src="https://img.shields.io/badge/Ubuntu-E95420?style=flat&logo=ubuntu&logoColor=white" />
</a>
<a href="https://www.microsoft.com/pt-br/software-download/windows10">
<img alt="Windows" src="https://img.shields.io/badge/Windows-0078D6?style=flat&logo=windows&logoColor=white" />
</a>
</p>
<p align="center">Did you like the project? Please, considerate <a href="https://www.buymeacoffee.com/hebertcisco">a donation</a> to help improve!</p>

<p align="center"><strong></strong>✨</p>

# Getting started

[![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)

# FastApi + Vercel

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).

[![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)
[![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)

## Demo

[deploy-python-fastapi-in-vercel.vercel.app](https://deploy-python-fastapi-in-vercel.vercel.app)

## How it Works

This example uses the Web Server Gateway Interface (WSGI) with FastApi to enable handling requests on Vercel with Serverless Functions.

## Running Locally

### With Docker Compose

```bash
docker-compose up
```

### With Docker

```bash
# Build the Docker image
docker build -t deploy-python-fastapi-in-vercel .

# Run the Docker container
docker run -p 8000:8000 deploy-python-fastapi-in-vercel

```

### With uvicorn

#### Install dependencies

```bash
pip install -r requirements.txt
```

```bash
uvicorn main:app --host 0.0.0.0 --port 8000
```

Your FastApi application is now available at `http://localhost:8000`.

## One-Click Deploy

Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=vercel-examples):

[![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)

## 🤝 Contributing

Contributions, issues and feature requests are welcome!<br />Feel free to check [issues page](issues).

## Show your support

Give a ⭐️ if this project helped you!

Or buy me a coffee 🙌🏾

<a href="https://www.buymeacoffee.com/hebertcisco">
<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" />
</a>

## 📝 License

Copyright © 2024 [Hebert F Barros](https://github.com/hebertcisco).<br />
This project is [MIT](LICENSE) licensed.
1 change: 1 addition & 0 deletions api/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from src.index import app
5 changes: 5 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
web:
build: .
ports:
- "8000:8000"
1 change: 1 addition & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from api.index import app
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fastapi==0.109.1
pytest==7.1.3
pytest-asyncio==0.20.3
uvicorn==0.20.0
pydantic~=1.10.4
Empty file added src/__main__.py
Empty file.
4 changes: 4 additions & 0 deletions src/dtos/ISayHelloDto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from pydantic import BaseModel

class ISayHelloDto(BaseModel):
message: str
20 changes: 20 additions & 0 deletions src/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from fastapi import FastAPI

from src.dtos.ISayHelloDto import ISayHelloDto

app = FastAPI()


@app.get("/")
async def root():
return {"message": "Hello World"}


@app.get("/hello/{name}")
async def say_hello(name: str):
return {"message": f"Hello {name}"}


@app.post("/hello")
async def hello_message(dto: ISayHelloDto):
return {"message": f"Hello {dto.message}"}
20 changes: 20 additions & 0 deletions test_index.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Test your FastAPI endpoints

GET http://127.0.0.1:8000/
Accept: application/json

###

GET http://127.0.0.1:8000/hello/User
Accept: application/json

###

POST /hello HTTP/1.1
Host: 127.0.0.1:8000
Content-Type: application/json
Content-Length: 28

{
"message": "Fulano!"
}
23 changes: 23 additions & 0 deletions test_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pytest

from src.dtos.ISayHelloDto import ISayHelloDto
from src.index import root, say_hello, hello_message


@pytest.mark.asyncio
async def test_root():
result = await root()
assert result == {'message': 'Hello World'}


@pytest.mark.asyncio
async def test_say_hello():
result = await say_hello("John")
assert result == {'message': 'Hello John'}


@pytest.mark.asyncio
async def test_hello_message():
dto = ISayHelloDto(message="Alice")
result = await hello_message(dto)
assert result == {'message': 'Hello Alice'}
15 changes: 15 additions & 0 deletions vercel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"devCommand": "uvicorn main:app --host 0.0.0.0 --port 3000",
"builds": [
{
"src": "api/index.py",
"use": "@vercel/python"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "api/index.py"
}
]
}

0 comments on commit 95f7a21

Please sign in to comment.