mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-18 11:05:02 +03:00
feat(server): Phase 8 — cut HTML routes over to web/dist/
Production cutover. Every user-facing HTML route now serves the
Vue-3-built bundle from web/dist/ instead of rendering the legacy
Go template; the long-hashed Vite assets are served at /assets/ from
the same embedded filesystem. The legacy templates in web/html/ and
the legacy static tree in web/assets/ are kept on disk for now in
case a quick revert is needed, but nothing the binary serves
references them.
What changed:
- web.go: a new //go:embed dist/* feeds the controller package via
a SetDistFS hand-off before controller construction. The static
/assets/ route is rebound: in dev to web/dist/assets/ on disk so
Vite's incremental rebuilds show up live; in prod to the embedded
dist via wrapDistFS (rooted one level deeper than wrapAssetsFS).
- controller/dist.go: serveDistPage helper used by every HTML
handler. Reads dist/<name> from the embedded FS and applies two
transforms before sending:
1. injects <script>window.__X_UI_BASE_PATH__="..."</script>
just before </head> so AppSidebar links resolve under the
panel's basePath.
2. when basePath != "/", rewrites Vite's absolute /assets/ URLs
to <basePath>assets/ so installs running under a custom URL
prefix load the bundle where the static handler lives.
HTML responses go out with no-cache so panel upgrades reach
users on the next refresh; hashed JS/CSS stays cacheable.
- controller/index.go: IndexController.index now serves
dist/login.html for logged-out callers (the redirect for logged-in
users is unchanged).
- controller/xui.go: XUIController.{index,inbounds,settings,xraySettings}
each become a one-line wrapper around serveDistPage.
Smoke checklist for the maintainer:
- run `cd frontend && npm run build` to refresh web/dist/ before
building the Go binary (the embed snapshot is taken at compile
time);
- visit /panel/, /panel/inbounds, /panel/settings, /panel/xray and
confirm each loads its Vue page;
- log out and log back in to verify the login flow;
- confirm the sidebar links navigate correctly under your install's
basePath;
- POST flows (e.g. saving settings) still need the CSRF token —
that endpoint (/panel/csrf-token, added earlier) is unchanged.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
+49
-2
@@ -43,8 +43,35 @@ var htmlFS embed.FS
|
||||
//go:embed translation/*
|
||||
var i18nFS embed.FS
|
||||
|
||||
// distFS embeds the Vite-built frontend (web/dist/). All five user-
|
||||
// facing pages are served from here in production; the legacy
|
||||
// templates in htmlFS / static asset tree in assetsFS are kept on
|
||||
// disk for now so a quick revert is possible, but nothing the
|
||||
// binary serves references them after the cutover.
|
||||
//
|
||||
//go:embed dist/*
|
||||
var distFS embed.FS
|
||||
|
||||
var startTime = time.Now()
|
||||
|
||||
// wrapDistFS adapts the embedded `dist/` directory so it can be mounted
|
||||
// as the panel's `/assets/` static route. Vite emits its bundled JS/CSS
|
||||
// under `dist/assets/`; serving the FS rooted at `dist/assets` makes
|
||||
// `/assets/<hash>.js` URLs resolve directly.
|
||||
type wrapDistFS struct {
|
||||
embed.FS
|
||||
}
|
||||
|
||||
func (f *wrapDistFS) Open(name string) (fs.File, error) {
|
||||
file, err := f.FS.Open("dist/assets/" + name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &wrapAssetsFile{
|
||||
File: file,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type wrapAssetsFS struct {
|
||||
embed.FS
|
||||
}
|
||||
@@ -81,6 +108,13 @@ func (f *wrapAssetsFileInfo) ModTime() time.Time {
|
||||
return startTime
|
||||
}
|
||||
|
||||
// EmbeddedDist returns the embedded Vite-built frontend filesystem.
|
||||
// Controllers serve their HTML out of this FS via the dist-page handler
|
||||
// installed in NewEngine().
|
||||
func EmbeddedDist() embed.FS {
|
||||
return distFS
|
||||
}
|
||||
|
||||
// EmbeddedHTML returns the embedded HTML templates filesystem for reuse by other servers.
|
||||
func EmbeddedHTML() embed.FS {
|
||||
return htmlFS
|
||||
@@ -265,7 +299,10 @@ func (s *Server) initRouter() (*gin.Engine, error) {
|
||||
}
|
||||
// Use the registered func map with the loaded templates
|
||||
engine.LoadHTMLFiles(files...)
|
||||
engine.StaticFS(basePath+"assets", http.FS(os.DirFS("web/assets")))
|
||||
// In dev the bundled `web/dist/assets/` directory is served from
|
||||
// disk so the Vite watcher's incremental rebuilds show up
|
||||
// without restarting the binary.
|
||||
engine.StaticFS(basePath+"assets", http.FS(os.DirFS("web/dist/assets")))
|
||||
} else {
|
||||
// for production
|
||||
template, err := s.getHtmlTemplate(funcMap)
|
||||
@@ -273,12 +310,22 @@ func (s *Server) initRouter() (*gin.Engine, error) {
|
||||
return nil, err
|
||||
}
|
||||
engine.SetHTMLTemplate(template)
|
||||
engine.StaticFS(basePath+"assets", http.FS(&wrapAssetsFS{FS: assetsFS}))
|
||||
// `/assets/` now serves the Vite-built bundle. The legacy
|
||||
// `web/assets/` tree is no longer referenced by any served
|
||||
// page (every user-facing route comes from web/dist/), so
|
||||
// the embedded asset filesystem is rooted at dist/assets/.
|
||||
engine.StaticFS(basePath+"assets", http.FS(&wrapDistFS{FS: distFS}))
|
||||
}
|
||||
|
||||
// Apply the redirect middleware (`/xui` to `/panel`)
|
||||
engine.Use(middleware.RedirectMiddleware(basePath))
|
||||
|
||||
// Hand the embedded `dist/` filesystem to the controller package
|
||||
// before any HTML-serving controller is constructed. Phase 8
|
||||
// cutover: every HTML route reads from web/dist/ instead of
|
||||
// rendering a legacy template.
|
||||
controller.SetDistFS(distFS)
|
||||
|
||||
g := engine.Group(basePath)
|
||||
|
||||
s.index = controller.NewIndexController(g)
|
||||
|
||||
Reference in New Issue
Block a user