Metadata-Version: 2.4
Name: sdcp
Version: 0.1.0
Summary: A simple framework for building SDCP servers
License: MIT
Project-URL: Homepage, https://mineflare.app
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: cryptography>=41.0.0
Dynamic: license-file

# SDCP

A simple Python framework for building SDCP (Secure Data Communication Protocol) servers, inspired by Flask.

## Installation

```bash
pip install sdcp
```

## Requirements

You need a running SDCP main authentication server. Each server must have a `server_keys.json` file containing its pre-shared key with the main server.

## Quick Start

```python
from sdcp import SDCP, Request, Response, html

app = SDCP(
    server_keys_file="server_keys.json",
    server_id="server_1",
    main_server_host="127.0.0.1",
    main_server_port=300,
)

@app.route("/")
def index(request: Request) -> Response:
    return html("<head><title>Home</title></head><body><h1>Hello!</h1></body>")

@app.route("/about")
def about(request: Request) -> Response:
    return html("<head><title>About</title></head><body><h1>About</h1></body>")

@app.not_found
def not_found(request: Request) -> Response:
    return html(f"<h1>404 - {request.path} not found</h1>")

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=299, debug=True)
```

## API Reference

### `SDCP(server_keys_file, server_id, main_server_host, main_server_port)`

Creates an SDCP app instance.

| Parameter | Default | Description |
|---|---|---|
| `server_keys_file` | `"server_keys.json"` | Path to JSON file containing this server's PSK |
| `server_id` | `"server_1"` | This server's ID, must match the key in `server_keys_file` |
| `main_server_host` | `"127.0.0.1"` | Hostname of the main auth server |
| `main_server_port` | `300` | Port of the main auth server |

### `@app.route(path)`

Registers a handler for a given path. Use `*` at the end for wildcard matching.

```python
@app.route("/blog/*")
def blog(request: Request) -> Response:
    return html(f"<h1>Blog post: {request.path}</h1>")
```

### `@app.not_found`

Registers a custom 404 handler.

### `Request`

| Attribute | Description |
|---|---|
| `request.path` | The path requested by the client |
| `request.client_id` | The authenticated client's ID |

### `html(content)`

Helper to return an HTML response.

### `app.run(host, port, debug)`

Starts the server.

| Parameter | Default | Description |
|---|---|---|
| `host` | `"0.0.0.0"` | Interface to bind to |
| `port` | `299` | Port to listen on |
| `debug` | `False` | Print connection logs |

## server_keys.json format

```json
{
  "server_1": "<base64url-encoded-key>"
}
```

Generate this using the SDCP `add_server` tool.
