remove unnecessary pathpkg

Kim committed Nov 19, 2025 at 11:27 UTC 5f3564282364e127c8cf9e23e3bbaff5a9ae9374
2 files changed +32 -33
cmd/relay-server/serve.go
+29 -30
@@ -4,7 +4,6 @@ import (
4 "encoding/json"
5 "net/http"
6 "path"
7 - pathpkg "path"
7 "strconv"
8 "strings"
9 "sync"
@@ -36,7 +35,7 @@ func servePortalHTMLWithSSR(w http.ResponseWriter, r *http.Request, serv *portal
35 sdk.SetCORSHeaders(w)
36
37 // Read portal.html from embedded FS
39 - fullPath := pathpkg.Join("dist", "app", "portal.html")
38 + fullPath := path.Join("dist", "app", "portal.html")
39 htmlContent, err := distFS.ReadFile(fullPath)
40 if err != nil {
41 log.Error().Err(err).Msg("Failed to read portal.html")
@@ -102,9 +101,9 @@ func servePortalStaticFile(w http.ResponseWriter, r *http.Request, filePath stri
101
102 // serveAppStatic serves static files for app UI (React app) from embedded FS
103 // Falls back to portal.html with SSR when path is root or file not found
105 -func serveAppStatic(w http.ResponseWriter, r *http.Request, path string, serv *portal.RelayServer) {
104 +func serveAppStatic(w http.ResponseWriter, r *http.Request, appPath string, serv *portal.RelayServer) {
105 // Prevent directory traversal
107 - if strings.Contains(path, "..") {
106 + if strings.Contains(appPath, "..") {
107 http.Error(w, "Invalid path", http.StatusBadRequest)
108 return
109 }
@@ -112,23 +111,23 @@ func serveAppStatic(w http.ResponseWriter, r *http.Request, path string, serv *p
111 sdk.SetCORSHeaders(w)
112
113 // If path is empty or "/", serve portal.html with SSR
115 - if path == "" || path == "/" {
114 + if appPath == "" || appPath == "/" {
115 servePortalHTMLWithSSR(w, r, serv)
116 return
117 }
118
119 // Try to read from embedded FS
121 - fullPath := pathpkg.Join("dist", "app", path)
120 + fullPath := path.Join("dist", "app", appPath)
121 data, err := distFS.ReadFile(fullPath)
122 if err != nil {
123 // File not found - fallback to portal.html with SSR for SPA routing
125 - log.Debug().Err(err).Str("path", path).Msg("app static file not found, falling back to SSR")
124 + log.Debug().Err(err).Str("path", appPath).Msg("app static file not found, falling back to SSR")
125 servePortalHTMLWithSSR(w, r, serv)
126 return
127 }
128
129 // Set content type based on extension
131 - ext := pathpkg.Ext(path)
130 + ext := path.Ext(appPath)
131 contentType := sdk.GetContentType(ext)
132 if contentType != "" {
133 w.Header().Set("Content-Type", contentType)
@@ -139,7 +138,7 @@ func serveAppStatic(w http.ResponseWriter, r *http.Request, path string, serv *p
138 w.Write(data)
139
140 log.Debug().
142 - Str("path", path).
141 + Str("path", appPath).
142 Int("size", len(data)).
143 Msg("served app static file")
144 }
@@ -173,7 +172,7 @@ func initWasmCache() error {
172 if strings.HasSuffix(name, ".wasm.br") {
173 hash := strings.TrimSuffix(name, ".wasm.br")
174 if sdk.IsHexString(hash) {
176 - fullPath := pathpkg.Join("dist", "wasm", name)
175 + fullPath := path.Join("dist", "wasm", name)
176 // Cache under the URL path (<hash>.wasm) while reading the
177 // brotli-compressed artifact (<hash>.wasm.br) from embed.FS.
178 cacheKey := hash + ".wasm"
@@ -232,7 +231,7 @@ func serveCompressedWasm(w http.ResponseWriter, r *http.Request, filePath string
231 if !ok {
232 log.Debug().Str("path", filePath).Msg("WASM file not in cache")
233 // Fallback: try to serve uncompressed WASM from embedded FS
235 - fullPath := pathpkg.Join("dist", "wasm", filePath)
234 + fullPath := path.Join("dist", "wasm", filePath)
235 data, err := distFS.ReadFile(fullPath)
236 if err != nil {
237 log.Debug().Err(err).Str("path", fullPath).Msg("WASM file not found in embedded FS")
@@ -286,19 +285,19 @@ func serveCompressedWasm(w http.ResponseWriter, r *http.Request, filePath string
285 // servePortalStatic serves static files for portal frontend with appropriate cache headers
286 // Falls back to portal.html for SPA routing (404 -> portal.html)
287 func servePortalStatic(w http.ResponseWriter, r *http.Request) {
289 - path := strings.TrimPrefix(r.URL.Path, "/")
288 + staticPath := strings.TrimPrefix(r.URL.Path, "/")
289
290 // Prevent directory traversal
292 - if strings.Contains(path, "..") {
291 + if strings.Contains(staticPath, "..") {
292 http.Error(w, "Invalid path", http.StatusBadRequest)
293 return
294 }
295
296 // Special handling for specific files
298 - switch path {
297 + switch staticPath {
298 case "manifest.json":
299 // Serve dynamic manifest regardless of static presence
301 - serveDynamicManifest(w)
300 + serveDynamicManifest(w, r)
301 return
302
303 case "service-worker.js":
@@ -308,29 +307,29 @@ func servePortalStatic(w http.ResponseWriter, r *http.Request) {
307 case "wasm_exec.js":
308 w.Header().Set("Cache-Control", "public, max-age=86400")
309 w.Header().Set("Content-Type", "application/javascript")
311 - serveStaticFileWithFallback(w, r, path, "application/javascript")
310 + serveStaticFileWithFallback(w, r, staticPath, "application/javascript")
311 return
312
313 case "portal.mp4":
314 w.Header().Set("Cache-Control", "public, max-age=604800")
315 w.Header().Set("Content-Type", "video/mp4")
317 - serveStaticFileWithFallback(w, r, path, "video/mp4")
316 + serveStaticFileWithFallback(w, r, staticPath, "video/mp4")
317 return
318 }
319
320 // Default caching for other files
321 w.Header().Set("Cache-Control", "public, max-age=3600")
323 - serveStaticFileWithFallback(w, r, path, "")
322 + serveStaticFileWithFallback(w, r, staticPath, "")
323 }
324
325 // serveStaticFile reads and serves a file from the static directory
327 -func serveStaticFile(w http.ResponseWriter, r *http.Request, path string, contentType string) {
326 +func serveStaticFile(w http.ResponseWriter, r *http.Request, filePath string, contentType string) {
327 sdk.SetCORSHeaders(w)
328
330 - fullPath := pathpkg.Join("dist", "wasm", path)
329 + fullPath := path.Join("dist", "wasm", filePath)
330 data, err := distFS.ReadFile(fullPath)
331 if err != nil {
333 - log.Debug().Err(err).Str("path", path).Msg("static file not found")
332 + log.Debug().Err(err).Str("path", filePath).Msg("static file not found")
333 http.NotFound(w, r)
334 return
335 }
@@ -339,7 +338,7 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, path string, conten
338 if contentType != "" {
339 w.Header().Set("Content-Type", contentType)
340 } else {
342 - ext := pathpkg.Ext(path)
341 + ext := path.Ext(filePath)
342 ct := sdk.GetContentType(ext)
343 if ct != "" {
344 w.Header().Set("Content-Type", ct)
@@ -347,7 +346,7 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, path string, conten
346 }
347
348 log.Debug().
350 - Str("path", path).
349 + Str("path", filePath).
350 Int("size", len(data)).
351 Msg("served static file")
352
@@ -357,14 +356,14 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, path string, conten
356
357 // serveStaticFileWithFallback reads and serves a file from the static directory
358 // If the file is not found, it falls back to portal.html for SPA routing
360 -func serveStaticFileWithFallback(w http.ResponseWriter, r *http.Request, path string, contentType string) {
359 +func serveStaticFileWithFallback(w http.ResponseWriter, r *http.Request, filePath string, contentType string) {
360 sdk.SetCORSHeaders(w)
361
363 - fullPath := pathpkg.Join("dist", "wasm", path)
362 + fullPath := path.Join("dist", "wasm", filePath)
363 data, err := distFS.ReadFile(fullPath)
364 if err != nil {
365 // File not found - fallback to portal.html for SPA routing
367 - log.Debug().Err(err).Str("path", path).Msg("static file not found, serving portal.html")
366 + log.Debug().Err(err).Str("path", filePath).Msg("static file not found, serving portal.html")
367 w.Header().Set("Content-Type", "text/html; charset=utf-8")
368 serveStaticFile(w, r, "portal.html", "text/html; charset=utf-8")
369 return
@@ -374,7 +373,7 @@ func serveStaticFileWithFallback(w http.ResponseWriter, r *http.Request, path st
373 if contentType != "" {
374 w.Header().Set("Content-Type", contentType)
375 } else {
377 - ext := pathpkg.Ext(path)
376 + ext := path.Ext(filePath)
377 ct := sdk.GetContentType(ext)
378 if ct != "" {
379 w.Header().Set("Content-Type", ct)
@@ -382,7 +381,7 @@ func serveStaticFileWithFallback(w http.ResponseWriter, r *http.Request, path st
381 }
382
383 log.Debug().
385 - Str("path", path).
384 + Str("path", filePath).
385 Int("size", len(data)).
386 Msg("served static file")
387
@@ -391,7 +390,7 @@ func serveStaticFileWithFallback(w http.ResponseWriter, r *http.Request, path st
390 }
391
392 // serveDynamicManifest generates and serves manifest.json dynamically
394 -func serveDynamicManifest(w http.ResponseWriter) {
393 +func serveDynamicManifest(w http.ResponseWriter, r *http.Request) {
394 sdk.SetCORSHeaders(w)
395
396 // Find the content-addressed WASM file
@@ -463,7 +462,7 @@ func serveDynamicServiceWorker(w http.ResponseWriter, r *http.Request) {
462 sdk.SetCORSHeaders(w)
463
464 // Read the service-worker.js template
466 - fullPath := pathpkg.Join("dist", "wasm", "service-worker.js")
465 + fullPath := path.Join("dist", "wasm", "service-worker.js")
466 content, err := distFS.ReadFile(fullPath)
467 if err != nil {
468 log.Error().Err(err).Msg("Failed to read service-worker.js")
cmd/relay-server/view.go
+3 -3
@@ -57,7 +57,7 @@ func serveHTTP(addr string, serv *portal.RelayServer, nodeID string, bootstraps
57 }
58 p := strings.TrimPrefix(r.URL.Path, "/frontend/")
59 if p == "manifest.json" {
60 - serveDynamicManifest(w)
60 + serveDynamicManifest(w, r)
61 return
62 }
63
@@ -107,7 +107,7 @@ func serveHTTP(addr string, serv *portal.RelayServer, nodeID string, bootstraps
107 }
108 p := strings.TrimPrefix(r.URL.Path, "/frontend/")
109 if p == "manifest.json" {
110 - serveDynamicManifest(w)
110 + serveDynamicManifest(w, r)
111 return
112 }
113 servePortalStaticFile(w, r, p)
@@ -266,7 +266,7 @@ func convertLeaseEntriesToRows(serv *portal.RelayServer) []leaseRow {
266 dnsLabel = dnsLabel[:8] + "..."
267 }
268
269 - // Build link using the configured subdomain base (strip "*." if present)
269 + // Build link using the configured subdomain base
270 subdomainBase := strings.TrimPrefix(sdk.StripScheme(flagPortalSubdomainURL), "*.")
271 link := fmt.Sprintf("//%s.%s/", lease.Name, subdomainBase)
272