Skip to content

sansagara/banistmo-backend

Repository files navigation

Banistmo Backend: A challenge

A Django App. Meant as a display of some backend programming skills. A Frontend App is also available here. This is already deployed to Heroku for convenience. It can be found on banistmo-back.herokuapp.com

An index of the whole set of resources is available on banistmo-back.herokuapp.com

Running Locally

Make sure you have:

$ git clone git@github.com:sansagara/banistmo.git
$ cd banistmo

$ pipenv --three
$ pipenv install
$ pipenv shell

$ createdb banistmo

$ python manage.py migrate
$ python manage.py collectstatic

$ python manage.py runserver

Your app should now be running on localhost:8000.

Deploying to Heroku

This project is already configured to be run on heroku easily. If you want to make your own deployment, follow these instructions:

  • Create a free Heroku Account
  • Install the Heroku Toolbelt for your system.
  • Login using the account details you used then creating your Heroku account.
$ heroku login
  • Prepare the app.
$ git clone git@github.com:sansagara/banistmo.git
$ cd banistmo
  • Deploy the app
$ heroku create
$ git push heroku master
  • Ensure that at least one instance of the app is running
heroku ps:scale web=1
https://elements.heroku.com/addons/heroku-postgresql
  • Migrate the database and open
$ heroku run python manage.py migrate
$ heroku open

or instead, click on this button to deploy automatically:

Deploy

Administrate

The Django Admin page can be used to make CRUD operations on Users and Transactions. The superadmin username and password for the Live deployment is Banistmo:Banistmo123

Dataset

This comes with a companion dataset. It can be inserted to your chosen database backend with the import_dummy_data management command. You can run it with python manage.py import_dummy_data.

About the Dataset

  • The identifier of the rows is zero-indexed.
  • The date may require parsing before being inserted into a DB engine.
  • The UUID must be an integer.
  • The TXN must be a signed decimal.
  • Records with a missing date should be inserted, but considered in the results.
  • The records with missing date must be shown to the end user in some way, without affecting aggregates or accounts.

For more info, view the R Notebook here

API

This is a RESTFul application made as the backend for a Single-page Application (SPA) or App. A companion app was also developed and is available here The API is done with [django-rest-framework] and [Djoser]. Each API endpoint is browsable, which means it can be checked on the browser to check its purpose and inspect results. Authentication is possible via Session (for the browsable API) and Token (For the SPA).

API Docs

The API docs can be found on the /transactions/docs route. On the already deployed application, it can be found here

Postman Collection

A postman collection is provided for convenience.

Run in Postman

Async API responses

One of the requirements for the challenge involved coming up with a solution for long-running API endpoints on an async manner. There are multiple ways to handle this scenario: task schedulers, threads, and queues. I went the queue route with Redis, as it is lightweight, open-source and easy to install and setup. This will allow to set a worker, who will dispatch API requests as server resources become available, using Redis queue system.

Install

You can install redis on your local system. However, a Redis Heroku addon is also available and you can use it even for your local installation. The Redis URL for the Live deployment in case you want to use it is: redis://h:pdd34a64d7788396ebae5316a9fad2c46e77c3bdca863b4706d3c8aa4a7119e31@ec2-34-239-85-133.compute-1.amazonaws.com:32939 The project's Procfile is set to automatically start workers on deployments.

Setup

Django-rq is used to simplify setup. Just specify the URL of the redis service on the RQ_QUEUES on settings.py

RQ_QUEUES = {
    'default': {
        'URL': os.getenv('REDIS_URL', 'redis://localhost:6379/0'), # R
        'DEFAULT_TIMEOUT': 500,
    },
}

Endpoints

  • To make a new request: /service2/enqueue/ (Live)
  • To poll for a response: /service2/poll (Live)

Administrate

The queues information can be seen from the Django Admin page, specifically on /queues. (Live).

Documentation

Django in general

Async API

About

Author

Leonel Atencio

Version

1.0

License

Do What The F*ck You Want To Public License (wtfpl)