Simple CRUD API Async Database

Overview

In this example you can see how to build a simple CRUD API using LaunchFlow. All the source code for this example can be found on GitHub. This example will show you how to use LaunchFlow to create a Cloud SQL database to hold user information, and a GCS storage bucket to hold profile pictures. Then we will use an async database connection to interact with the database.

Code Walkthrough

There are three main files associated with our application:

infra.py is where we define the resource our application will use. In this case we define a Cloud SQL database and a GCS bucket. When you run launchflow create from the root directory of the project these resources will be created in the project and environment of your choice.

1
import launchflow as lf
2
3
# TODO(developer): Set these variables with your own values
4
db = lf.gcp.CloudSQLPostgres("launchflow-demo-db")
5
lf_bucket = lf.gcp.GCSBucket("launchflow-demo-bucket-1")

Running the application

To run this application make sure you update the launchflow.yaml to point to your own project and environment.

Install the dependencies

1
pip install -r requirements.txt

Login

To get started run login to LaunchFlow:

1
launchflow login

Connect GCP

If you haven't connected your LaunchFlow account to GCP, you can do so by running:

1
launchflow connect --provider=gcp

Create Resources

NOTE: Before running make sure you update the bucket name in infra.py to be unique.

To create the resources needed to run the application, run:

1
launchflow create

This will prompt you to create a LaunchFlow project and environment to hold the resources. It takes GCP several minutes to provision your Cloud SQL database.

Run the application

1
uvicorn app.main:app

Once running you can visit http://localhost:8000/docs to see the API documentation, and send requests.