Skip to content

api: migrate /api/v1 routes to Flamego - #8454

Merged
unknwon merged 6 commits into
mainfrom
api-v1-flamego
Nov 17, 2026
Merged

unknwon merged 6 commits into
mainfrom
api-v1-flamego

Conversation

@unknwon

@unknwon unknwon commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Describe the pull request

Migrates all /api/v1 routes from Macaron to Flamego. Macaron still handles sessions, authentication (token, basic, reverse proxy), CORS and security headers, and REQUIRE_SIGNIN_VIEW, then hands the request to a dedicated Flamego instance for API v1. That instance is separate from the web handler, so requests that reach the web handler (e.g., through ServeWeb) can never run API v1 handlers without those checks.

Contract kept: every API v1 response is the same as on main, including status codes, headers, error bodies, and form keys. The two exceptions are listed below.

How Macaron-specific behavior is preserved:

  • Routing. Like Macaron, apiV1Bridger trims the trailing slash and unescapes each path segment on its own, so %2F stays inside its segment (e.g., branches/feature%2Fx) and encoded characters in static segments still match. The original path is kept for Link headers.
  • Auth flags. IsTokenAuth and IsBasicAuth are passed across the bridge for reqToken, reqBasicAuth, and the admin check in repoAssignment.
  • Binding. The new bind helper mirrors go-macaron/binding: the same Content-Type dispatch (JSON, form, multipart, YAML), the same binding:"..." tags and validation rules (through binding.RawValidate), the same form keys (e.g., u_i_d, source_i_d), and the same [{"fieldNames", "classification", "message"}] error body with 415, 400, or 422.
  • 404s. Unknown routes, and raw files and archives whose reference does not resolve, still respond with the web 404 page, through hooks the bridge passes in. Other 404s stay empty.
  • HEAD. HEAD is served by GET handlers, and unknown routes return 404.
  • Tracing. The bridge hands the Flamego route, e.g., GET /api/v1/repos/{owner}/{repo}, back to the request the tracing handler reads, so spans are named after the route instead of the full path.

Intentional behavior changes:

  • GET /repos/:owner/:repo/editorconfig/:filename on an empty repository returns 404 instead of 500. The 500 was a nil-pointer panic.
  • On main, Macaron's static-route shortcut matched the decoded path, so parameterless routes were also reachable through an encoded slash between fixed segments (e.g., /api%2Fv1/user, /api/v1/user%2Frepos). These now get the web 404 page, the same as such paths to parameterized routes already did.

Refactors that come with the migration:

  • internal/context.APIContext is removed, and apiContext in api/v1 replaces it.
  • Archive creation moves to repo.PrepareArchive.
  • .editorconfig parsing moves into the API v1 handler, its only user, and context.Repository.Editorconfig is removed.
  • Context.HasError and Context.GetErrMsg are removed, because API v1 was their last user.

Link to the issue: n/a

Checklist

  • I agree to follow the Code of Conduct by submitting this pull request.
  • I have read and acknowledge the Contributing guide.
  • I have added test cases to cover the new code or have provided the test plan. (if applicable)
  • I have added an entry to CHANGELOG. (if applicable)

Test plan

  1. Build main and this branch with -tags prod. Run both against fresh copies of the same SQLite data (users, orgs, teams, repositories including empty, private and mirror ones, issues, labels, milestones, hooks, keys).
  2. Replay the same 488-request sequence against both. It covers every API v1 route and method, including:
    • auth: anonymous, token (header and query), basic, and bad credentials, as an admin, a collaborator, and a user without access
    • JSON, form-encoded, multipart, and YAML bodies, an empty or unsupported Content-Type, and malformed bodies
    • every validation rule, unparsable form values, and u_i_d and source_i_d form keys
    • create, edit, and delete for repositories, hooks, collaborators, keys, labels, milestones, issues, comments, orgs, teams, and users, with IDs read from each server's own responses
    • raw files, archives, trees, blobs, commits (Accept: application/vnd.gogs.sha, If-None-Match), editorconfig, mirror sync
    • unknown routes for every method at several depths, HEAD, and OPTIONS
  3. Compare status codes, headers (Content-Type, Content-Disposition, Location, CORS, security), and bodies with hosts, ports, and timestamps normalized.
  4. As a control, run the same sequence against two copies of main.

Results:

  • 478 of 488 responses are identical.
  • 9 of the 10 that differ also differ between the two copies of main: new access token SHAs, and the order of repositories updated within the same second.
  • The remaining one is the empty-repository editorconfig, now 404 instead of 500.
  • /api%2Fv1/user and /api/v1/user%2Frepos return 200 on main and 404 here, as listed above.

TestBind checks the go-macaron/binding error bodies for 415, 400, and 422, and the snake_case form keys. It fails against the previous bind. Also ran go test ./internal/route/... ./cmd/gogs/... and golangci-lint run (0 issues).

AI usage

Written by Amp (Claude), directed and reviewed by @unknwon.

@unknwon
unknwon changed the base branch from main to ui-cleanup-react-flamego November 13, 2026 14:10
@unknwon
unknwon added this pull request to stack #8457 November 13, 2026 15:32
@unknwon
unknwon force-pushed the api-v1-flamego branch 2 times, most recently from 570ca6b to e1884f9 Compare November 14, 2026 07:09
@unknwon
unknwon force-pushed the api-v1-flamego branch 7 times, most recently from f872393 to fcb8f7a Compare November 14, 2026 19:40
@unknwon
unknwon force-pushed the api-v1-flamego branch 3 times, most recently from 92fd754 to fe2ddfa Compare November 14, 2026 21:23
@unknwon
unknwon force-pushed the api-v1-flamego branch 3 times, most recently from 8d08831 to 11fc1b8 Compare November 14, 2026 23:00
@unknwon
unknwon force-pushed the api-v1-flamego branch 4 times, most recently from 5bf15dd to c8f8d3a Compare November 15, 2026 22:45
@unknwon
unknwon force-pushed the api-v1-flamego branch 2 times, most recently from 334fafe to 9cadbd0 Compare November 16, 2026 03:07
Base automatically changed from ui-cleanup-react-flamego to main November 16, 2026 22:25
@unknwon
unknwon marked this pull request as ready for review November 16, 2026 22:25
@mintlify

mintlify Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated
Gogs 🟢 Ready View Preview Nov 16, 2026, 10:41 PM

💡 Tip: Enable Automations to automatically generate PRs for you.

Serve all API v1 routes from the Flamego router through the existing
Macaron bridge. Status codes and the {"message", "url"} error body are
unchanged. Request binding errors now use the same {"message", "url"}
body instead of the go-macaron/binding error array.
Requests that Macaron hands to the web handler, e.g., through
Context.ServeWeb, could reach API v1 routes without the sign-in and
authentication checks. API v1 now has a dedicated Flamego instance that
is only reachable through its bridge.

The bridge also unescapes each path segment like Macaron did, so encoded
characters in static segments still match, and passes the original path
so the pagination Link header keeps the trailing slash.
The API v1 bridge passes a copy of the request to Flamego, so the route
pattern was set on the copy instead of the request the tracing handler
reads, and spans were named after the full path. Also list the changed
form keys of API v1 in the CHANGELOG.
Bind request bodies with the same rules and error responses as
github.com/go-macaron/binding, including form keys such as u_i_d and
source_i_d. Unknown routes, and raw files and archives whose reference
does not resolve, respond with the web 404 page as before.
@unknwon
unknwon merged commit 423e250 into main Nov 17, 2026
12 checks passed
@unknwon
unknwon deleted the api-v1-flamego branch November 17, 2026 01:50
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.

1 participant