Skip to content
This repository has been archived by the owner on Dec 10, 2021. It is now read-only.

platelet-app/platelet-api

Repository files navigation

Archived after platelet was moved to the Amplify platform.


Platelet API

This is the API for Platelet. It makes use of Flask, RESTx, SQLALchemy and Marshmallow along with other Flask plugins.

It is a REST API. Refer to api_routes.md or the generated Swagger documentation for more detailed documentation.

Docker set up

Run:

docker-compose up

to build and setup a docker instance.

To prepopulate the database with some data, put a sql file named platelet_init.sql in the root directory.

Manual setup on Ubuntu 20.04

Install needed packages:

sudo apt install postgresql postgresql-server-dev-12 python3-venv python3-dev python3-wheel gcc

Install ElasticSearch:

sudo apt install apt-transport-https

wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -

sudo add-apt-repository "deb https://artifacts.elastic.co/packages/7.x/apt stable main"

sudo apt update && sudo apt install elasticsearch

Install Redis

sudo apt install redis

Configure postgresql for local testing:

sudo nano /etc/postgresql/12/main/pg_hba.conf

Edit the line:

# IPv4 local connections:
host    all             all             127.0.0.1/32            md5

to:

# IPv4 local connections:
host    all             all             127.0.0.1/32            trust

This will allow unauthenticated connections on localhost

Restart the services:

sudo systemctl restart postgresql && sudo systemctl restart elasticsearch && sudo systemctl restart redis

Run the setup script:

./setup.sh

Optionally input a json file with premade data to insert into the database. You can also add a password to set for the admin user instead of setting it interactively.

./setup.sh dev_data.json somepass

Run

API

Make sure the services are active:

sudo systemctl start postgresql && sudo systemctl start elasticsearch

Source the environment:

source venv/bin/activate

Environment Variables:
SQLALCHEMY_DATABASE_URI

URI string for the database you are connecting to. E.g.:

postgres://user:password@some-database-host/database_name

If no URI is specified it will attempt to connect to a localhost database named platelet_dev

CLOUD_PLATFORM

Specify the cloud platform that should be used. Currently only aws is available.

AWS_ENDPOINT

If specified, it will attempt to connect to another endpoint. This can be useful for mocking AWS for testing (e.g with moto).

AWS_ACCESS_KEY_ID

The access key id for the AWS account you are connecting to.

AWS_SECRET_ACCESS_KEY

The secret access key for the AWS account you are connecting to.

CLOUD_PROFILE_PICTURE_STORE_NAME

The name of the image store on the cloud provider. On AWS this is the name of the bucket that has been created.

PROFILE_PROCESSING_DIRECTORY

A directory created locally that the flask process has write access to. Suggested /tmp/profile_pics

DEFAULT_PROFILE_PICTURE_URL

If a user has no profile picture set in the database, this URL will be returned for their picture instead.

CORS_ENABLED

Enable CORS. Defaults to False. When developing locally suggest setting this to True so that processes running on another port can access the API (e.g. a React server running on port 3000).

CORS_ORIGIN

The origin URL when CORS_ENABLED is set to True. Defaults to http://localhost:3000. Does nothing if CORS_ENABLED is set to False.

REDIS_URL

The URL for the Redis server. Defaults connecting to localhost if none is specified.

ELASTICSEARCH_URL

The URL for the ElasticSearch server. Defaults to None in which case no connection attempts will be made.

Start the server:

In a terminal, export your environment variables and run:

python app.py

For profile picture uploads, also run the redis worker. This must have the same environment variables as the Flask server.

python redis_worker.py

Run the tests (in another terminal):

source venv/bin/activate

cd tests

pytest -v .

(Some fail at the moment when all tests are ran together, but won't fail when ran individually.)

To run individual tests use

pytest -v . -k "some pattern"

e.g.:

pytest -v . -k "users"

Will run all tests associated with users.

Visit root to see the swagger API documentation:

http://localhost:5000

Development

Libraries

When you add a new library with pip install run:
pip freeze > requirements.txt

Database changes

When you make changes that require database migrations run:
flask db migrate -m "message"

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages