shithub: mycel

Download patch

ref: 793e9a1d7c697af189320c9e684e5559bb06e3c1
parent: 94389da8017a2bcca3cb29a4f954c928d577a31a
author: Philip Silva <philip.silva@protonmail.com>
date: Tue Jun 1 13:56:25 EDT 2021

add js files to fs and move to separate package

diff: cannot open b/browser/fs//null: file does not exist: 'b/browser/fs//null'
--- a/browser/browser.go
+++ b/browser/browser.go
@@ -14,6 +14,7 @@
 	"net/url"
 	"github.com/psilva261/opossum"
 	"github.com/psilva261/opossum/browser/cache"
+	"github.com/psilva261/opossum/browser/fs"
 	"github.com/psilva261/opossum/browser/history"
 	"github.com/psilva261/opossum/img"
 	"github.com/psilva261/opossum/logger"
@@ -1361,6 +1362,8 @@
 		log.Fatalf("%v", err)
 	}
 	display = dui.Display
+
+	go fs.Srv9p()
 
 	b.Website.layout(b, htm, InitialLayout)
 
--- /dev/null
+++ b/browser/fs/fs.go
@@ -1,0 +1,82 @@
+package fs
+
+import (
+	"fmt"
+	"github.com/knusbaum/go9p/fs"
+	"github.com/knusbaum/go9p/proto"
+	"github.com/psilva261/opossum/logger"
+	"os/user"
+	"sync"
+)
+
+var (
+	log *logger.Logger
+	mu sync.Mutex
+	oFS *fs.FS
+	un string
+	gn string
+	jsDir *fs.StaticDir
+	html string
+)
+
+func SetLogger(l *logger.Logger) {
+	log = l
+}
+
+func Update(htm string, js []string) {
+	mu.Lock()
+	defer mu.Unlock()
+
+	html = htm
+
+	for name := range jsDir.Children() {
+		jsDir.DeleteChild(name)
+	}
+	for i, s := range js {
+		fn := fmt.Sprintf("%d.js", i)
+		f := fs.NewStaticFile(
+			oFS.NewStat(fn, un, gn, 0400),
+			[]byte(s),
+		)
+		jsDir.AddChild(f)
+	}
+}
+
+func Srv9p() {
+	if err := srv9p(); err != nil {
+		log.Errorf("srv9p: %v", err)
+	}
+}
+
+func srv9p() (err error) {
+	var root *fs.StaticDir
+
+	u, err := user.Current()
+	if err != nil {
+		return fmt.Errorf("get user: %w", err)
+	}
+	un = u.Username
+	gn, err = group(u)
+	if err != nil {
+		return fmt.Errorf("get group: %w", err)
+	}
+	oFS, root = fs.NewFS(un, gn, 0500)
+	h := fs.NewDynamicFile(
+		oFS.NewStat("html", un, gn, 0400),
+		func() []byte {
+			mu.Lock()
+			defer mu.Unlock()
+
+			return []byte(html)
+		},
+	)
+	root.AddChild(h)
+	d, err := fs.CreateStaticDir(oFS, root, un, "js", 0500|proto.DMDIR, 0)
+	if err != nil {
+		return
+	}
+	jsDir = d.(*fs.StaticDir)
+	root.AddChild(jsDir)
+
+	return post(oFS.Server())
+}
--- /dev/null
+++ b/browser/fs/fs_plan9.go
@@ -1,0 +1,32 @@
+package fs
+
+import (
+	"fmt"
+	"github.com/knusbaum/go9p"
+	"os"
+	"os/user"
+	"syscall"
+)
+
+func group(u *user.User) (string, error) {
+	return u.Gid, nil
+}
+
+func post(srv go9p.Srv) (err error) {
+	f1, f2, err := os.Pipe()
+	if err != nil {
+		return fmt.Errorf("pipe: %w", err)
+	}
+	
+	go func() {
+		err = go9p.ServeReadWriter(f1, f1, srv)
+		if err != nil {
+			log.Fatalf("serve rw: %v", err)
+		}
+	}()
+
+	if err = syscall.Mount(int(f2.Fd()), -1, "/mnt/opossum", syscall.MCREATE, ""); err != nil {
+		return fmt.Errorf("mount: %w", err)
+	}
+	return
+}
--- /dev/null
+++ b/browser/fs/fs_unix.go
@@ -1,0 +1,22 @@
+// +build !plan9
+
+package fs
+
+import (
+	"fmt"
+	"github.com/knusbaum/go9p"
+	"os/user"
+)
+
+func group(u *user.User) (string, error) {
+	g, err := user.LookupGroupId(u.Gid)
+	if err != nil {
+		return "", fmt.Errorf("get group: %w", err)
+	}
+	return g.Name, nil
+}
+
+func post(srv go9p.Srv) (err error) {
+	return go9p.PostSrv("opossum", srv)
+}
+
--- a/browser/website.go
+++ b/browser/website.go
@@ -7,6 +7,7 @@
 	"io/ioutil"
 	"net/url"
 	"github.com/psilva261/opossum"
+	"github.com/psilva261/opossum/browser/fs"
 	"github.com/psilva261/opossum/domino"
 	"github.com/psilva261/opossum/nodes"
 	"github.com/psilva261/opossum/style"
@@ -118,6 +119,9 @@
 			downloads[src] = string(buf)
 		}
 		codes := domino.Scripts(nt, downloads)
+		w.mu.Lock()
+		w.js = append([]string{}, codes...)
+		w.mu.Unlock()
 		log.Infof("JS pipeline start")
 		if w.d != nil {
 			log.Infof("Stop existing JS instance")
@@ -170,6 +174,7 @@
 	w.mu.Lock()
 	w.html = htm
 	w.mu.Unlock()
+	fs.Update(w.html, w.js)
 
 	log.Flush()
 }
--- a/cmd/browse/fs.go
+++ /dev/null
@@ -1,46 +1,0 @@
-package main
-
-import (
-	"fmt"
-	"github.com/knusbaum/go9p/fs"
-	"github.com/psilva261/opossum/browser"
-	"os/user"
-	"os"
-	"sync"
-)
-
-func Srv9p(b *browser.Browser) {
-	if err := srv9p(b); err != nil {
-		log.Errorf("srv9p: %v", err)
-	}
-}
-
-type Root struct{
-	html os.FileInfo
-	mu sync.Mutex
-	of int64
-}
-
-func srv9p(b *browser.Browser) (err error) {
-	log.Infof("srv9p")
-	u, err := user.Current()
-	if err != nil {
-		return fmt.Errorf("get user: %w", err)
-	}
-	un := u.Username
-	gn, err := group(u)
-	if err != nil {
-		return fmt.Errorf("get group: %w", err)
-	}
-	oFS, root := fs.NewFS(un, gn, 0500)
-	h := fs.NewDynamicFile(
-		oFS.NewStat("html", un, gn, 0400),
-		func() []byte {
-			return []byte(b.Website.Html())
-		},
-	)
-	root.AddChild(h)
-
-	log.Infof("post fs")
-	return post(oFS.Server())
-}
--- a/cmd/browse/fs_plan9.go
+++ /dev/null
@@ -1,32 +1,0 @@
-package main
-
-import (
-	"fmt"
-	"github.com/knusbaum/go9p"
-	"os"
-	"os/user"
-	"syscall"
-)
-
-func group(u *user.User) (string, error) {
-	return u.Gid, nil
-}
-
-func post(srv go9p.Srv) (err error) {
-	f1, f2, err := os.Pipe()
-	if err != nil {
-		return fmt.Errorf("pipe: %w", err)
-	}
-	
-	go func() {
-		err = go9p.ServeReadWriter(f1, f1, srv)
-		if err != nil {
-			log.Fatalf("serve rw: %v", err)
-		}
-	}()
-
-	if err = syscall.Mount(int(f2.Fd()), -1, "/mnt/opossum", syscall.MCREATE, ""); err != nil {
-		return fmt.Errorf("mount: %w", err)
-	}
-	return
-}
--- a/cmd/browse/fs_unix.go
+++ /dev/null
@@ -1,22 +1,0 @@
-// +build !plan9
-
-package main
-
-import (
-	"fmt"
-	"github.com/knusbaum/go9p"
-	"os/user"
-)
-
-func group(u *user.User) (string, error) {
-	g, err := user.LookupGroupId(u.Gid)
-	if err != nil {
-		return "", fmt.Errorf("get group: %w", err)
-	}
-	return g.Name, nil
-}
-
-func post(srv go9p.Srv) (err error) {
-	return go9p.PostSrv("opossum", srv)
-}
-
--- a/cmd/browse/main.go
+++ b/cmd/browse/main.go
@@ -8,6 +8,7 @@
 	"os"
 	"github.com/psilva261/opossum"
 	"github.com/psilva261/opossum/browser"
+	"github.com/psilva261/opossum/browser/fs"
 	"github.com/psilva261/opossum/domino"
 	"github.com/psilva261/opossum/img"
 	"github.com/psilva261/opossum/logger"
@@ -132,6 +133,7 @@
 
 	style.Init(dui, log)
 	browser.SetLogger(log)
+	fs.SetLogger(log)
 	domino.SetLogger(log)
 	img.SetLogger(log)
 	opossum.SetLogger(log)
@@ -148,7 +150,6 @@
 		return confirm(b, fmt.Sprintf("Download %v", b.URL()), "/download.file")
 	}
 	render(b, mainView(b))
-	go Srv9p(b)
 
 	for {
 		select {