Getting Started with Inspira on Render
This guide will demonstrate how you can deploy Inspira applications on Render.
Prerequisites
Before proceeding with this tutorial, it is essential to read the How To Use a PostgreSQL Database in a Inspira Application guide.
Gunicorn
When we put our application into action for real use, we'll use Gunicorn along with Uvicorn workers to make it work smoothly.
$ pip install gunicorn
Configuration Update
Please open the config.py
file and modify its content to match the following configuration:
import os
from inspira.config import Config
config = Config()
config["SECRET_KEY"] = os.environ.get("SECRET_KEY", "my-dummy-secret-key")
uri = os.environ.get("DATABASE_URL", "no-db")
if uri.startswith("postgres://"):
uri = uri.replace("postgres://", "postgresql://", 1)
config['SQLALCHEMY_DATABASE_URI'] = uri
We are replacing postgres
with postgresql
because SQLAlchemy
has removed support for the postgres
name.
You can also change the DATABASE_URL
in the settings of the app under Environment
and redeploy the app.
Update to Main File
Open main.py
and modify it as shown below:
from inspira import Inspira
from config import config
app = Inspira(config=config)
The secret_key
has been removed and replaced with config since the secret_key
is now included in the config file.
Migrations
To initiate the creation of the user table, execute the following command to generate a migration file:
$ inspira new migration create_table_users
This command will create the migration file in the migrations
folder.
Open the file and make the necessary modifications as shown below:
CREATE TABLE users (
id SERIAL NOT NULL,
name VARCHAR(50),
email VARCHAR(120),
PRIMARY KEY (id),
UNIQUE (email)
);
Deploy to Render
Create two files, build.sh
and render.yml
, in the root of your project.
Append the following content to build.sh
:
#!/usr/bin/env bash
# exit on error
set -o errexit
pip install -r requirements.txt
inspira migrate
In this script, we install the dependencies and execute the migrate
command to create the tables.
Ensure that the script is set as executable before committing it to Git.
$ chmod a+x build.sh
Include the following in render.yml
:
databases:
- name: mydb
databaseName: mydb
user: mydb
services:
- type: web
name: inspira_app_on_render
runtime: python
buildCommand: "./build.sh"
startCommand: "gunicorn main:app -k uvicorn.workers.UvicornWorker"
envVars:
- key: DATABASE_URL
fromDatabase:
name: mydb
property: connectionString
- key: SECRET_KEY
generateValue: true
- key: WEB_CONCURRENCY
value: 4
Log in to your Render account. Navigate to Blueprints
and choose the repository that contains the render.yaml
file.
Great job!
Your Inspira application is now up and running on Render with a PostgreSQL database.
The code is available on GitHub
Happy coding!