# HTTP API

このページでは、API サーバーのエンドポイント、リクエスト形式、テーブルスキーマを示します。

## エンドポイント

API サーバーは `:8080` で待ち受け、成功時は文字列 `ok` または `{"data": [...]}` を返します。JSON バインディングのエラーは `400`、データベースのエラーは `500` を返し、どちらもエラーメッセージを文字列で返します。

| メソッド | パス | ボディ | 説明 |
|---|---|---|---|
| `GET` | `/api/health` | — | ヘルスチェック。`ok` を返します |
| `GET` | `/api/pod/list` | — | `dismiss = 0` のデプロイを一覧表示します |
| `POST` | `/api/pod/upsert` | `Pod` | `uid` でデプロイを挿入または更新し、`dismiss = 0` にリセットします |
| `POST` | `/api/pod/update/:uid` | `Pod`（`status`、`dismiss` のみ使用） | 指定したデプロイを更新します |
| `POST` | `/api/pod/record/insert` | `Record`（`uid`、`content`、`hostname`、`ip` を使用） | `uid` で操作記録を挿入します |

登録されていないパスは `NoRoute` に落ち、リクエストは応答を返さないまま保持されます。

## 例

```bash
curl -X POST localhost:8080/api/pod/upsert \
  -d '{"uid":"t1","pod_id":"p","pod_name":"n","local_dir":"/a","remote_dir":"/b","status":"starting","replicas":1}'

curl localhost:8080/api/pod/list
```

## Pod のフィールド

| フィールド | 型 | 説明 |
|---|---|---|
| `uid` | `string` | `md5("<MAC>@<local-absolute-path>")` または `-u` の値 |
| `pod_id` | `string` | Podman の Pod ID。取得に失敗した場合はリモートフォルダ名 |
| `pod_name` | `string` | Podman の Pod 名。取得に失敗した場合はリモートフォルダ名 |
| `local_dir` | `string` | ローカルのプロジェクトの絶対パス |
| `remote_dir` | `string` | `/home/podrun/<folder-name>_<first-8-hash-chars>` |
| `file` | `string` | `-f` で渡された compose ファイル |
| `target` | `string` | `--type` の値。**[Unfinished]** k3s は記録されるだけです |
| `status` | `string` | `up` の後は `starting`、`removePod()` の後は空 |
| `hostname` | `string` | CLI を実行したマシンのホスト名 |
| `ip` | `string` | CLI を実行したマシンの最初の非ループバック IPv4 |
| `replicas` | `int` | 常に `1` |
| `created_at` / `updated_at` | `time` | データベースが書き込みます |
| `dismiss` | `int` | `0` は有効、`1` は削除済み |

`pod_id` はテーブルの `pod_uid` カラムに保存されます。

## テーブル

| テーブル | 用途 | カラム |
|---|---|---|
| `pods` | デプロイごとに 1 行。`uid` と `pod_uid` は一意 | `uid`, `pod_uid`, `pod_name`, `local_dir`, `remote_dir`, `file`, `target`, `status`, `hostname`, `ip`, `replicas`, `created_at`, `updated_at`, `dismiss` |
| `records` | 操作ログ。`pod_id` は `pods.id` を参照します（削除時はカスケード） | `pod_id`, `content`, `hostname`, `ip` |
| `domains` | **[Unfinished]** `domain` コマンド用に予約されています | `pod_id`, `container_name`, `domain`, `created_at`, `updated_at`, `dismiss` |
