Skip to content

Commit

Permalink
wip writable fs
Browse files Browse the repository at this point in the history
  • Loading branch information
infogulch committed Mar 29, 2024
1 parent 9db9399 commit 81cb143
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 8 deletions.
6 changes: 6 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# TODO

- [ ] Add fs methods to write files, save file upload
- https://github.com/golang/go/issues/45757#issuecomment-1675157698
- https://github.com/hack-pad/hackpadfs
- https://github.com/jarxorg/wfs
- Add separate "wfs" for writable fs?
- [ ] Update `xtemplate-caddy`. Note only caddy 2.8.0 uses Go 1.22
- [ ] Figure out how to run caddy with xtemplate
- [ ] Must test on caddy head?
Expand Down Expand Up @@ -49,6 +54,7 @@

# BACKLOG

- [ ] Review https://github.com/hairyhenderson/gomplate for data source ideas
- [ ] Fix `superfluous response.WriteHeader call from github.com/felixge/httpsnoop.(*Metrics).CaptureMetrics` https://go.dev/play/p/spBB4w7nBCZ
- [ ] Accept Env configuration
- [ ] Built-in CSRF handling?
Expand Down
14 changes: 14 additions & 0 deletions providers/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,17 @@ func (c *DotFS) Open(path_ string) (fs.File, error) {

return file, nil
}

// func (c *DotFS) Create(path_ string) (fs.File, error) {
// path_ = path.Clean(path_)

// dir, ok := (c.fs).(string)

// if !ok {
// return nil, fmt.Errorf("failed to open file")
// }

// f, err := os.Create(filepath.Join(dir, path_))

// return f, err
// }
4 changes: 2 additions & 2 deletions test/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"dot": [
{
"type": "fs",
"name": "FS2",
"path": "./data"
"name": "FSW",
"path": "./dataw"
},
{
"type": "kv",
Expand Down
2 changes: 1 addition & 1 deletion test/templates/fs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
at the same time. (Though in this case it's the same dir twice, which is
admittedly not as useful.)

{{.FS2.Read "hello.txt"}}
{{.FSW.List "."}}
7 changes: 3 additions & 4 deletions test/templates/fs/serve.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
You can still set headers that are added to the response.
{{.Resp.AddHeader "Content-Type" "text/customcontenttype; charset=utf-8"}}

{{with $file := .FS.Open "foo.txt"}}
{{$stat := $file.Stat}}
{{.Resp.ServeContent $stat.Name $stat.ModTime $file}}
{{end}}
{{$file := .FS.Open "foo.txt"}}
{{$stat := $file.Stat}}
{{.Resp.ServeContent $stat.Name $stat.ModTime $file}}

Opened files are automatically closed when the request completes.
7 changes: 7 additions & 0 deletions test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ func main() {
try(log.Seek(0, 0))("seek to beginning")
defer log.Close()

// recreate the rw directory
{
path := filepath.Join(testdir, "dataw")
try0(os.RemoveAll(path), "delete dataw dir")
try0(os.Mkdir(path, os.ModeDir|os.ModePerm), "create dataw dir")
}

defer func() {
if err := recover(); err != nil {
fmt.Printf("exiting because: %v\n", err)
Expand Down
1 change: 0 additions & 1 deletion test/tests/fs.hurl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ HTTP 200
[Asserts]
body contains "<!doctype html>"
body contains "bar"
body contains "world"

# serve content
GET http://localhost:8080/fs/serve
Expand Down

0 comments on commit 81cb143

Please sign in to comment.