5b7f7d9f31
A packed binary opened the system browser, so it felt like a tab, not an app. Build with -tags webview (cgo) and the viewer instead opens the site in its own window backed by the OS WebView: WKWebView on macOS, WebView2 on Windows, WebKitGTK on Linux. The viewer package picks an implementation at build time. The default file opens the browser and keeps the build pure Go, so CGO_ENABLED=0 and the release pipeline are untouched. The webview file links the platform WebView and runs its event loop on the main goroutine, which main now pins with LockOSThread before anything else, since macOS requires UI on the initial thread. Both kage open and the embedded viewer serve over HTTP in a goroutine and hand the URL to the viewer, then tear the server down when the window closes or Ctrl-C cancels. The window title comes from the archive's M/Title. OpenInBrowser moves out of pack into the viewer package, its only caller.
46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
package pack
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/tamnd/kage/zim"
|
|
)
|
|
|
|
// Handler serves a ZIM archive over HTTP. "/" maps to the archive's main page;
|
|
// "/a/b.png" maps to the C/a/b.png content entry. Because the saved HTML's links
|
|
// are mirror-relative paths, and those are exactly the C urls, a click in a
|
|
// served page hits the right entry with no rewriting. A miss is a plain 404.
|
|
func Handler(r *zim.Reader) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
p := strings.TrimPrefix(req.URL.Path, "/")
|
|
if p == "" {
|
|
blob, err := r.MainPage()
|
|
if err != nil {
|
|
http.NotFound(w, req)
|
|
return
|
|
}
|
|
serveBlob(w, blob)
|
|
return
|
|
}
|
|
blob, err := r.Get(zim.NamespaceContent, p)
|
|
if errors.Is(err, zim.ErrNotFound) {
|
|
http.NotFound(w, req)
|
|
return
|
|
}
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
serveBlob(w, blob)
|
|
})
|
|
}
|
|
|
|
func serveBlob(w http.ResponseWriter, b zim.Blob) {
|
|
if b.MimeType != "" {
|
|
w.Header().Set("Content-Type", b.MimeType)
|
|
}
|
|
_, _ = w.Write(b.Data)
|
|
}
|