Metadata-Version: 2.5
Name: django-neolith
Version: 0.1.0a2
Summary: Deploy Django apps, not Django projects.
Project-URL: Repository, https://github.com/stack314/django-neolith
Project-URL: Issues, https://github.com/stack314/django-neolith/issues
Project-URL: Changelog, https://github.com/stack314/django-neolith/blob/main/CHANGELOG.md
Author: Vignesh Pai and django-neolith contributors
License-Expression: BSD-3-Clause
License-File: LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: Framework :: Django
Classifier: Framework :: Django :: 5.2
Classifier: Framework :: Django :: 6.1
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Typing :: Typed
Requires-Python: >=3.12
Requires-Dist: django>=5.2
Requires-Dist: pydantic<3,>=2.12
Description-Content-Type: text/markdown

<div align="center">

<h1>
<picture>
  <source media="(prefers-color-scheme: dark)" srcset="docs/assets/neolith-wordmark-dark.svg">
  <source media="(prefers-color-scheme: light)" srcset="docs/assets/neolith-wordmark-light.svg">
  <img alt="django-neolith" src="https://raw.githubusercontent.com/stack314/django-neolith/main/docs/assets/neolith-wordmark-light.svg" width="360">
</picture>
</h1>

<p><strong>Deploy Django apps, not Django projects.</strong></p>

<p>
<a href="https://github.com/stack314/django-neolith/actions/workflows/ci.yml?query=branch%3Amain"><img alt="CI" src="https://img.shields.io/github/actions/workflow/status/stack314/django-neolith/ci.yml?branch=main&label=CI"></a>
<a href="https://pypi.org/project/django-neolith/"><img alt="PyPI version" src="https://img.shields.io/pypi/v/django-neolith"></a>
<a href="https://pypi.org/project/django-neolith/"><img alt="Python versions" src="https://img.shields.io/pypi/pyversions/django-neolith"></a>
<a href="https://pypi.org/project/django-neolith/"><img alt="Django versions" src="https://img.shields.io/pypi/frameworkversions/django/django-neolith"></a>
<a href="https://github.com/stack314/django-neolith/blob/main/LICENSE"><img alt="License: BSD-3-Clause" src="https://img.shields.io/badge/license-BSD--3--Clause-blue"></a>
<a href="https://github.com/astral-sh/ruff"><img alt="Code style: Ruff" src="https://img.shields.io/badge/code%20style-ruff-261230"></a>
<a href="https://mypy-lang.org/"><img alt="Checked with mypy" src="https://img.shields.io/badge/mypy-checked-2a6db2"></a>
</p>

</div>

**Status: alpha (0.1.0a2). The API may change before 0.1.**

```
pip install --pre django-neolith
```

## What it is

Neolith makes the Django app, not the Django project, the unit of deployment. An app can run inside a shared process or on its own as a service, with its models, migrations, admin and tests intact, and without changing its code. Apps talk to each other only through typed contracts, so the same code and the same tests hold in both arrangements. Neolith is defined by a [specification](https://github.com/stack314/django-neolith/blob/main/spec/README.md) that is independent of Django and of Python, and django-neolith is its reference implementation.

## Features

What this version does. The [changelog](https://github.com/stack314/django-neolith/blob/main/CHANGELOG.md) has the details.

- **Typed contracts.** A contract is a `Protocol` class with `@contract` and `@action`, and an implementation is marked with `@implements`. Request and reply types are Pydantic models in a portable type system, and every check runs when the module is imported.
- **Typed errors.** `ContractError` carries a `kind`, a message and a detail. An action can declare its own error kinds with their detail records.
- **Calls cross as JSON, in-process too.** Every request, reply and error detail is written and read through its declared type, so an in-process call carries exactly what a remote one will.
- **App declaration.** An app declares what it provides, requires and owns on its `NeolithAppConfig`, and the declaration is checked when the class is defined.
- **Checks before serving.** At start-up the host resolves every required contract to a provider and validates each implementation, listing every refusal in one error; once those pass, it runs its system checks on the settings and reports all their errors together.
- **One database per app.** `neolith.routers.AppRouter` keeps each app's data on a database alias of its own.
- **Call context.** A principal, a trace, a deadline and the calling app travel with each call. A root is opened for each request by `RootMiddleware`, and for a command or background work by `command_scope`. A request's principal comes from the backends listed in `NEOLITH["PRINCIPAL_BACKENDS"]`, such as `neolith.principals.DjangoUserBackend` for the logged-in Django user.
- **Request principal.** Ordered principal backends, such as `neolith.principals.DjangoUserBackend`, name who a request is for, and `PrincipalMiddleware` fixes that principal for the request's calls.
- **One app alone.** `neolith run <app>` boots apps from their declarations and serves them, and `neolith migrate <app>` applies each app's migrations to its own database.

Not yet in this version: the HTTP transport, the conformance suite and events. See the roadmap below.

## A short example

A contract, in a contract package other apps may import:

```python
# inventory/contracts.py
from typing import Protocol

from pydantic import BaseModel

from neolith import action, contract


class ReserveRequest(BaseModel):
    sku: str
    qty: int


class ReserveReply(BaseModel):
    reserved: int


@contract("inventory.v1")
class Inventory(Protocol):
    @action(idempotent=True)
    def reserve(self, request: ReserveRequest) -> ReserveReply: ...
```

Its implementation, private to the app that provides it:

```python
# inventory/services.py
from neolith import CallContext, implements

from inventory.contracts import Inventory, ReserveReply, ReserveRequest


@implements(Inventory)
class InventoryService:
    def reserve(self, request: ReserveRequest, ctx: CallContext) -> ReserveReply:
        return ReserveReply(reserved=request.qty)
```

The two apps' declarations:

```python
# inventory/apps.py
from neolith import NeolithAppConfig, Provided

from inventory.contracts import Inventory


class InventoryConfig(NeolithAppConfig):
    name = "inventory"
    label = "inventory"
    provides = (
        Provided(
            Inventory,
            implementation="inventory.services.InventoryService",
            principal_from=("shop",),  # apps that may pass a principal on their calls
        ),
    )


# shop/apps.py
from neolith import NeolithAppConfig

from inventory.contracts import Inventory


class ShopConfig(NeolithAppConfig):
    name = "shop"
    label = "shop"
    requires = (Inventory,)
```

And a call from the shop app, which knows the contract and nothing else of inventory:

```python
# shop/orders.py
from neolith import ContractError, connect

from inventory.contracts import ReserveRequest

inventory = connect("inventory.v1")


def place_order(sku: str, qty: int) -> int:
    try:
        reply = inventory.reserve(ReserveRequest(sku=sku, qty=qty))
    except ContractError as error:
        print(error.kind)  # "unavailable", "conflict", ...
        raise
    return reply.reserved
```

The settings, beside the project's usual ones such as `SECRET_KEY`. They end with the host's builder, which builds `INSTALLED_APPS`, `DATABASES` and `DATABASE_ROUTERS` from the apps' declarations and `NEOLITH`, and checks the rest before the host serves:

```python
# settings.py
from neolith.host import build_settings

MIDDLEWARE = [
    "neolith.middleware.RootMiddleware",  # first, and listed once
    "neolith.middleware.PrincipalMiddleware",  # listed once
]
NEOLITH = {}

globals().update(build_settings(["inventory", "shop"], globals()))
```

With these, a view in the shop app can call `place_order` directly; a command opens its root first, with `neolith.command_scope("shop")`. A test runs every Python block on this page under these settings.

To run apps without a project, put the deployer's names (`SECRET_KEY`, `MIDDLEWARE`, `ROOT_URLCONF`, `NEOLITH` and the rest) in a module that does not call the builder, then apply the migrations and serve:

```console
$ neolith migrate inventory shop --settings deploy
$ neolith run inventory shop --settings deploy --bind 127.0.0.1:8000
```

`neolith run` uses Django's development server, which is not for production; in production, serve a settings module like the one above with any WSGI or ASGI server. On `SIGINT` or `SIGTERM` it stops taking requests and gives each request in flight until `NEOLITH["REQUEST_DEADLINE"]`, or 30 seconds when that is `None`. Under another server, give its graceful shutdown a timeout no shorter than the request deadline. In a host built by a settings module, `manage.py migrateapps` is the migrate step, and `manage.py migrateapps --check` reports what the host would refuse at start-up.

## Roadmap

The steps to 0.1 are in the [roadmap](https://github.com/stack314/django-neolith/blob/main/docs/roadmap.md).

## Links

- [Vision](https://github.com/stack314/django-neolith/blob/main/VISION.md)
- [Specification](https://github.com/stack314/django-neolith/blob/main/spec/README.md) (draft 0.1)
- [Roadmap to 0.1](https://github.com/stack314/django-neolith/blob/main/docs/roadmap.md)
- [Guiding principles](https://github.com/stack314/django-neolith/blob/main/docs/principles.md)
- [Architecture decision records](https://github.com/stack314/django-neolith/blob/main/docs/adr/README.md)
- [Open questions](https://github.com/stack314/django-neolith/blob/main/docs/open-questions.md)
- [Contributing](https://github.com/stack314/django-neolith/blob/main/CONTRIBUTING.md)
- [Code of conduct](https://github.com/stack314/django-neolith/blob/main/CODE_OF_CONDUCT.md)

Licensed under the [BSD 3-Clause License](https://github.com/stack314/django-neolith/blob/main/LICENSE).
