Borg/cmd/serve.go
Claude fe0f85a069
Some checks failed
Go / build (push) Failing after 3s
mkdocs / deploy (push) Failing after 10s
Release / release (push) Failing after 4s
chore: migrate module path from github.com to forge.lthn.ai
Move module declaration and all internal imports from
github.com/Snider/Borg to forge.lthn.ai/Snider/Borg. Also updates
Enchantrix dependency path to forge.lthn.ai/Snider/Enchantrix.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-22 21:39:18 +00:00

73 lines
1.7 KiB
Go

package cmd
import (
"fmt"
"net/http"
"os"
"strings"
"forge.lthn.ai/Snider/Borg/pkg/compress"
"forge.lthn.ai/Snider/Borg/pkg/datanode"
"forge.lthn.ai/Snider/Borg/pkg/tarfs"
"github.com/spf13/cobra"
)
// serveCmd represents the serve command
var serveCmd = NewServeCmd()
func NewServeCmd() *cobra.Command {
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),
RunE: func(cmd *cobra.Command, args []string) error {
dataFile := args[0]
port, _ := cmd.Flags().GetString("port")
rawData, err := os.ReadFile(dataFile)
if err != nil {
return fmt.Errorf("Error reading data file: %w", err)
}
data, err := compress.Decompress(rawData)
if err != nil {
return fmt.Errorf("Error decompressing data: %w", err)
}
var fs http.FileSystem
if strings.HasSuffix(dataFile, ".matrix") {
fs, err = tarfs.New(data)
if err != nil {
return fmt.Errorf("Error creating TarFS from matrix tarball: %w", err)
}
} else {
dn, err := datanode.FromTar(data)
if err != nil {
return fmt.Errorf("Error creating DataNode from tarball: %w", err)
}
fs = http.FS(dn)
}
http.Handle("/", http.FileServer(fs))
fmt.Printf("Serving PWA on http://localhost:%s\n", port)
err = http.ListenAndServe(":"+port, nil)
if err != nil {
return fmt.Errorf("Error starting server: %w", err)
}
return nil
},
}
serveCmd.PersistentFlags().String("port", "8080", "Port to serve the PWA on")
return serveCmd
}
func GetServeCmd() *cobra.Command {
return serveCmd
}
func init() {
RootCmd.AddCommand(GetServeCmd())
}