Skip to content

Go router groups: keep the group's prefix on its routes, and read Go 1.22 method patterns - #134

Open
ryoaonetsuki wants to merge 7 commits into
haydenschmitty:mainfrom
ryoaonetsuki:fix/go-router-groups
Open

ryoaonetsuki wants to merge 7 commits into
haydenschmitty:mainfrom
ryoaonetsuki:fix/go-router-groups

Conversation

@ryoaonetsuki

Copy link
Copy Markdown

Problem

A Go server that groups its endpoints shows them without their prefix, so the map can't connect the website's requests to them:

  • api := r.Group("/api") in Gin, Echo, Fiber or chi, then api.GET("/orders", …) — the map showed GET /orders, not GET /api/orders
  • chi's r.Route("/api", func(r chi.Router) { r.Get("/orders", …) }) — same, the prefix was lost
  • Go 1.22's mux.HandleFunc("GET /orders/{id}", …) wasn't read at all (only plain patterns were)

Change

  • extractGo now returns a routers map (like Python and C# already do), so the group's prefix is applied when the map is built: nested groups compose (/api + /v1 → /api/v1), a group used inline (r.Group("/api").GET(…)) works, and routes chained on one statement (r.GET("/a", …).POST("/b", …)) are both read.
  • chi's Route subrouter prefixes the routes inside its callback, including nested Routes, r.With(auth).Route(…), a Route on a group variable, and a group made inside the callback.
  • Go 1.22 method patterns ("GET /orders/{id}", "POST /orders") read their method; a plain pattern stays ANY.

A route beside a group, or on a router the callback doesn't hand over, keeps no prefix.

Limitations

  • r.Method("GET", "/x", handler) isn't read yet — chi takes the method as an argument there rather than a call of its own.
  • Mount with a factory function (r.Mount("/api", ordersRouter())) isn't resolved.
  • With arguments with nested calls aren't parsed, and a Route whose callback is a variable rather than a literal function isn't read.

How I tested

  • New tests/go-groups.test.mjs: 10 tests — unit tests for each shape above plus the look-alikes that must stay unprefixed, and an end-to-end test where a chi server's grouped routes land under their prefix and the website's fetches connect to them. All 10 fail on the code before this change and pass with it.
  • Full suite: npm test — 494 of 498 tests pass. The 2 failures are pre-existing timing-threshold tests (C# and Liquid pattern-matching speed) that also fail on the code before this change (the 4 failures are pre-existing timing failures on this slow machine, unchanged from before this change).
  • Tried on small sample chi and Gin projects with grouped routes; the change follows the same routers pattern Python and C# already use.

Tested on Linux only. claude plugin validate was not available on this machine.

@SkippySteve

Copy link
Copy Markdown

Nice change. Trying it on a plain net/http server, I noticed two more Go 1.22 pattern shapes it doesn't cover yet: a host before the path ("GET shop.example.com/api/orders" is still missed) and a trailing {$} ("GET /{$}" shows as GET /{$}). In case it's useful, I opened a small PR into your branch with a fix and tests: ryoaonetsuki#1. Feel free to take it or leave it.

#1)

A ServeMux pattern can name a host ("GET shop.example.com/api/orders",
"example.com:8080/"); those endpoints were missed. A host counts only when
it looks like a domain name and only on Handle/HandleFunc, so names like
"devices/+/status" or a host-like string on another router stay out.
A trailing {$} only marks an exact match, so "GET /{$}" is now GET /.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants