How to Build and Deploy a FastAPI Task Manager App on Vercel | by Arctype | Feb, 2022

Construct and deploy a Postgres FastAPI utility

How to build and deploy a  FastAPI task manager app on Vercel

Organising our venture

mkdir PostgresWithFastAPI && cd PostgresWithFastAPI
contact fundamental,database,mannequin,schema,session.py
pip set up virtualenv
python3 -m venv env
supply env/bin/activate
pip set up fastapi uvicorn sqlalchemy psycopg2-binary
pip freeze > necessities.txt
from fastapi import FastAPI
app = FastAPI()

@app.get("/")
def read_root():
return "message": "Server is up and working!"

uvicorn fundamental:app --reload
How to build and deploy a  FastAPI task manager app on Vercel
sudo su - postgresAltering to the Postgres person account
createuser --interactiveCreating a brand new person
How to build and deploy a  FastAPI task manager app on Vercel
createdb job
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
SQLALCHEMY_DATABASE_URL = "postgresql://postgres:1234@localhost:5432/job"
engine = create_engine(SQLALCHEMY_DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
from sqlalchemy.schema import Column
from sqlalchemy.sorts import String, Integer, Textual content
from database import Base

class Process(Base):
__tablename__ = "Duties"
id = Column(Integer, primary_key=True, index=True)
task_name = Column(String(20))
task_des = Column(Textual content())
created_by = Column(String(20))
date_created = Column(String(15))

from pydantic import BaseModel
from typing import Optionally available

class task_schema(BaseModel):
task_name :str
task_des :str
created_by : Optionally available[str]= None
date_created : Optionally available[str]= None

class Config:
orm_mode = True

import mannequin
from database import SessionLocal, engine

mannequin.Base.metadata.create_all(bind=engine)
def create_get_session():
strive:
db = SessionLocal()
yield db
lastly:
db.shut()

from fastapi import FastAPI, Relies upon, HTTPException
from sqlalchemy.orm import Session
from typing import Record
from mannequin import Process
from schema import task_schema
from session import get_database_session

@app.get("/job", response_model=Record[task_schema], status_code=200)
async def read_tasks(db: Session = Relies upon(create_get_session)):
duties = db.question(Process).all()
return duties

@app.publish('/job', response_model = task_schema, status_code=201)
async def create_task(job: task_schema, db: Session = Relies upon(create_get_session)):
new_task = Process(
task_name = job.task_name,
task_des = job.task_des,
created_by =job.created_by,
datecreated = job.date_created,
)
db.add(new_task)
db.commit()

return new_task


@app.get("/job/id", response_model = task_schema, status_code=200)
async def get_task(id:int,db: Session = Relies upon(create_get_session)):
job = db.question(Process).get(id)
return job

@app.patch("/job/id", response_model = task_schema, status_code=200)
async def update_task(id:int, job:task_schema, db: Session = Relies upon(create_get_session)):
db_task = db.question(Process).get(id)
db_task.task_name = job.task_name
db_task.task_des = job.task_des
db.commit()
db.refresh(db_task)

return db_task

@app.delete('/job/id', status_code=200)
async def delete_task(id:int, db: Session = Relies upon(create_get_session)):
db_task = db.question(Process).get(id)
if not db_task:
elevate HTTPException(status_code="404",element="Process id doesn't exist")

db.delete(db_task)
db.commit()

return None

How to build and deploy a  FastAPI task manager app on Vercel
How to build and deploy a  FastAPI task manager app on Vercel
How to build and deploy a  FastAPI task manager app on Vercel
How to build and deploy a  FastAPI task manager app on Vercel
How to build and deploy a  FastAPI task manager app on Vercel
SQLALCHEMY_DATABASE_URL = “Distant connection string”
//Ubuntu
sudo npm set up vercel

//Home windows
npm set up -g vercel

Vercel login

"builds": [ "src": "main.py", "use": "@vercel/python" ],
"routes": [ "src": "/(.*)", "dest": "main.py" ]
Vercel .
How to build and deploy a  FastAPI task manager app on Vercel
How to build and deploy a  FastAPI task manager app on Vercel
How to build and deploy a  FastAPI task manager app on Vercel

More Posts