Backend API written in Python (Flask).
This guide will show you how to deploy the API application.
For deployment, you only need Docker and docker-compose.
Choose whether to use SQLite or MySQL as your database driver. SQLite is great for smaller applications where no traffic is expected or if you are tight on RAM (SQLite is just a .db
file). MySQL scales a lot better but uses more RAM. You can also easily modify the project to work with PostgreSQL since the module Flask-SQLAlchemy, that is used in this project, supports it as well.
Clone the project to your server. Go to the backend
folder. Copy the file .env.mysql.example
to .env
and modify the variables to suit your setup. All the variables are described in the file.
Once configured, start the app with docker-compose
.
docker-compose -f docker-compose.prod.mysql.yml up -d
Clone the project to your server. Go to the backend
folder. Copy the file .env.sqlite.example
to .env
and modify the variables to suit your setup. All the variables are described in the file.
Once configured, start the app with docker-compose
.
docker-compose -f docker-compose.prod.mysql.yml up -d
Once you got the app running, you need to create your first user from the command-line. This can easily be done using docker-compose (replace mysql
with sqlite
if you are using SQLite).
docker-compose -f docker-compose.prod.mysql.yml exec app python scripts/create_user.py
Serving the app on port 80/443 is not suitable. You should use Nginx to proxy_pass
all requests on /api
to the Docker container. Then you can use Nginx to serve the static frontend which improves application performance.
Example Nginx location block (this is not a full Nginx configuration file!).
location /api {
# replace 5000 with the port you are using
proxy_pass http://127.0.0.1:5000;
proxy_redirect off;
}
For local development, you do not only need the deployment dependencies but also the following:
- Python 3.9
- Pipenv
For testing, you need a MySQL database running locally. We use docker-compose to make this very easy.
docker-compose up -d
Then use Pipenv to make sure you got all the dependencies for the Python application.
pipenv --python 3.9 install --dev
To enter the virtual environment, use Pipenv.
pipenv shell
You can now start the app locally.
python app.py