|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +This is the **Panel** server and VM infrastructure for deploying Treasury nodes. It provides a "1-click" deployment solution that handles initial Treasury installation, pairing with other nodes, and backup/restore procedures through a built-in UI. The system is designed to run on VMs in cloud environments (AWS, GCP) with no SSH access or manual configuration required. |
| 8 | + |
| 9 | +The project consists of: |
| 10 | + |
| 11 | +- **Go-based Panel Server**: HTTP API server that manages Treasury lifecycle |
| 12 | +- **Next.js Web UI**: Static single-page application for activation and management |
| 13 | +- **Bootable Container Image**: VM images built as bootable containers for various cloud platforms |
| 14 | + |
| 15 | +## Development Commands |
| 16 | + |
| 17 | +### Go Backend |
| 18 | + |
| 19 | +```bash |
| 20 | +# Run panel server in development mode |
| 21 | +just dev |
| 22 | + |
| 23 | +# Install panel binary |
| 24 | +just install |
| 25 | + |
| 26 | +# Run tests |
| 27 | +just test |
| 28 | + |
| 29 | +# Run linter and formatter |
| 30 | +just lint |
| 31 | + |
| 32 | +# Tidy Go modules (GOWORK=off for this repo) |
| 33 | +just tidy |
| 34 | +``` |
| 35 | + |
| 36 | +### Web Frontend (Next.js) |
| 37 | + |
| 38 | +```bash |
| 39 | +cd web |
| 40 | + |
| 41 | +# Install dependencies |
| 42 | +pnpm install |
| 43 | + |
| 44 | +# Development server (hot reload on http://localhost:3000) |
| 45 | +pnpm run dev |
| 46 | + |
| 47 | +# Build static export |
| 48 | +pnpm run build |
| 49 | + |
| 50 | +# Run tests |
| 51 | +pnpm test |
| 52 | + |
| 53 | +# Run tests in watch mode |
| 54 | +pnpm run test:watch |
| 55 | +``` |
| 56 | + |
| 57 | +### Testing Individual Components |
| 58 | + |
| 59 | +```bash |
| 60 | +# Run a specific Go test |
| 61 | +GOWORK=off go test -v ./pkg/bak/... |
| 62 | + |
| 63 | +# Run a single test function |
| 64 | +GOWORK=off go test -v -run TestSpecificFunction ./server/panel/... |
| 65 | +``` |
| 66 | + |
| 67 | +### Docker/VM Development |
| 68 | + |
| 69 | +```bash |
| 70 | +# Build dev container image |
| 71 | +just build-dev |
| 72 | + |
| 73 | +# Run first dev VM instance |
| 74 | +just run-dev-1 |
| 75 | + |
| 76 | +# Run second dev VM instance (for testing multi-node setups) |
| 77 | +just run-dev-2 |
| 78 | + |
| 79 | +# Execute into running container |
| 80 | +just exec-1 # or just exec-2 |
| 81 | + |
| 82 | +# Run panel server inside container |
| 83 | +just dev-container n="1" |
| 84 | +``` |
| 85 | + |
| 86 | +## Architecture Overview |
| 87 | + |
| 88 | +### Panel Server (Go) |
| 89 | + |
| 90 | +The Panel server is a Fiber-based HTTP API server that orchestrates the Treasury deployment lifecycle: |
| 91 | + |
| 92 | +**Key Components:** |
| 93 | + |
| 94 | +- `cmd/panel/main.go`: CLI entrypoint with cobra commands (`start`, `activate`, `reset`, etc.) |
| 95 | +- `server/server.go`: Fiber app setup and server initialization |
| 96 | +- `server/endpoints/`: API endpoint handlers organized by domain (activate, treasury, backup, service, etc.) |
| 97 | +- `server/panel/`: Panel state management and persistence |
| 98 | +- `pkg/`: Reusable packages for admin client, secrets, paths, genesis, etc. |
| 99 | + |
| 100 | +**Activation Flow:** |
| 101 | + |
| 102 | +1. API key activation (`/v1/activate/api-key`) |
| 103 | +2. Binary installation (`/v1/activate/binaries`) |
| 104 | +3. Network setup via Netbird (`/v1/activate/network`) |
| 105 | +4. Backup key configuration (`/v1/activate/backup`) |
| 106 | +5. Treasury generation and peer synchronization |
| 107 | + |
| 108 | +**Service Management:** |
| 109 | +The panel manages systemd services (`treasury.service`, `start-treasury.service`) and can start/stop/restart them via the `/v1/services/*` endpoints. |
| 110 | + |
| 111 | +**Backup System:** |
| 112 | +Uses age encryption with BIP39 mnemonic-based keys. Backups are stored in S3-compatible storage and can be restored through the UI. |
| 113 | + |
| 114 | +### Web Frontend (Next.js + TypeScript) |
| 115 | + |
| 116 | +The web UI is a static Next.js application that exports to `web/out/` and is served by the Go server at the root path. |
| 117 | + |
| 118 | +**Key Components:** |
| 119 | + |
| 120 | +- `pages/index.tsx`: Main entry point with tab-based UI |
| 121 | +- `components/`: React components for each major feature |
| 122 | + - `ActivationPanel.tsx`: Step-by-step activation wizard |
| 123 | + - `BackupRestoreTab.tsx`: Backup management and restore |
| 124 | + - `StatusTab.tsx`: Treasury health and service status |
| 125 | + - `EncryptionAtRestTab.tsx`: Encryption-at-rest configuration |
| 126 | + - `InitialUsersTab.tsx`: User management |
| 127 | + - `TreasuryLogsTab.tsx`: Real-time logs viewer |
| 128 | + |
| 129 | +**API Communication:** |
| 130 | +The frontend calls the Panel server API at `NEXT_PUBLIC_API_HOST` (defaults to same host). During development, set this in `.env.local` to point to `http://localhost:7666` while running the Next.js dev server on port 3000. |
| 131 | + |
| 132 | +**Static Export:** |
| 133 | +The app uses `output: 'export'` in `next.config.js` to generate static HTML/JS/CSS files that are served by the Go server. |
| 134 | + |
| 135 | +### State Management |
| 136 | + |
| 137 | +**Panel State** (`/etc/panel/panel.json`): |
| 138 | + |
| 139 | +- API key and node configuration |
| 140 | +- Treasury ID and node ID (assigned during activation) |
| 141 | +- Backup keys (age public keys) |
| 142 | +- Encryption-at-rest secret reference |
| 143 | +- Connector and API node flags |
| 144 | + |
| 145 | +### VM and Container Architecture |
| 146 | + |
| 147 | +The project builds bootable container images using [bootc](https://docs.fedoraproject.org/en-US/bootc/): |
| 148 | + |
| 149 | +1. Base image built with platform-specific config (AWS/GCP) |
| 150 | +2. Panel binary and web UI embedded in the image |
| 151 | +3. Systemd services configured for automatic startup |
| 152 | +4. Container converted to VM image using `bootc-image-builder` |
| 153 | +5. VM image published to cloud provider marketplaces |
| 154 | + |
| 155 | +**Build Process:** |
| 156 | + |
| 157 | +```bash |
| 158 | +# Build for AWS |
| 159 | +BASE=aws docker buildx bake |
| 160 | + |
| 161 | +# Build for GCP |
| 162 | +BASE=gcp docker buildx bake |
| 163 | +``` |
| 164 | + |
| 165 | +The VM runs the panel server on port 7666 and the user accesses it through a port forward for initial activation. |
| 166 | + |
| 167 | +### GOWORK Environment |
| 168 | + |
| 169 | +This repository uses `GOWORK=off` for all Go commands because the Panel is intended to eventually be split into a separate repository from the main Treasury codebase. |
| 170 | + |
| 171 | +## API Endpoint Structure |
| 172 | + |
| 173 | +All activation endpoints follow a POST-based flow: |
| 174 | + |
| 175 | +- `/v1/activate/api-key` - Activates API key and fetches node assignment |
| 176 | +- `/v1/activate/binaries` - Downloads Treasury, Signer, and Cord binaries |
| 177 | +- `/v1/activate/network` - Joins Netbird network for peer connectivity |
| 178 | +- `/v1/activate/backup` - Configures backup keys |
| 179 | +- `/v1/activate/otel` - Configures observability collection |
| 180 | + |
| 181 | +Treasury management endpoints: |
| 182 | + |
| 183 | +- `POST /v1/treasury` - Generate new Treasury node |
| 184 | +- `POST /v1/treasury/complete` - Complete Treasury setup after peers are synced |
| 185 | +- `POST /v1/treasury/image` - Use specific Treasury container image |
| 186 | +- `DELETE /v1/treasury` - Delete Treasury node (with confirmation) |
| 187 | +- `GET /v1/treasury/healthy` - Health check with optional verbose output |
| 188 | + |
| 189 | +Service control endpoints: |
| 190 | + |
| 191 | +- `GET /v1/services` - List all systemd services |
| 192 | +- `POST /v1/services/{service}/{action}` - Perform action (start/stop/restart/enable/disable) |
| 193 | + |
| 194 | +Backup endpoints: |
| 195 | + |
| 196 | +- `POST /v1/backup/snapshot` - Create snapshot backup |
| 197 | +- `POST /v1/backup/restore` - Restore from snapshot |
| 198 | +- `GET /v1/backup/list` - List available backups |
| 199 | + |
| 200 | +## Testing Strategy |
| 201 | + |
| 202 | +**Go Tests:** |
| 203 | + |
| 204 | +- Unit tests in `*_test.go` files |
| 205 | +- Use testify for assertions |
| 206 | +- Mock systemd interactions where needed |
| 207 | +- Test secret loading with different providers |
| 208 | + |
| 209 | +**Frontend Tests:** |
| 210 | + |
| 211 | +- Vitest for component tests |
| 212 | +- React Testing Library for component interaction |
| 213 | +- `vitest.setup.ts` configures jsdom environment |
| 214 | + |
| 215 | +## Common Gotchas |
| 216 | + |
| 217 | +- Always use `GOWORK=off` when running Go commands |
| 218 | +- The panel server serves static files from `web/out/` - rebuild the frontend when making UI changes |
| 219 | +- The `--treasury-user` flag defaults to `cordial` - this is the Linux user that runs Treasury |
| 220 | +- Binary downloads are verified with Sigstore cosign signatures |
| 221 | +- Multi-node activation requires all nodes to exchange peer information before completing |
| 222 | +- The `start-treasury.service` keeps retrying if peer information isn't ready yet |
0 commit comments