Skip to content
This repository has been archived by the owner on Jul 15, 2024. It is now read-only.

fauna-labs/fauna-shopapp-flask

Repository files navigation

Fauna Labs

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.

Sample shop API using Fauna, Python, and Flask

Table of Contents

Overview

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.

Prerequisites

  • 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.

Installation

To install the project and get the development environment running:

  1. 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
  1. Create a virtual Python environment in a directory named venv, activate the virtualenv, and install required dependencies using pip:
$ virtualenv -p `which python3` venv
$ source venv/bin/activate
(venv) $ pip install -r requirements.txt
  1. Rename .env.sample to .env

  2. 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's FAUNA_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's FAUNA_SECRET_SERVER_KEY field. The application will use this key to access the database.
  3. Run the script to initialize collections, indexes, roles, and seed some data.

python -m shop.fauna.init.seed
  1. 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

  2. Start the application

python -m shop.app
  1. Open the Swagger documentation at http://localhost:8888/api/.

  2. To test the requests via the Swagger UI, use any of the account credentials listed here.

About

No description, website, or topics provided.

Resources

License

MIT-0, Unknown licenses found

Licenses found

MIT-0
LICENSE
Unknown
LICENSE.txt

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages