Skip to main content

FastAPI

FastAPI is a modern, fast (high-performance) web framework for building APIs, using Python and based on standard Python type hints. It has the following features:

  • Fast: Extremely high performance, on par with NodeJS and Go (thanks to Starlette and Pydantic). One of the fastest Python web frameworks.
  • Efficient Coding: Increases feature development speed by approximately 200% to 300%.
  • Fewer bugs: Reduces approximately 40% of human-induced (developer) errors.
  • Intelligent: Excellent editor support. Auto-completion everywhere reduces debugging time.
  • Simple: Designed to be easy to use and learn, reducing the time spent reading documentation.

This document describes how to deploy the sample FastAPI application on CloudBase (container-based) using multiple approaches:

Create a FastAPI Project

NOTE: If you already have a FastAPI project, you can skip this step.

  1. Create a fastapi-app directory
  2. In the fastapi-app directory, create an app.py file with the following content:
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def root():
return {"greeting": "Hello, World!", "message": "Welcome to FastAPI!"}

This app.py file defines a FastAPI application containing a root path ("/") and a GET request handler (root). When users access the root path, the application returns a JSON response containing a greeting and welcome message.

  1. In the fastapi-app directory, create a requirements.txt file with the following content:
fastapi==0.100.0
hypercorn==0.14.4

This requirements.txt file defines the dependencies required for the FastAPI application. Among them, hypercorn is the ASGI server needed to run FastAPI.

  1. Install dependencies and start the service

Use pip to install dependencies:

pip install -r requirements.txt

It is recommended to use a virtual environment to install dependencies.

python -m venv venv
source venv/bin/activate
pip install -r requirements.txt

After installation is complete, use hypercorn to start the service:

hypercorn main:app --bind 0.0.0.0:80

Accessing http://127.0.0.1:80 will return the corresponding result.

During the development phase, you can also use fastapi-cli to start the service:

First, install fastapi-cli:

pip install fastapi-cli

Then start the service:

fastapi dev main.py

Configuring Dockerfile

In the fastapi-app directory, create a new Dockerfile file with the following content:

FROM python:3-alpine

# Set Current Working Directory
WORKDIR /app

# Copy the current project into the container.
COPY . .

# Install dependency.
RUN pip install --no-cache-dir -r requirements.txt

# Start the service.
CMD ["hypercorn", "main:app", "--bind", "0.0.0.0:80"]

With the above changes, your express application will be deployable to Tencent Cloud Hosting!

Deployment to Cloud Hosting

Cloud Hosting provides multiple deployment methods to deploy your application:

Deploy via Console

Open Tencent Cloud Hosting, click Deploy via local code -> enter the service name -> select Upload code package for the deployment method -> select Folder for the code package type -> choose the cloudrun-fastapi directory for upload -> enter 80 for the port -> click Create and wait for the creation to complete.

Deploying via cli

If you have installed CloudBase CLI, you can use the following command in the project directory to deploy the application to CloudBase Cloud Hosting:

tcb cloudrun deploy

After entering the environment and service names, the CLI will automatically package the application image and deploy it to Cloud Hosting.

Besides manual deployment, you can also install the aforementioned applications with one click:

One-Click Deployment from Template

One-Click Deployment from github