refactor(main): extract route handlers and adopt Hono for dispatch #104
No reviewers
Labels
No labels
area:agents
area:dashboard
area:database
area:design
area:design-review
area:flows
area:infra
area:meta
area:security
area:sessions
area:webhook
area:workdir
security
type:bug
type:chore
type:meta
type:user-story
No milestone
No project
No assignees
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
charles/claude-hooks!104
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "refactor/main-route-handlers"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Two-step refactor in one branch:
handleRequest's 14 routes into its own named handler function (commit 1).handleRequestdrops from a 282-line if-else to a thin dispatcher.c.req.param("id")instead ofurl.pathname.slice(…), method dispatch + 404 are framework-provided, and the route table is oneapp.get/postper route.Zero behavior change. Public entry (
handleRequest(req)) andBun.servewiring stay identical; tests need no updates.Why now
The Agent pool milestone (#53) adds dashboard CRUD for agent instances —
GET /agents,POST /agents,PUT /agents/:name,DELETE /agents/:name, pagination, validation. Doing that on top of the hand-rolled if-else would hurt. Switching before those routes land means they come in idiomatic Hono from day one.Dependencies
hono— only addition. 184 ms install on Bun; single package (no transitive bloat to flag).Shape (post-Hono)
What stays stable
handleRequest(req)exported with the same signature —main.test.ts(20 tests through this entry) passes unchanged;Bun.serve({ fetch: handleRequest })works.Response. Hono is only the dispatcher; handler bodies are portable if we ever swap back.handleForgejoWebhook(c.req.raw)— the raw native Request flows through untouched, so signature verification (which readsreq.text()) still works.Handler signature cleanups
Now that Hono extracts path params:
handleTaskGet(url: URL)→handleTaskGet(id: string)handleHistoryById(url: URL)→handleHistoryById(id: string)handleIcons(url: URL)→handleIcons(filename: string)Test plan
just qa— 313 pass, 0 fail across both commitsmain.test.ts(20 tests covering/health,/queue,/task,/cancel, unknown routes) passes via the unchangedhandleRequestentrywebhook.test.ts(42 tests) — direct calls tohandleForgejoWebhook, unchanged🤖 Generated with Claude Code
refactor(main): extract HTTP route handlers from handleRequestto refactor(main): extract route handlers and adopt Hono for dispatchReview: refactor(main) — extract route handlers + adopt Hono
CI: ✅ Green (run #1644, 3m45s, all 313 tests pass)
Summary
This is a clean, focused refactor. I reviewed the full
src/main.tsdiff, thepackage.jsonchange, and thebun.lockupdate. Everything checks out.Correctness
handleForgejoWebhookreceivesc.req.raw— the nativeRequestis passed through untouched, so HMAC signature verification (which callsreq.text()) is unaffected. ✅c.req.param("id")) replaces the previousurl.pathname.slice(…)slicing — cleaner and less error-prone. ✅handleIconsreceives a hardcoded string literal from its closure ("icon.svg","icon-maskable.svg"), NOT a path param — no path-traversal surface. ✅export const handleRequest = (req) => Promise.resolve(app.fetch(req))correctly narrows Hono's(req, env?, ctx?) => Response | Promise<Response>signature to the(req) => Promise<Response>shape thatBun.serveandmain.test.tsexpect. ✅TaskRequest,TaskResult,WorkerConfig) preserved — existing consumers likewebhook.tsstay source-compatible. ✅Dependency
hono@^4.12.14is the only new dependency. No transitive bloat. The API usage (app.get/post,c.req.raw,c.req.param,c.json,app.notFound) is stable public Hono API. ✅Scope
main.test.ts+ 42webhook.test.ts) passing without modification. No scope creep — handler bodies are unchanged. ✅LGTM.