abhijhacodes / newsletter

Backend for newsletter app in FastAPI

Home Page:https://newsletter-qmtk.onrender.com/docs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Newsletter backend

This repository contains code for backend of newsletter project in FastAPI

See the code for client side here

Steps to setup and run locally ๐Ÿ‘‡

Create python virtual environment

python -m venv fastapi-env

Activate virutal environment (Windows)

Powershell:
.\fastapi-env\Scripts\Activate.ps1
Command prompt:
fastapi-env\Scripts\activate.bat

Install all required dependencies

pip install -r requirements.txt

Create database

Create sqlite.db file in root of your project if you want to use sqlite database
or update SQLALCHEMY_DATABASE_URL in db/database.py file to use any other local or remote sql database

Run server locally

uvicorn main:app --reload

--reload flag enables live reload as you save files
This will run your server on http://localhost:8000
You can change port by running this command ๐Ÿ‘‡

uvicorn main:app --port PORT_NUMBER

Steps to deploy to production

You can use any approach to deploy your FastAPI application and Postgresql database to production either using Docker containers or using any cloud server provider.
I'm using render.com to deploy server as well as DB.

  • On render, create a new PostgreSQL instance, and copy the external URL from it after it is created.
  • change postgres to postgresql in the url
  • add the URL in POSTGRESQL_DATABASE_URL varible in your .env file
  • Install these dependencies:
    pip install alembic psycopg2 psycopg2-binary
  • Create alembic migration environment
    alembic init alembic
  • Update sqlalchemy.url in alembic.ini file to your postgresql url
  • In alembic/env.py file import your Base and all models from your database config files
  • Update value of target_metadata to
    target_metadata = Base.metadata
  • Do initial migration
    alembic revision --autogenerate
  • You can now start using this database
  • Add alembic.ini and alembic/versions in .gitignore
  • Now you can directly connect your code repo on render.com and deploy the app in new web service
  • Add all the environment variables of your app on render
  • For each subsequent migration of your database you can run the same command

About

Backend for newsletter app in FastAPI

https://newsletter-qmtk.onrender.com/docs


Languages

Language:Python 97.4%Language:Mako 2.6%