e6afa91e09
kage renders every page in headless Chrome, snapshots the final DOM, strips all JavaScript, and localises CSS, images, and fonts so a site can be browsed offline as a plain folder of files. The engine is split into small packages: urlx deterministic URL to local-path mapping and scope rules sanitize remove scripts, on* handlers, and javascript: URLs asset rewrite HTML and CSS references, download assets browser headless Chrome pool over the DevTools protocol robots robots.txt matcher clone the orchestrator: a polite resumable breadth-first crawl The cli package wires a cobra and fang command surface with two commands, clone and serve. Every pure package has table tests; the browser and clone packages add Chrome-driven end-to-end tests that skip when no browser is present or under -short. CI runs gofmt, vet, build, race tests, golangci-lint, govulncheck, and a tidy check on Linux and macOS. A goreleaser config fans one tag out to archives, deb/rpm/apk, a Chromium-bundled GHCR image, and the package managers. A tago docs site builds to Pages and Cloudflare.
55 lines
1.4 KiB
Go
55 lines
1.4 KiB
Go
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func newServeCmd() *cobra.Command {
|
|
var addr string
|
|
cmd := &cobra.Command{
|
|
Use: "serve [dir]",
|
|
Short: "Preview a cloned folder in your browser",
|
|
Long: "serve runs a local static file server over a cloned folder so you can click\n" +
|
|
"through the mirror exactly as a visitor would. With no dir it serves the\n" +
|
|
"current directory.",
|
|
Args: cobra.MaximumNArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
dir := "."
|
|
if len(args) == 1 {
|
|
dir = args[0]
|
|
}
|
|
return runServe(dir, addr)
|
|
},
|
|
}
|
|
cmd.Flags().StringVarP(&addr, "addr", "a", "127.0.0.1:8800", "address to listen on")
|
|
return cmd
|
|
}
|
|
|
|
func runServe(dir, addr string) error {
|
|
info, err := os.Stat(dir)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot serve %q: %w", dir, err)
|
|
}
|
|
if !info.IsDir() {
|
|
return fmt.Errorf("%q is not a directory", dir)
|
|
}
|
|
abs, _ := filepath.Abs(dir)
|
|
|
|
ln, err := net.Listen("tcp", addr)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot listen on %s: %w", addr, err)
|
|
}
|
|
|
|
srv := &http.Server{Handler: http.FileServer(http.Dir(abs))}
|
|
fmt.Fprintln(os.Stderr, styleTitle.Render("kage serve")+" "+styleDim.Render(abs))
|
|
fmt.Fprintln(os.Stderr, " open "+styleAccent.Render("http://"+ln.Addr().String()))
|
|
fmt.Fprintln(os.Stderr, styleDim.Render(" press Ctrl-C to stop"))
|
|
return srv.Serve(ln)
|
|
}
|