diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..f5f4229 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +.vercel +venv +.idea +__pycache__ diff --git a/.github/script/test.ps1 b/.github/script/test.ps1 new file mode 100644 index 0000000..fcd0167 --- /dev/null +++ b/.github/script/test.ps1 @@ -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 +} \ No newline at end of file diff --git a/.github/script/test.sh b/.github/script/test.sh new file mode 100644 index 0000000..12f3022 --- /dev/null +++ b/.github/script/test.sh @@ -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 \ No newline at end of file diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml new file mode 100644 index 0000000..d657a63 --- /dev/null +++ b/.github/workflows/docker-image.yml @@ -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) diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml new file mode 100644 index 0000000..b528a9e --- /dev/null +++ b/.github/workflows/python-app.yml @@ -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 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1e6eda4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.vercel +venv +.idea +__pycache__ \ No newline at end of file diff --git a/.gitpod.yml b/.gitpod.yml new file mode 100644 index 0000000..57df11a --- /dev/null +++ b/.gitpod.yml @@ -0,0 +1,2 @@ +tasks: + - init: docker-compose up diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1373048 --- /dev/null +++ b/Dockerfile @@ -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"] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9e0c47e --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..cdb6f47 --- /dev/null +++ b/README.md @@ -0,0 +1,106 @@ +
+ +
This example shows how to use FastApi on Vercel with Serverless Functions using the Python Runtime.
+ + +
+
+
+
+
+
+
+
+ Report Bug
+ Request Feature
+
Did you like the project? Please, considerate a donation to help improve!
+ +✨
+ +# 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!