This is a Flask backend for Stronk that exposes a GraphQL API. We also use Azure Pipelines for CI.
Ensure you have Docker, python3.7, pip, postgreSQL and newman installed.
git clone https://github.com/not-monday/stronk-backend.git
cd stronk-backend
source venv/bin/activate
to use venvpip3 install -r requirements.txt
To install dependencies- Install Docker and Docker Compose
- Rename the
dotenv
file to.env
and fill out the secrets. - Open
.flaskenv
to fill out any missing environment variables. - Ensure you create a database in PostgreSQL that matches the one in
DATABASE_URL
in.env
. If you don't, follow the instructions below to set up the database! - Set up a Firebase Service Account for Stronk if you do not already have one and download the json credentials file
- Rename the json file to
stronk-google-credentials.json
and copy it to the the root of the project - Run
docker-compose -f docker-compose.yml -f docker-compose.dev.yml up
for development anddocker-compose up
otherwise. Running in development mounts the project dir in the container and lets the app reflect changes you make during development without being forced to rebuild each time.
Server will be live on port 5000 and database on port 5001.
Ensure you havepython3, , postgreSQL and newman installed.
- Set up your virtualenv by following this guide
pip install -r requirements.txt
- Create a Postgres database. This database will have a database (connect) url in the format
postgresql://{username}:{password}@localhost:{port}/{database name}
- Make a copy of
dotenv
and rename it to.env
and fill in the environment variables. You should also look at.flaskenv
where you can changeFLASK_ENV
. - Set up a Firebase Service Account for Stronk if you do not already have one and download the json credentials file
- Rename the json file to
stronk-google-credentials.json
and copy it to the the root of the project - Run the database migrations
flask db upgrade
- Run the server
flask run
Server will be live on port 5000
docker-compose.yml
will pull the latest version of the official postgres docker image.
Fill in the DB_NAME
and DB_PASSWORD
in .env
which correspond to the POSTGRES_PASSWORD
and POSTGRES_DB
in docker-compose.yml
. Any name is fine for the database, just make sure you remember it so that you can set up the .env file.
A database will be created automatically for you with the name DB_NAME
. Access from outside the db
container will require a password whereas access from within does not.
More info on this and the environment variables available here.
Run this to get which port the container is mapped to docker ps
DATABASE_URL="postgresql://{username}:{password}@localhost:{port}/{database name}"
# This is mine for example:
# DATABASE_URL="postgresql://postgres:test@localhost:32773/stronk"
# run this on your host if you need to connect to the database in the container
psql -h localhost -p {port} -U postgres --password
# psql -h localhost -p {port} -U {username} -c "CREATE DATABASE {database name};" --password
# Example:
# psql -h localhost -p 5001 -U postgres -c "CREATE DATABASE stronk;" --password
# -c : lets us run an SQL command directly
this is a pretty good resource to consult
To add mock data to the database so interacting is easier:
# example:
# psql -h localhost -p 32768 -U postgres -d stronk -a -f ./tests/fixtures/insert_mock_data.sql
psql -h localhost -p {port} -U {user name} -d {database name} -a -f ./tests/fixtures/insert_mock_data.sql
- Make sure your
.flaskenv
file is configured for testing and run the app
export FLASK_ENV=testing
flask run
- In another terminal, set the environment variable,
TEST_DATABASE_URL
to the address exposed by thedb
service. - Run
./scripts/test.sh
Server will be live on port 5000
- when developing, make sure to run repeat
step 3
to use your virtual env
We will use Flask Blueprints to modularize the design of the back-end. Blueprints can be found under the controllers/
directory.
Example usage of blueprints can be found here.
The entry point of the backend is stronk/__init__.py
.