This is the back-end of the CastBuddy server-side software. It has an incoming REST endpoint designed for Hologram to push data into the database, and it has outgoing REST endpoints to retrieve data from the database.
In all cases of this app running, it requires access to a MySQL database. Prior to any development or deployment, ensure that:
- Such a database exists with a schema called castbuddy.
- The database must have the username cb_admin configured with read and write access to the castbuddy schema.
- On all development and deployment servers, configure the following environment variables:
DB_ROOTset to the URI of the database, e.g.database.rds.amazonaws.comCB_PASSset to cb_admin's password for the database
Install the following packages in the following order:
- Install Python
- On Mac, it's suggested you first install homebrew and install python3 through it via
brew install python3.
- On Mac, it's suggested you first install homebrew and install python3 through it via
- Install PIP
- On Mac, it's suggested you first install homebrew and install pip3 through it via
brew install pip3(on mac, all calls to pip will be pip3 instead).
- On Mac, it's suggested you first install homebrew and install pip3 through it via
- Install virtualenv via PIP.
Development should be done in a python virtual environment to keep the packages necessary for this project decoupled from any individual's local environment.
- Open a terminal and
cdinto the repo root. - Create a directory called "venv".
- Create a new virtual environment in the venv directory with
virtualenv venv. - Open the file venv/bin/activate and add this line to the end:
export FLASK_APP=./application.py. - Enter the new virtual environment with
source venv/bin/activate. - Install the project's top-level dependencies with
pip install -r requirements-top.txt - You can exit the virtualenv with
deactivate.
If your IDE supports python parsing, configure your workspace to use the python binary located in ./venv/bin/python.
Depending on your IDE, when you open the project for the first time you may be prompted to install pylint. If you want to use the python linter, you must install it manually in the virtualenv.
- In a terminal enter the virtualenv.
- Install pylint with
python -m pip install -U pylint.
- In a terminal enter the virtual environment.
- Run the app with
flask run. - The console should tell you your app is being served at http://127.0.0.1:5000/ (aka localhost:5000).
You'll notice there are two requirement files: requirements-top.txt and requirements.txt. The idea behind this is that only a select few libraries are specifically called in the python project. Those select few libraries are listed in requirements-top.txt as the "top-level" dependencies, without a specific version number. When setting up a development environment and running pip install -r requirements-top.txt, further dependencies of the used libraries are installed.
As you develop, if you need to add libraries to the project, be sure to record them in requirements-top.txt.
When you're ready to push up to the server, requirements.txt needs to be updated. This file will be a "snapshot" record of all libraries in your development environment, including sub-dependencies and version numbers (this is analogous to npm's package.json and package-lock.json paradigm), so that when deploying to production, the exact environment used in development can be reproduced in deployment.
Follow this process to update requirements.txt before pushing:
- Make sure requirements-top.txt is updated.
- Clear all libraries currently in venv:
pip freeze > requirements.txtpip uninstall -r requirements.txt -y
- Re-install dependencies from requirements-top.txt:
pip install -r requirements-top.txt - Try running the flask app to make sure requirements-top.txt actually captures all necessary dependencies:
flask run- If you get errors due to missing dependencies, resolve them and update requirements-top.txt accordingly.
- Once you're sure you have only the libraries necessary for the project to run in venv, run
pip freeze > requirements.txtto update requirements.txt. - Commit and push.
Source: https://www.kennethreitz.org/essays/a-better-pip-workflow.
Deployment can be done in a number of ways according to Flask's documentation. One of the simpler ways to deploy is with gunicorn:
- Ensure Python is installed on the deploy server.
git clonethe repository on the server.- Follow the steps in Environment Setup, this time using requirements.txt.
- Enter the virtual environment.
- Serve the app with
gunicorn app:app -w 4 > api.log 2>&1 &. This will serve on the default port of 8000. - Use a reverse proxy like nginx to link an external port/address to localhost:8000.
To takedown the app, run pkill gunicorn.