# genropy-asgi

GenroPy integration for genro-asgi. Provides `GenropyAsgiServer` — an AsgiServer
that knows how to create GenroPy database connections from config.

## Installation

```bash
pip install -e contrib/genropy_asgi
```

Requires GenroPy to be available in the Python environment.

## What it does

`GenropyAsgiServer` extends `AsgiServer` with one capability: reading
`genropy_app` entries from the `databases` config section.

For each entry:

```yaml
databases:
  default:
    genropy_app: "sourcerer"
```

It creates a `GnrApp("sourcerer")` and registers `gnr_app.db` in
the server's `db_registry` under the given name.

## Quick start

### Minimal (no YAML)

```python
from gnr.app.gnrapp import GnrApp
from genro_asgi import AsgiServer, OpenApiApplication
from my_project.api import MyAPI

gnr_app = GnrApp("myapp")
api = MyAPI(gnr_app.db)

server = AsgiServer()
app = OpenApiApplication(routing_class=api, docs="swagger")
server.mount("api", app)
server.run()
```

### With config.yaml (recommended)

```yaml
# config.yaml
server:
  host: "0.0.0.0"
  port: 8082

databases:
  default:
    genropy_app: "myapp"
```

```python
# run.py
from genropy_asgi import GenropyAsgiServer
from genro_asgi import OpenApiApplication
from my_project.api import MyAPI

server = GenropyAsgiServer(server_dir=".")
api = MyAPI(server.get_db("default"))
app = OpenApiApplication(routing_class=api, docs="swagger")
server.mount("api", app)
server.run()
```

```bash
cd my_server
python run.py
```

### Endpoints

- `/api/docs` — Swagger UI
- `/api/openapi` — OpenAPI 3.1 JSON schema
- `/api/api/...` — API endpoints from the routing class

## How the db flows

```
config.yaml databases.default.genropy_app: "myapp"
    ↓
GenropyAsgiServer creates GnrApp("myapp")
    ↓
server.register_db("default", gnr_app.db)
    ↓
server.get_db("default") → db connection
    ↓
MyAPI(db) uses it directly
    ↓
Sub-modules access it via self.db (DbRoutingClass chain)
```

## Future: fully declarative

When the loader supports automatic RoutingClass wrapping and db_name resolution:

```yaml
databases:
  default:
    genropy_app: "myapp"

apps:
  api:
    module: "my_project.api:MyAPI"
    db_name: default
    docs: swagger
```

```python
from genropy_asgi import GenropyAsgiServer
server = GenropyAsgiServer(server_dir=".")
server.run()
```

This mode is not yet implemented.
