# Deploying OAuthMC

OAuthMC compiles to a static Linux musl binary inside a non-root `gcr.io/distroless/static-debian12:nonroot` container image. It needs no database or persistent volume: tickets live in memory and expire after a few minutes.

## Container image

The GHCR image (`ghcr.io/yellowumbrelladev/oauthmc`) is not published yet; it ships with the first stable release. Until then, build the image locally from `crates/oauthmc`:

```sh
docker build -t oauthmc crates/oauthmc
```

```text
 => => naming to docker.io/library/oauthmc:latest
```

Once published, replace `build: ./crates/oauthmc` / `image: oauthmc` in the examples below with `ghcr.io/yellowumbrelladev/oauthmc:<version>` (pin a version tag such as `0.1.0`, not `:latest`).

## Docker Compose

Publish the Minecraft port (`25565`) publicly and keep the HTTP API (`8080`) on an internal network reachable only by your platform backend:

```yaml
services:
  oauthmc:
    image: oauthmc
    build: ./crates/oauthmc
    restart: unless-stopped
    ports:
      - "25565:25565"
    environment:
      OAUTHMC_API_KEYS: "${OAUTHMC_API_KEY}"
      OAUTHMC_PUBLIC_ADDRESS: "verify.example.com"
    networks:
      - internal

networks:
  internal:
    driver: bridge
```

Inside the Docker image, `OAUTHMC_API_ADDR` defaults to `0.0.0.0:8080` so sibling containers on `internal` can reach `http://oauthmc:8080`, while port `8080` is never published on the host.

Start the service and inspect its log:

```sh
docker compose up -d oauthmc
docker compose logs oauthmc
```

```text
level=info event=listening mc_addr=0.0.0.0:25565 api_addr=0.0.0.0:8080
```

## Non-default port with a DNS SRV record

If a Minecraft game server already uses port `25565` on the host, publish OAuthMC on another port (for example `25566`) and add a DNS `SRV` record so players still connect to `verify.example.com` without typing a port:

```text
_minecraft._tcp.verify.example.com. 300 IN SRV 0 5 25566 verify-host.example.com.
```

Set `OAUTHMC_PUBLIC_ADDRESS=verify.example.com`. Minecraft Java Edition resolves `_minecraft._tcp.<host>` automatically.

## Running behind a TCP proxy

OAuthMC enforces per-IP concurrency (`OAUTHMC_MAX_CONNECTIONS_PER_IP`, default `16`), per-IP held link sessions (`OAUTHMC_MAX_HELD_PER_IP`, default `2`), and the per-IP auth rate limit (`OAUTHMC_AUTH_RATE_PER_MINUTE`, `OAUTHMC_AUTH_BURST`) using the TCP peer address returned by `accept()`.

OAuthMC does not parse PROXY protocol headers. If you run it behind a SNATing L4 proxy or load balancer (such as HAProxy, Nginx stream, or a cloud TCP load balancer), every connection arrives from the proxy's IP address instead of the player's IP address. When running behind a SNATing proxy:

- Raise `OAUTHMC_MAX_CONNECTIONS_PER_IP`, `OAUTHMC_MAX_HELD_PER_IP`, `OAUTHMC_AUTH_RATE_PER_MINUTE`, and `OAUTHMC_AUTH_BURST` so legitimate players do not trip the shared per-IP caps.
- Enforce per-client connection and rate limits at the proxy layer.

## Health checks

The container image defines a built-in `HEALTHCHECK` that runs every 30 seconds:

```sh
/oauthmc healthcheck
```

The `healthcheck` subcommand reads `OAUTHMC_API_ADDR` (rewriting `0.0.0.0` to `127.0.0.1` and `::` to `::1`), sends `GET http://<addr>/healthz` with a 3-second timeout, and exits `0` on HTTP `200` or `1` otherwise.

Verify it on a running container:

```sh
docker exec oauthmc /oauthmc healthcheck && echo "healthy"
```

```text
healthy
```

External orchestrators on the internal network can also probe `GET /healthz` directly without an API key:

```sh
curl -sS http://127.0.0.1:8080/healthz
```

```json
{"ok":true}
```

## Upgrades

OAuthMC keeps no state on disk. Upgrading replaces the container image and restarts the process:

1. Rebuild the image (or pull the new version tag once published on GHCR):

   ```sh
   docker compose build oauthmc
   ```

2. Recreate the container:

   ```sh
   docker compose up -d oauthmc
   ```

3. Verify the container is listening and healthy:

   ```sh
   docker compose exec oauthmc /oauthmc healthcheck && echo "healthy"
   ```

   ```text
   healthy
   ```

Because tickets live in memory, any open ticket is dropped during the restart. Players whose ticket disappears receive `404 not_found` on the platform and start a fresh verification.

## Graceful shutdown

OAuthMC listens for `SIGTERM` and `SIGINT`. On receiving either signal, it:

1. Logs `level=info event=shutdown`.
2. Stops accepting new Minecraft and HTTP API connections and stops the ticket-expiry sweep loop.
3. Sends Configuration Disconnect (`0x02`) with the `shutdown` template to held link-mode clients.
4. Exits with status `0`.
