Note
This directory features the backend/API part of the full-stack web app.
Read through this documentation to install dependencies.
-
Python 3.11+
andPIP
- follow instructions to install the latest version of Python for your platform in this Python docs -
Working In a Virtual Environment - it is recommended to leverage a virtual environment whenever using Python for projects. This keeps your dependencies for each project separate and organized. Instructions for setting up a virtual environment for your platform can be found in this Python docs
NOTE
: You can create the virtual environment via GNU make
, a commandline language. For context, GNU Make is (typically) a tool which controls the generation of executables and other non-source files of a program from the program's source files. In this case, we use it to specify commands to create a virtual environment.
Create a siloed Python environment, and activate it - using a rule or directive in Makefile. This will use your pip virtualenv
or pip venv
depending on which one is installed on your local machine.
# cd backend; DO in a terminal/shell;
make setup
# activate virtual environment;
source splog/bin/activate # GNU/Linux Bash
source splog/Scripts/activate # GitBash Windows
- Install
PIP
Dependencies - once your virtual environment is setup/activated and running, install the required dependencies in/backend
directory. This will use the dependencies listed out in therequirements.tx
file:
# expands to 'pip install -r requirements.txt'
make install
-
Flask is a lightweight backend microservices framework. Flask is required to handle requests and responses. The API is in
flaskr/__init__.py
and it referencesmodels.py
. -
SQLAlchemy is the Python SQL toolkit and ORM we'll use to handle the lightweight
PostgreSQL
database. -
Flask-CORS is the extension we'll use to handle cross-origin requests from our frontend server, which will communicate with the backend via HTTP.
With Postgres
running, create a senpais_log
database:
# system terminal;
$ pg_isready
$ sudo -u <username> -i
# PSQL shell;
$ createdb senpais_log;
The analogous command for Windows environment:
# system terminal; login to postgres
$ psql -U <username>
# PSQL shell;
$ create database senpais_log;
TIP:
default<username>
ispostgres
IF Postgres is not running, then start it;
# replace 'path/to/PostgreSQL/data' with the actual path;
$ pg_ctl -D 'C:/Program Files/PostgreSQL/16/data' start
From within the root backend directory and in an activated virtual environment, execute:
# the --reload flag will detect file changes and restart the server automatically.
export FLASK_APP=flaskr && export FLASK_DEBUG=true && flask run --reload
Note
For eachAPI
endpoint, response data is expected from it, and to be returned inJSON
. This data is consumed by the REACT frontend viaCORS
based proxy.
- Use Flask-CORS to enable cross-domain requests and set response headers.
- Create an endpoint to handle
GET
requests for anime titles, including pagination (every 5 titles). This endpoint should return a list of titles, number of total titles. - Create an endpoint to handle
GET
requests for all available titles, anime, users. - Create an endpoint to
DELETE
a anime using a animeID
. - Create an endpoint to
POST
a new anime, titles etc which will require the descriptions and other attributes etc. - Create a
POST
endpoint to get anime based on asearch term
. It should return any anime for a match of the search term is a substring of the anime. - Create a
POST
endpoint to get anime for recommendations. This endpoint should return a random anime for a user. - Create error handlers for all expected errors including 400, 404, 422, and 500.
View the API DOCS
for sample API endpoints behavior, including each URL, request parameters, and the response body.
You can use the dump file
in this directory if you want to assess how the app works with prepoluted data.
$
# cd backend
psql -U <username> -d senpais_log < splog-anime.sql
$