Files
Glimmer/AGENTS.md
T
2026-05-22 01:15:56 -07:00

198 lines
9.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Glimmer — Codex Guide
## Project Overview
**Glimmer** is a single-binary Go URL shortener + pastebin with image and file upload support. It runs on a Raspberry Pi Zero (512 MB RAM, ARMv5) and compiles to a fully self-contained binary — no CGo, no external runtime, no system SQLite.
Live at [luci.ooo](https://luci.ooo).
---
## Build Commands
```bash
make build # Windows → glimmer.exe
make build-linux # Linux x86-64 → glimmer
make build-arm # RPi Zero (ARMv5) → glimmer-arm
make run # Build + run on Windows
make clean # Remove all compiled binaries
make hash # Interactively hash a password for config.toml
```
Always run `go build ./...` (or `make build`) after any Go changes to confirm the project compiles before finishing a task.
---
## Project Structure
```
glimmer/
├── main.go # Entry point; --hash-password flag
├── config.toml # Runtime config (not embedded — loaded at startup)
├── go.mod / go.sum
├── Makefile
├── data/
│ ├── urls.db # SQLite DB (auto-created)
│ └── uploads/ # Uploaded images & files (auto-created)
└── internal/
├── config/config.go # TOML config structs + loader
├── db/db.go # DB open/migrate; Link + Paste + Upload CRUD
├── slug/slug.go # Base-36 slug generation + validation
└── server/
├── server.go # Mux, middleware wiring, server start
├── handlers.go # All HTTP handlers + rendering helpers
├── middleware.go # Auth, CSRF, sessions, rate limiter
├── templates.go # embed.FS for templates + static; initTemplates()
├── static/
│ ├── style.css # Custom styles on top of Pico CSS 2
│ └── upload.js # Shared client-side upload helper (window.GlimmerUpload)
└── templates/
├── index.html # Public: URL shortener form
├── admin_login.html # Login (dark, centered)
├── admin.html # Dashboard: stats, tiles, links tables
├── admin_edit.html # Edit a single link
├── admin_bin.html # Pastes list
├── admin_bin_edit.html # Create / edit paste (uses /static/upload.js)
├── admin_uploads.html # Image + file upload management
├── bin_view.html # Public paste viewer
├── bin_token.html # Token prompt for protected pastes
└── 404.html
```
---
## Architecture
### Single package per layer
- `internal/config` — pure config loading, no HTTP
- `internal/db` — pure database, no HTTP
- `internal/slug` — pure slug logic, no HTTP
- `internal/server` — everything HTTP: mux, handlers, middleware, templates
### All handlers on `*Server`
```go
type Server struct {
cfg *config.Config
db *db.DB
sessions *sessionStore
limiter *rateLimiter
}
```
### pageData is the single template context struct
All templates receive a `pageData` struct. Add new fields there when templates need new data — never pass raw types directly.
### Templates are embedded, not on disk
Templates and static files are embedded into the binary via `//go:embed` in `templates.go`. Changes to templates are compiled in; they are **not** reloaded at runtime. Always rebuild after template changes.
### No JS frameworks
All JavaScript is vanilla ES5-style (`var`, not `const`/`let`). Most JS is inline in templates; shared upload logic lives in `static/upload.js` and is exposed as `window.GlimmerUpload`. No build step, no npm, no bundler.
### CSS framework
[Pico CSS 2](https://picocss.com/) loaded from CDN. Custom overrides in `static/style.css`. Admin pages use `data-theme="dark"`.
---
## Key Patterns
### Adding a new admin route
1. Write the handler method on `*Server` in `handlers.go`
2. Register the route in `server.go` `Start()`, wrapped with middleware:
- Read routes: `s.requireAuth(s.handleFoo)`
- Write routes: `s.requireAuth(s.requireCSRF(s.handleFoo))`
3. Add the route to the URL routes table in `README.md`
### CSRF
All state-changing POSTs must include `<input type="hidden" name="csrf_token" value="{{$.CSRFToken}}">` in the form. Handlers must be wrapped with `requireCSRF`. For multipart form handlers (file uploads), verify CSRF manually inside the handler *after* calling `r.ParseMultipartForm` — do not use the `requireCSRF` wrapper as it will parse with Go's default 32 MB limit before `MaxBytesReader` takes effect.
### File uploads
Two endpoints, both admin-only and CSRF-protected:
- `POST /admin/upload` — images only (PNG/JPEG/GIF/WebP), up to `upload.max_size_mb` from config (default 50 MB). MIME type validated by magic bytes via `http.DetectContentType`.
- `POST /admin/upload-file` — any file type, hard-coded 5 MB cap. Validates only that the extension is `[a-z0-9]{1,10}`.
Shared upload rules:
- Max size enforced via `http.MaxBytesReader(w, r.Body, maxBytes)` at handler start
- Filenames written to disk are always `[32 hex chars].[ext]` — validated by `validUploadRe` regexp before any file operation. Original filenames are stored in the `uploads` table (filename → original_name) and used for `Content-Disposition` and the admin listing.
- Per-request timeouts extended to 120s via `http.NewResponseController` — global timeouts stay at 5s/10s
- `sanitizeDisplayFilename()` strips control chars and markdown-significant chars (`[ ] ( ) \ \` * < > " '`) before the original name is embedded in JSON responses or stored.
### Serving uploads (`/uploads/{filename}`)
`handleUploadsServe` validates the filename against `validUploadRe`, then:
- **Images** (`.png`/`.jpg`/`.gif`/`.webp`) — served inline by `http.ServeFile` so they can embed in public pastes.
- **Everything else** — forced to download with `Content-Type: application/octet-stream` + `Content-Disposition: attachment` (with both ASCII `filename=` and RFC 5987 `filename*=UTF-8''…`). This prevents same-origin XSS from uploaded HTML/SVG/JS being rendered by the browser.
### Image resize
Only PNG (`.png`) and JPEG (`.jpg`) are resizable. Resize uses a nearest-neighbour scale implemented in stdlib (`image`, `image/color`, `image/jpeg`, `image/png`) — no external image library. GIF and WebP are served and deletable but not resizable.
### Admin tabs
Every admin page (`admin.html`, `admin_bin.html`, `admin_bin_edit.html`, `admin_uploads.html`) must include the full three-tab nav. Add the `active` class to the correct tab:
```html
<div class="admin-tabs">
<a href="/admin" class="tab-link [active]">Links</a>
<a href="/admin/bin" class="tab-link [active]">Pastes</a>
<a href="/admin/uploads" class="tab-link [active]">Uploads</a>
</div>
```
### Database migrations
Migrations run automatically in `db.Open()`. New columns are added with `ALTER TABLE ... ADD COLUMN IF NOT EXISTS`. Never drop columns — the schema must remain backwards compatible with existing `data/urls.db` files on deployed instances.
### Paste name / token validation
Both paste names and access tokens must pass `validPasteName()`: alphanumeric + hyphens + underscores, 1–64 chars.
### lowercasePath middleware
Redirects uppercase paths to lowercase (for QR-code URL compatibility). **Excludes** `/static/`, `/admin`, and `/uploads/` — these prefixes must stay in the exclusion list if new case-sensitive routes are added.
---
## Configuration (`config.toml`)
```toml
[server]
port = 8888
base_url = "http://localhost:8888"
read_timeout = "5s"
write_timeout = "10s"
[admin]
username = "admin"
password_hash = "$2a$10$..." # bcrypt; generate with: ./glimmer --hash-password
session_hours = 24
[database]
path = "./data/urls.db"
[slugs]
length = 3
[upload]
dir = "./data/uploads"
max_size_mb = 50
```
All fields have safe defaults in `config.Load()` — the only required field is `admin.password_hash`.
---
## Dependencies
| Package | Purpose |
|---|---|
| `github.com/BurntSushi/toml` | Config parsing |
| `golang.org/x/crypto/bcrypt` | Password hashing |
| `modernc.org/sqlite` | Pure-Go SQLite (no CGo) |
| `github.com/yuin/goldmark` | Markdown rendering (GFM + tables) |
| `rsc.io/qr` | QR code generation (SVG) |
Everything else in `go.sum` is a transitive dependency. **Do not add new external dependencies** without strong justification — the project is intentionally minimal for RPi Zero deployment.
---
## Deployment Target
The primary deployment target is a **Raspberry Pi Zero** (ARMv5, 512 MB RAM). Keep this in mind:
- Avoid memory-heavy operations (e.g. loading many large images into RAM at once)
- Image resize uses nearest-neighbour (fast, low memory) rather than bicubic
- The binary embeds all templates and static assets — no deployment of separate files needed beyond the binary and `config.toml`
- The `data/` directory (SQLite DB + uploads) lives alongside the binary and must persist across restarts