Skip to main content

Backend

codecov

Introduction

Integrates' backend is an HTTP web service written in Python, served with Hypercorn and built with Starlette.

Principles

  • Functional: The codebase favors functions instead of classes and avoids direct mutations.
  • No need to reinvent the wheel: Sometimes, third-party packages have already figured it out.

Getting started

To view the changes reflected as you edit the code, you can run:

  m . /integrates

Linting

The back uses Prospector and Mypy to enforce compliance with a defined coding style.

To view linting issues, you can run:

  m . /lintPython/dirOfModules/integrates/name of the module

To view and auto-fix formatting issues, you can run:

  m . /formatPython/default

Testing

The back uses pytest as a test runner.

To execute the unit tests on your computer, you can run:

  m . /integrates/back/test/unit 'changes_db'
  m . /integrates/back/test/unit 'not_changes_db'

To execute a functional test on your computer, you can run:

  m . /integrates/back/test/functional 'name of the test'

Core

The backend codebase consists of several modules, which are classified into three layers: API, business logic, and data access.

Web server

The back is served by Hypercorn, a program with two sides:

  • 🌐 On one side it speaks the HTTP protocol, receiving requests and sending responses.
  • 🐍 On the other it speaks Python (ASGI), passing the request data over to Python code for processing.

This is lower-level stuff that would be difficult to build an application directly on top of, so that's where Starlette comes in handy.

Starlette abstracts the handling of requests, and provides us with utilities that make it simple to build a web application, such as declaring routes, middlewares, working with sessions and managing cookies.

API layer

This layer provides an external interface for external systems or clients to interact with our services.

It is implemented as a GraphQL API, of which you can learn more here.

You can find it in the back/src/api directory.

Business logic layer

This layer implements the core functionality of the application, processing inputs and producing outputs, according to the specified business requirements.

You can find it across modules in files usually named domain.py.

Data access layer

This layer declares the structure of each entity used in the application and interacts with database-specific modules to read from and write to a data store.

It provides functions to perform CRUD operations for each entity, taking care of batching and caching with the use of dataloaders, and was designed to be agnostic so that the underlying data store can be swapped without causing major disruptions to the upper layers.

You can find it in the back/src/db_model, back/src/dynamodb and back/src/search directories.