2025-10-31 20:32:46 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
2025-10-31 20:47:11 +00:00
|
|
|
|
|
|
|
|
"borg-data-collector/pkg/datanode"
|
2025-10-31 20:32:46 +00:00
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// serveCmd represents the serve command
|
|
|
|
|
var serveCmd = &cobra.Command{
|
|
|
|
|
Use: "serve [file]",
|
|
|
|
|
Short: "Serve a packaged PWA file",
|
|
|
|
|
Long: `Serves the contents of a packaged PWA file using a static file server.`,
|
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
|
pwaFile := args[0]
|
|
|
|
|
port, _ := cmd.Flags().GetString("port")
|
|
|
|
|
|
|
|
|
|
pwaData, err := os.ReadFile(pwaFile)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("Error reading PWA file: %v\n", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-31 20:47:11 +00:00
|
|
|
dn, err := datanode.FromTar(pwaData)
|
2025-10-31 20:32:46 +00:00
|
|
|
if err != nil {
|
2025-10-31 20:47:11 +00:00
|
|
|
fmt.Printf("Error creating DataNode from tarball: %v\n", err)
|
2025-10-31 20:32:46 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-31 20:47:11 +00:00
|
|
|
http.Handle("/", http.FileServer(http.FS(dn)))
|
2025-10-31 20:32:46 +00:00
|
|
|
|
|
|
|
|
fmt.Printf("Serving PWA on http://localhost:%s\n", port)
|
|
|
|
|
err = http.ListenAndServe(":"+port, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("Error starting server: %v\n", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
rootCmd.AddCommand(serveCmd)
|
|
|
|
|
serveCmd.PersistentFlags().String("port", "8080", "Port to serve the PWA on")
|
|
|
|
|
}
|