Skip to content

Commit a6980ea

Browse files
committed
embedding html assets (fix #9)
1 parent 43ba725 commit a6980ea

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

internal/analyzer/server.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"encoding/json"
55
"log"
66
"net/http"
7-
"os"
8-
"path/filepath"
97
"strings"
108
)
119

@@ -44,7 +42,7 @@ func (s *Server) Start(addr string) error {
4442
})
4543

4644
// Serve static UI files
47-
fs := http.FileServer(http.Dir("internal/analyzer/ui"))
45+
fs := http.FileServer(getUIFileSystem())
4846
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
4947
// If the request is for an API endpoint, return 404
5048
if strings.HasPrefix(r.URL.Path, "/api/") {
@@ -54,10 +52,9 @@ func (s *Server) Start(addr string) error {
5452

5553
// For all other requests, serve the UI
5654
// If the path doesn't exist, serve index.html for client-side routing
57-
path := filepath.Join("internal/analyzer/ui", r.URL.Path)
58-
if _, err := os.Stat(path); err != nil {
59-
http.ServeFile(w, r, "internal/analyzer/ui/index.html")
60-
return
55+
path := r.URL.Path
56+
if path == "/" {
57+
path = "/index.html"
6158
}
6259
fs.ServeHTTP(w, r)
6360
})

internal/analyzer/ui_embed.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package analyzer
2+
3+
import (
4+
"embed"
5+
"io/fs"
6+
"net/http"
7+
)
8+
9+
//go:embed ui
10+
var uiFS embed.FS
11+
12+
// getUIFileSystem returns a http.FileSystem for the embedded UI files
13+
func getUIFileSystem() http.FileSystem {
14+
// Get the subdirectory "ui" from the embedded filesystem
15+
subFS, err := fs.Sub(uiFS, "ui")
16+
if err != nil {
17+
panic(err)
18+
}
19+
return http.FS(subFS)
20+
}

0 commit comments

Comments
 (0)