feat: implement web mode for process detail fetching and management, enhance server routing for SPA
This commit is contained in:
22
server.go
22
server.go
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
@@ -19,6 +20,14 @@ func runServer(port int) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
// Serve embedded frontend/dist at /
|
||||
distFS, err := fs.Sub(assets, "frontend/dist")
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to load embedded assets: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fileServer := http.FileServer(http.FS(distFS))
|
||||
|
||||
streamSys := backend.NewSysInfoHeadless(ctx)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
@@ -148,6 +157,19 @@ func runServer(port int) {
|
||||
fmt.Printf(" GET /api/man/<name> — manual page for a command\n")
|
||||
fmt.Println("\nPress Ctrl+C to stop.")
|
||||
|
||||
// Fallback: serve index.html for any non-API, non-file route (SPA)
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
// Try to serve the exact file from dist; if it doesn't exist, serve index.html
|
||||
_, err := distFS.Open(strings.TrimPrefix(r.URL.Path, "/"))
|
||||
if err != nil || r.URL.Path == "/" {
|
||||
r2 := r.Clone(r.Context())
|
||||
r2.URL.Path = "/"
|
||||
fileServer.ServeHTTP(w, r2)
|
||||
return
|
||||
}
|
||||
fileServer.ServeHTTP(w, r)
|
||||
})
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: addr,
|
||||
Handler: mux,
|
||||
|
||||
Reference in New Issue
Block a user