This repository contains unofficial patterns, sample code, or tools to help developers build more effectively with Fauna. All Fauna Labs repositories are provided “as-is” and without support. By using this repository or its contents, you agree that this repository may never be officially supported and moved to the Fauna organization.
This is the backend API service for a sample e-commerce application that uses Fauna as a database.
Features include:
- Authentication using Fauna's built-in authentication methods
- Authorization using Fauna built-in ABAC:
- Only the Admin role is authorized for API requests to create or update products, or update order status.
- The Customer role can only purchase products, and access his own list of orders.
- Sensitive purchase logic encapsulated server-side using Fauna's User-Defined Functions:
- All conditions (user has a high enough balance, product is still available)
- All actions (decrement product quantity, subtract product price from user balance, and create order document).
- Order status history is provided using Fauna's built-in temporality.
- You will need to have Python with Virtualenv and Git installed on your machine.
- I would recommend using Python 3, but Python 2 should work.
To install the project and get the development environment running:
- First, clone the application code into any directory on your disk:
$ cd /path/to/my/workspace/
$ git clone https://github.com/fauna-labs/fauna-shopapp-flask.git
$ cd faunadb-shop
- Create a virtual Python environment in a directory named
venv
, activate the virtualenv, and install required dependencies usingpip
:
$ virtualenv -p `which python3` venv
$ source venv/bin/activate
(venv) $ pip install -r requirements.txt
-
Rename
.env.sample
to.env
-
Sign up for free or log in at dashboard.fauna.com.
- Click [CREATE DATABASE], name it "shopapp", select a region group (e.g., "Classic"), and click [CREATE].
- Click the [SECURITY] tab at the bottom of the left sidebar, and [NEW KEY].
- Create a Key with the default Role of "Admin" selected, and paste the secret into into your
.env
file'sFAUNA_SECRET_ADMIN_KEY
. We'll use this key during setup to seed the database with schema and sample data. - Create another key, but this time select "Server" from the "Role" dropdown. Paste the secret into your
.env
file'sFAUNA_SECRET_SERVER_KEY
field. The application will use this key to access the database.
-
Run the script to initialize collections, indexes, roles, and seed some data.
python -m shop.fauna.init.seed
-
In your Fauna Dashboard, click the [Shell] tab in the left sidebar, and then [Open File] to run this command from file
shop/fauna/functions.py
-
Start the application
python -m shop.app
-
Open the Swagger documentation at http://localhost:8888/api/.
-
To test the requests via the Swagger UI, use any of the account credentials listed here.