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:
- Connect to our postgres data base using BuildFlow’s SQLAlchemy dependency.
- Check if the request was authenticated. If not we will redirect the user to a login page, and perform the authentication flow.
- If this is the user’s first time logging in we will create a new user in our database.
- 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:
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.
Clone the GitHub Repo
git clone email@example.com:launchflow/gcp-saas-example.git cd gcp-saas-example
Install your requirements
pip install -r requirements.txt
Create a .env file
.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.
|GCP_PROJECT_ID||The GCP Project where your resources (Cloud SQL instance and database) should be created.|
|DATABASE_USER||The user used for authenticating with your database (defaults to |
|DATABASE_PASSWORD||The password of the user for authenticating with your database this will be set in GCP in a later step.|
|CLIENT_ID||OAuth client ID obtained from google.|
|CLIENT_SECRET||OAuth client secret obtained from google.|
Create your resources
Create all the resources that are required for your system.
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":
Create your database user
A user and password can be added following this guide. Ensure this matches with what you put in your
Update your OAuth Credentials
Run your project
.env to set
CREATE_MODELS=true this will create all the necessary databases when you run your project.
Run your project with:
Once running you can visit http://localhost:8000 to begin using your app.
Cleanup your resources
Once you’re finished with your app, you can easily delete all of your resources with one command.