Skip to content

Commit 63b9d15

Browse files
committed
♻️ Rewrite server from TypeScript/Express to Go/Echo
- Replace Bun+Express stack with Go+Echo framework - Add standard Go project layout (cmd/, pkg/) - Add /health endpoint with version and runtime info - Multi-stage Docker build (golang:1.24-alpine → alpine:3.21) - Add Makefile for build, run, dev, docker, clean, lint - Preserve all existing API endpoints and behavior
1 parent b591d06 commit 63b9d15

File tree

24 files changed

+523
-877
lines changed

24 files changed

+523
-877
lines changed

.dockerignore

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
# Include any files or directories that you don't want to be copied to your
2-
# container here (e.g., local build artifacts, temporary files, etc.).
3-
# For more help, visit the .dockerignore file reference guide at
4-
# https://docs.docker.com/go/build-context-dockerignore/
5-
6-
node_modules
1+
bin/
72
Dockerfile*
83
docker-compose*
94
.dockerignore
105
.git
116
.gitignore
7+
.github
8+
.claude
9+
.vscode
1210
README.md
1311
LICENSE
14-
.vscode
15-
.editorconfig

.github/copilot-instructions.md

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,31 @@
22

33
## Overview
44

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`.
88

99
## Repo layout
1010

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`.
1722

1823
## Build & run
1924

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`
2328
- Docker Compose: `docker compose up --build`
24-
- Override secret once:
29+
- Override secret:
2530
- PowerShell: `$env:SECRET = "<long-random>"; docker compose up --build`
2631
- Unix: `SECRET="<long-random>" docker compose up --build`
2732

@@ -30,24 +35,27 @@
3035
- `SECRET` (required): HMAC key for ALTCHA. Default `$ecret.key` is unsafe; code logs a warning if used.
3136
- `PORT`: API port (default 3000).
3237
- `EXPIREMINUTES`: challenge expiry minutes (default 10).
38+
- `MAXNUMBER`: max number for PoW difficulty (default 1000000).
3339
- `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.
3643

3744
## API contracts (keep stable)
3845

3946
- `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()`.
4149
- `GET /verify?altcha=<payload>``202 Accepted` on success, `417 Expectation Failed` on invalid or reused token.
4250
- 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.
4452

4553
## Patterns & conventions
4654

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).
4957
- 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.
5159

5260
## CI/CD
5361

.gitignore

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
.yarn/sdks
2-
.yarn/unplugged
3-
build/
4-
node_modules/
1+
bin/
2+
.env

.vscode/extensions.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

Dockerfile

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,15 @@
1-
# syntax=docker/dockerfile:1
2-
3-
FROM oven/bun:1 AS base
4-
5-
WORKDIR /usr/src/app
6-
7-
FROM base AS build
8-
9-
RUN --mount=type=bind,source=package.json,target=package.json \
10-
--mount=type=bind,source=bun.lock,target=bun.lock \
11-
bun i --frozen-lockfile
12-
1+
FROM golang:1.24-alpine AS build
2+
WORKDIR /app
3+
COPY go.mod go.sum ./
4+
RUN go mod download
135
COPY . .
14-
RUN bun run build
15-
16-
FROM base AS final
17-
18-
ENV NODE_ENV=production
19-
USER bun
20-
COPY package.json .
21-
COPY .env .
22-
COPY src/demo/index.html ./build/demo/index.html
23-
COPY --from=build /usr/src/app/node_modules ./node_modules
24-
COPY --from=build /usr/src/app/build ./build
25-
6+
ARG VERSION=dev
7+
RUN CGO_ENABLED=0 go build -ldflags "-X altcha/pkg/handler.Version=${VERSION}" -o /server ./cmd/server
8+
9+
FROM alpine:3.21
10+
RUN apk add --no-cache ca-certificates
11+
COPY --from=build /server /server
12+
COPY .env .env
13+
COPY web/ web/
2614
EXPOSE 3000
27-
28-
CMD ["bun", "start"]
15+
CMD ["/server"]

Makefile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
VERSION ?= dev
2+
3+
.PHONY: build run dev docker-build docker-up clean lint
4+
5+
build:
6+
go build -ldflags "-X altcha/pkg/handler.Version=$(VERSION)" -o bin/server ./cmd/server
7+
8+
run: build
9+
./bin/server
10+
11+
dev:
12+
go run ./cmd/server
13+
14+
docker-build:
15+
docker compose build
16+
17+
docker-up:
18+
docker compose up --build
19+
20+
clean:
21+
rm -rf bin/
22+
23+
lint:
24+
golangci-lint run ./...

README.Docker.md

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)