# Fly.io

Fly runs the hub's image close to you (Singapore by default) with a small volume for its data. The
machine stops when nothing is connected, for example overnight, and starts again on the first
request in under a second.

You need the `fly` command ([install it](https://fly.io/docs/flyctl/install/)) and a Fly account.

```sh
mkdir openkedai-fly && cd openkedai-fly
curl -fsSLO https://raw.githubusercontent.com/OpenKedai/openkedai/main/deploy/fly.toml
fly apps create kedai-hub                  # any free name
# In fly.toml, set: app = "kedai-hub"
fly volumes create openkedai_data --region sin --size 1
fly deploy
fly logs | grep "Setup code"
```

Then open `https://kedai-hub.fly.dev` and choose **Set up a new shop**. The hub already knows its
address (from Fly), so pairing codes and QR codes use it.

## Settings

```sh
fly secrets set OPENKEDAI_BACKUP_S3_ACCESS_KEY_ID=… OPENKEDAI_BACKUP_S3_SECRET_ACCESS_KEY=…
```

Plain settings can go under `[env]` in `fly.toml`; see [Configuration](https://docs.openkedai.com/self-hosting/configuration/). Keep
the S3 keys in secrets.

## Updating

`fly deploy` again pulls the newest image. To stay on a version, set `image` in `fly.toml` to
`ghcr.io/openkedai/openkedai:0.9`.

## Good to know

- One machine only: the data is on its volume. Don't scale out.
- 256 MB of memory is enough for a shop. Add memory if you run plugins
  (`fly scale memory 512`).
- Fly snapshots volumes daily; the hub's own snapshots are on the same volume, so also turn on
  [copies to S3-compatible storage](https://docs.openkedai.com/self-hosting/backups/).
- To build from a clone of the repository instead of the published image:
  `fly deploy --config deploy/fly.toml --dockerfile deploy/Dockerfile` from the repository root.
