Skip to content

Commit

Permalink
Merge pull request #61 from juanolon/recusive
Browse files Browse the repository at this point in the history
Support for directories
  • Loading branch information
emad-elsaid committed Jun 19, 2024
2 parents 6a2a9b0 + 824c909 commit 3099d7f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 35 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ require (
require (
github.com/emad-elsaid/memoize v0.0.0-20240524185039-a963118906c5
github.com/emad-elsaid/types v0.0.4
github.com/fsnotify/fsnotify v1.7.0
github.com/gorilla/websocket v1.5.1
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/rjeczalik/notify v0.9.3
golang.org/x/sync v0.6.0
)

Expand All @@ -33,7 +33,7 @@ require (
github.com/google/go-querystring v1.1.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/oauth2 v0.18.0
golang.org/x/sys v0.18.0 // indirect
golang.org/x/sys v0.21.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/protobuf v1.33.0 // indirect
)
Expand Down
9 changes: 5 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ github.com/emad-elsaid/memoize v0.0.0-20240524185039-a963118906c5 h1:M5V6cvhr4f8
github.com/emad-elsaid/memoize v0.0.0-20240524185039-a963118906c5/go.mod h1:StFIjzcCkMtwKnLcaCGx66uBSH68sfGoz8q227+ZxFo=
github.com/emad-elsaid/types v0.0.4 h1:wk9ftd4uoRAdwDIJKzigAmexK05D0RWqNYdsYXxHZOg=
github.com/emad-elsaid/types v0.0.4/go.mod h1:7A4ii8wOJCtw6WUyJjlNVpA/AoMEfj/r8Oi2l9q5FF4=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
Expand All @@ -49,6 +47,8 @@ github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyf
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rjeczalik/notify v0.9.3 h1:6rJAzHTGKXGj76sbRgDiDcYj/HniypXmSJo1SWakZeY=
github.com/rjeczalik/notify v0.9.3/go.mod h1:gF3zSOrafR9DQEWSE8TjfI9NkooDxbyT4UgRGKZA0lc=
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd h1:CmH9+J6ZSsIjUK3dcGsnCnO41eRBOnY12zwkn5qVwgc=
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd/go.mod h1:hPqNNc0+uJM6H+SuU8sEs5K5IQeKccPqeSjfgcKGgPk=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down Expand Up @@ -87,6 +87,7 @@ golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20180926160741-c2ed4eda69e7/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand All @@ -96,8 +97,8 @@ golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
Expand Down
3 changes: 3 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func getPageHandler(w Response, r Request) Output {
page := NewPage(r.PathValue("page"))

if !page.Exists() {
if s, err := os.Stat(page.Name()); err == nil && s.IsDir() {
return Redirect(INDEX)
}
if output, err := staticHandler(r); err == nil {
return output
}
Expand Down
52 changes: 23 additions & 29 deletions markdown_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"io/fs"
"log"
"log/slog"
"path"
"path/filepath"
Expand All @@ -12,8 +13,9 @@ import (

"github.com/emad-elsaid/memoize"
"github.com/emad-elsaid/memoize/cache/adapters/hashicorp"
"github.com/fsnotify/fsnotify"

lru "github.com/hashicorp/golang-lru/v2"
"github.com/rjeczalik/notify"
)

func newMarkdownFS(p string) *markdownFS {
Expand Down Expand Up @@ -43,44 +45,36 @@ func newMarkdownFS(p string) *markdownFS {

m.watch = sync.OnceFunc(func() {
go func() {
watcher, err := fsnotify.NewWatcher()
events := make(chan notify.EventInfo, 1)

absPath, err := filepath.Abs(m.path)
if err != nil {
slog.Error("Can't watch files for change", "error", err)
log.Fatal(err)
}
defer watcher.Close()

err = watcher.Add(m.path)
if err != nil {
slog.Error("Can't add Markdown FS path", "error", err)
if err := notify.Watch(m.path+"/...", events, notify.All); err != nil {
slog.Error("Can't watch files for change", "error", err)
}
defer notify.Stop(events)

for {
select {
case event, ok := <-watcher.Events:
if !ok {
slog.Error("Filesystem watcher returned not OK from Events")
return
switch ei := <-events; ei.Event() {
case notify.Write, notify.Remove, notify.Rename:
relPath, err := filepath.Rel(absPath, ei.Path())
if err != nil {
slog.Error("Can't resolve relative path", "error", err)
continue
}

if event.Has(fsnotify.Write) || event.Has(fsnotify.Remove) {
if !strings.HasSuffix(event.Name, ".md") {
continue
}

name := strings.TrimSuffix(event.Name, ".md")
name, _ = filepath.Rel(m.path, name)
cp := m._page(name)
Trigger(Changed, cp)

m.cache.Remove(name)
}
case err, ok := <-watcher.Errors:
if !ok {
slog.Error("Filesystem watcher returned not OK from Errors")
return
if !strings.HasSuffix(relPath, ".md") {
continue
}

slog.Error("Filesystem watcher error", "error", err)
name := strings.TrimSuffix(relPath, ".md")
cp := m._page(name)
Trigger(Changed, cp)

m.cache.Remove(name)
}
}
}()
Expand Down

0 comments on commit 3099d7f

Please sign in to comment.