Skip to content

Add base-path so the web UI can be served under a path prefix#17

Merged
florianl merged 3 commits into
florianl:mainfrom
julianocosta89:add-base-path
May 5, 2026
Merged

Add base-path so the web UI can be served under a path prefix#17
florianl merged 3 commits into
florianl:mainfrom
julianocosta89:add-base-path

Conversation

@julianocosta89

Copy link
Copy Markdown
Contributor

Summary

  • Add --base-path flag and BASE_PATH env var to allow serving the web UI and API under a sub-path prefix (e.g. /firepit)
  • Inject the base path into the frontend via a Go template so all fetch() calls use the correct prefixed URLs
  • Redirect bare / to <base-path>/ when a base path is configured

Motivation

On the OTel Demo Firepit will be deployed behind Envoy.
Without this change the browser fetches /api/* instead of /<whatever>/api/*, breaking the UI entirely.

Changes

cmd/firepit/main.go

  • Added BasePath string to Config and wired up the flag/env var
  • normalizeBasePath enforces a leading slash and strips any trailing slash
  • startWebUIServer refactored into buildWebUIMux (testable without starting a real listener) and startWebUIServer
  • All route registrations (/, /api/*, /debug/pprof/*) are now prefixed with cfg.BasePath
  • index.html is served as a parsed Go template that injects {{.BasePath}} into a window.__BASE_PATH__ script tag; all other static assets are served by the existing stripped file server
  • When BasePath != "", a redirect handler sends /<base-path>/

cmd/firepit/web/index.html

  • Added <script>window.__BASE_PATH__ = '{{.BasePath}}';</script> in <head>
  • All four fetch() calls now prefix with window.__BASE_PATH__

cmd/firepit/main_test.go

  • TestNormalizeBasePath — unit tests for the normalization helper
  • TestLoadConfigBasePath — env var parsing, including edge cases (/, trailing slash, no leading slash)
  • TestWebUIServerBasePath — integration-style tests using buildWebUIMux directly, covering redirect behaviour and prefixed routes for both the empty and /firepit cases

@florianl florianl left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution!
Just a minor issue: Can we use strings.ReplaceAll() instead of the boiler platte, that is introduced by html/template?

Comment thread cmd/firepit/main.go Outdated
Comment thread cmd/firepit/main.go Outdated
@julianocosta89

Copy link
Copy Markdown
Contributor Author

Thank you @florianl!
Suggestions applied 😊

@florianl florianl left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update 👍

@florianl
florianl merged commit f71b0fc into florianl:main May 5, 2026
9 checks passed
@julianocosta89
julianocosta89 deleted the add-base-path branch May 5, 2026 13:37
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