In this example we will be creating a simple CRUD API that allows users to log in with their Google account and create and update journal entries. These entries will be stored in a Postgres database hosted on GCP and will be accessible to the user when they log in. All the code for this example can be found on GitHub.

Our application will:

  1. Connect to our postgres data base using BuildFlow’s SQLAlchemy dependency.
  2. Check if the request was authenticated. If not we will redirect the user to a login page, and perform the authentication flow.
  3. If this is the user’s first time logging in we will create a new user in our database.
  4. Handle the users request to create, read, update or delete journels stored in our postgres database

If all goes well, you should have a working chatbot that looks like this:

Before completing the example ensure you have installed BuildFlow with all extra dependencies.

To run this example on resources created on GCP use this github repo.

In order to run this code you will need to have a GCP project created that you have access to.

Follow the Google Cloud Walkthrough to create a new one if you need to.

1

Clone the GitHub Repo

git clone git@github.com:launchflow/gcp-saas-example.git
cd gcp-saas-example
2

Install your requirements

    pip install -r requirements.txt
3

Create a .env file

Rename the template.env to .env and update all variables

You will need to create OAuth 2.0 credentials to fill out these variables.

Or you can use one you already have in place if you want.

VariableDescription
GCP_PROJECT_IDThe GCP Project where your resources (Cloud SQL instance and database) should be created.
DATABASE_USERThe user used for authenticating with your database (defaults to postgres) this will be set in GCP in a later step.
DATABASE_PASSWORDThe password of the user for authenticating with your database this will be set in GCP in a later step.
CLIENT_IDOAuth client ID obtained from google.
CLIENT_SECRETOAuth client secret obtained from google.
4

Create your resources

Create all the resources that are required for your system.

    buildflow apply

Make sure you have logged in with GCP with: gcloud auth login --update-adc

This will output every primitive that will be created, and ask you to confirm before creating them:

Primitives to create:
---------------------
├── CloudSQLInstance
│   └── <GCP_PROJECT_ID>:launchflow-gcp-postgres-auth-cloud-sql
└── CloudSQLDatabase
└── <GCP_PROJECT_ID>:launchflow-gcp-postgres-auth-cloud-sql.launchflow-gcp-postgres-auth-cloud-db


Would you like to apply these changes?
Enter "y (yes)" to confirm, "n (no) to reject":
5

Create your database user

A user and password can be added following this guide. Ensure this matches with what you put in your .env file.

6

Update your OAuth Credentials

For your OAuth credentials update the Authorized JavaScript origins and Authorized redirect URIs to match your local environment.

For Authorized JavaScript origins you will need to add http://localhost:8000 and for Authorized redirect URIs you will need to add http://localhost:3001/auth/callback/google

7

Run your project

Update the .env to set CREATE_MODELS=true this will create all the necessary databases when you run your project.

Run your project with:

    buildflow run

Once running you can visit http://localhost:8000 to begin using your app.

8

Cleanup your resources

Once you’re finished with your app, you can easily delete all of your resources with one command.

    buildflow destroy