Skip to content

feat: support serving Ratel under a URL prefix (-url-prefix)#422

Open
shaunpatterson wants to merge 1 commit into
dgraph-io:mainfrom
shaunpatterson:sp/subpath-hosting
Open

feat: support serving Ratel under a URL prefix (-url-prefix)#422
shaunpatterson wants to merge 1 commit into
dgraph-io:mainfrom
shaunpatterson:sp/subpath-hosting

Conversation

@shaunpatterson

Copy link
Copy Markdown
Contributor

What

Implements #390: serve Ratel under a URL prefix (e.g. https://host/ratel/) behind reverse proxies.

  • New -url-prefix flag with RATEL_URL_PREFIX env fallback (env used only when the flag is empty). Default empty = behavior unchanged.
  • The value is normalized (leading slash added, trailing slashes stripped; ""/"/" → no prefix); multi-segment prefixes (/a/b) work.
  • With a prefix set: all routes served under it via http.StripPrefix; bare /ratel301 to /ratel/ (path-only redirect, proxy-safe); root-relative href/src attributes and the inline injectJs('/loader.js') in the served index.html are rewritten at startup (protocol-relative //cdn... URLs deliberately untouched); paths outside the prefix return 404 with a hint ("Ratel is served under /ratel/") rather than hijacking the root of a shared host.
  • The main JS bundle is injected with a relative path and the client talks to Dgraph directly, so no client/build changes are needed.

Testing

9 Go tests (httptest): prefix normalization table, HTML rewriting incl. the protocol-relative exclusion, no-prefix behavior unchanged (root + asset), prefixed index rewritten, bare-prefix redirect, outside-prefix 404s, embedded static assets resolving under the prefix. Asset tests pick a real name from bindata's AssetNames() so they don't depend on build contents.

Manually verified with the built binary: -url-prefix /ratel serves /ratel/ with rewritten asset URLs, redirects /ratel, 404s /, and the env-var fallback works. Docs added to INSTRUCTIONS.md alongside the existing TLS flags.

Closes #390.

🤖 Generated with Claude Code

Adds a -url-prefix flag (with RATEL_URL_PREFIX env fallback) to the Go
server so Ratel can be hosted under a subpath behind reverse proxies,
e.g. https://example.com/ratel/ (fixes dgraph-io#390).

When a prefix is set:
- all routes are served under the prefix via http.StripPrefix
- the bare prefix redirects (301) to the prefix with a trailing slash
- root-relative href/src URLs and the inline loader.js reference in the
  served index.html are rewritten to include the prefix
- paths outside the prefix return 404 with a hint at the prefix

With no prefix (the default) behavior is unchanged.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@shaunpatterson shaunpatterson requested a review from a team as a code owner June 12, 2026 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

allow subpath configuration

1 participant