|
2 | 2 |
|
3 | 3 | ## Overview |
4 | 4 |
|
5 | | -- Purpose: Dockerized ALTCHA challenge/verify microservice using Bun + Express. Provides `/challenge` and `/verify` used by the ALTCHA widget. Optional demo UI. |
6 | | -- Key libs: `altcha`, `altcha-lib`, `express@5`, `helmet`, `cors`, `dotenv`. |
7 | | -- Entrypoint: `src/index.ts` → transpiled to `build/index.js`. |
| 5 | +- Purpose: Dockerized ALTCHA challenge/verify microservice using Go + Echo. Provides `/challenge` and `/verify` used by the ALTCHA widget. Optional demo UI. |
| 6 | +- Key libs: `github.com/altcha-org/altcha-lib-go`, `github.com/labstack/echo/v4`, `github.com/joho/godotenv`. |
| 7 | +- Entrypoint: `cmd/server/main.go`. |
8 | 8 |
|
9 | 9 | ## Repo layout |
10 | 10 |
|
11 | | -- `src/index.ts`: Express server with two roles: |
12 | | - - API (always): `/`, `/challenge`, `/verify` on `PORT` (default 3000) |
13 | | - - Demo (when `DEMO=true`): simple site on port 8080 (`src/demo/index.html`) |
14 | | -- `Dockerfile`: multi-stage Bun build; copies `.env` and demo html into image; runs `bun start`. |
15 | | -- `compose.yaml`: exposes 3000 (API) and 8080 (demo); sets `SECRET` (default is placeholder), `NODE_ENV=production`. |
16 | | -- `package.json` scripts: `build` (tsc via Bun), `dev` (watch), `start` (run built server). |
| 11 | +- `cmd/server/main.go`: Entrypoint; loads .env, parses config, starts API and optional demo server. |
| 12 | +- `pkg/config/config.go`: Config struct and env-var parsing with defaults. |
| 13 | +- `pkg/handler/challenge.go`: `GET /challenge` handler. |
| 14 | +- `pkg/handler/verify.go`: `GET /verify` handler with in-memory record cache. |
| 15 | +- `pkg/handler/demo.go`: Demo page serving and proxy handlers. |
| 16 | +- `pkg/middleware/security.go`: CSP header middleware for demo server. |
| 17 | +- `pkg/server/server.go`: Echo server creation and route registration. |
| 18 | +- `web/demo/index.html`: Demo UI page. |
| 19 | +- `Dockerfile`: multi-stage Go build; copies `.env` and `web/` into image. |
| 20 | +- `compose.yaml`: exposes 3000 (API) and 8080 (demo); sets `SECRET` (default is placeholder). |
| 21 | +- `Makefile`: `build`, `run`, `dev`, `docker-build`, `docker-up`, `clean`, `lint`. |
17 | 22 |
|
18 | 23 | ## Build & run |
19 | 24 |
|
20 | | -- Local (Bun): |
21 | | - - PowerShell: `bun install; bun run build; bun start` |
22 | | - - Unix/macOS: `bun install && bun run build && bun start` |
| 25 | +- Local (Go): |
| 26 | + - `make build && make run` |
| 27 | + - Development: `make dev` |
23 | 28 | - Docker Compose: `docker compose up --build` |
24 | | - - Override secret once: |
| 29 | + - Override secret: |
25 | 30 | - PowerShell: `$env:SECRET = "<long-random>"; docker compose up --build` |
26 | 31 | - Unix: `SECRET="<long-random>" docker compose up --build` |
27 | 32 |
|
|
30 | 35 | - `SECRET` (required): HMAC key for ALTCHA. Default `$ecret.key` is unsafe; code logs a warning if used. |
31 | 36 | - `PORT`: API port (default 3000). |
32 | 37 | - `EXPIREMINUTES`: challenge expiry minutes (default 10). |
| 38 | +- `MAXNUMBER`: max number for PoW difficulty (default 1000000). |
33 | 39 | - `MAXRECORDS`: in-memory single-use token cache size (default 1000). |
34 | | -- `DEMO`: when `true`, serve demo on 8080 with CSP via Helmet. |
35 | | -- `.env` is loaded by `dotenv` at runtime; Dockerfile also copies `.env` into image. |
| 40 | +- `CORS_ORIGIN`: comma-separated allowed origins; defaults to `*` if unset. |
| 41 | +- `DEMO`: when `true`, serve demo on 8080 with CSP middleware. |
| 42 | +- `.env` is loaded by `godotenv` at runtime; Dockerfile also copies `.env` into image. |
36 | 43 |
|
37 | 44 | ## API contracts (keep stable) |
38 | 45 |
|
39 | 46 | - `GET /` → `204 No Content` (liveness). |
40 | | -- `GET /challenge` → `200 OK` JSON from `altcha-lib#createChallenge({ hmacKey: SECRET, expires })`. |
| 47 | +- `GET /health` → `200 OK` JSON with status, version, go runtime. |
| 48 | +- `GET /challenge` → `200 OK` JSON from `altcha.CreateChallenge()`. |
41 | 49 | - `GET /verify?altcha=<payload>` → `202 Accepted` on success, `417 Expectation Failed` on invalid or reused token. |
42 | 50 | - Reuse prevention uses an in-memory `recordCache` (size = `MAXRECORDS`); cache clears on restart/scaling. |
43 | | -- CORS is `*` for simplicity; demo sets strict CSP. |
| 51 | +- CORS defaults to `*`; configurable via `CORS_ORIGIN`. Demo uses strict CSP. |
44 | 52 |
|
45 | 53 | ## Patterns & conventions |
46 | 54 |
|
47 | | -- TypeScript strict mode; output in `build/` (`tsconfig.json` → `outDir`=`build`). |
48 | | -- Express 5 style middleware; minimal error handling by design (status-only API). |
| 55 | +- Standard Go project layout: `cmd/`, `pkg/`. |
| 56 | +- Echo framework for HTTP; minimal error handling by design (status-only API). |
49 | 57 | - Keep endpoints and status codes as-is to preserve client integrations and docs. |
50 | | -- When adding env vars or endpoints, update `README.md` and `.env.example`. |
| 58 | +- When adding env vars or endpoints, update `README.md`, `.env.example`, and this file. |
51 | 59 |
|
52 | 60 | ## CI/CD |
53 | 61 |
|
|
0 commit comments