diff --git a/build.go b/build.go index 5e74294..9b43e82 100644 --- a/build.go +++ b/build.go @@ -212,13 +212,16 @@ func (b *builder) addTemplateHandler(path_ string) error { // strip the extension from the handled path routePath := strings.TrimSuffix(path_, b.config.TemplateExtension) // files named 'index' handle requests to the directory - if path.Base(routePath) == "index" { - routePath = path.Dir(routePath) + base := path.Base(routePath) + if base == "index" { + routePath = path.Dir(routePath) + "/" } - if strings.HasSuffix(routePath, "/") { - routePath += "{$}" + if base == "index{$}" { + routePath = path.Dir(routePath) + "/{$}" } + routePath = path.Clean(routePath) pattern = "GET " + routePath + fmt.Printf("pattern: %s\n", pattern) handler = bufferingTemplateHandler(b.Instance, tmpl) } else if matches := routeMatcher.FindStringSubmatch(name); len(matches) == 3 { method, path_ := matches[1], matches[2] diff --git a/go.mod b/go.mod index 6b19769..4561b55 100644 --- a/go.mod +++ b/go.mod @@ -17,7 +17,7 @@ require ( github.com/microcosm-cc/bluemonday v1.0.26 github.com/nats-io/nats-server/v2 v2.10.12 github.com/nats-io/nats.go v1.34.1 - github.com/tdewolff/minify/v2 v2.20.19 + github.com/tdewolff/minify/v2 v2.20.24 github.com/yuin/goldmark v1.7.1 github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc gopkg.in/yaml.v3 v3.0.1 diff --git a/test/data/subdir/world.txt b/test/data/subdir/world.txt new file mode 100644 index 0000000..e69de29 diff --git a/test/templates/fs/index.html b/test/templates/fs/index.html deleted file mode 100644 index db8761c..0000000 --- a/test/templates/fs/index.html +++ /dev/null @@ -1,11 +0,0 @@ - - -You can read files: - -{{.FS.Read "foo.txt"}} - -You can use the 'fs' provider multiple times to read from different directories -at the same time. (Though in this case it's the same dir twice, which is -admittedly not as useful.) - -{{.FSW.List "."}} diff --git a/test/templates/fs/serve.html b/test/templates/fs/serve.html index 49b7f39..b622ab4 100644 --- a/test/templates/fs/serve.html +++ b/test/templates/fs/serve.html @@ -1,9 +1,10 @@ -You can serve a file by opening it and using .Resp.ServeContent. +You can serve a file by opening it and using .Resp.ServeContent, which discards +any content rendered so far and responds with the contents of the file instead. You can still set headers that are added to the response. -{{.Resp.AddHeader "Content-Type" "text/customcontenttype; charset=utf-8"}} +{{.Resp.AddHeader "Content-Type" "text/plain; charset=utf-8"}} {{$file := .FS.Open "foo.txt"}} {{$stat := $file.Stat}} diff --git a/test/templates/fs/{filepath...}.html b/test/templates/fs/{filepath...}.html new file mode 100644 index 0000000..e7af627 --- /dev/null +++ b/test/templates/fs/{filepath...}.html @@ -0,0 +1,19 @@ + +{{$path := .Req.PathValue "filepath"}} +{{if ne $path ""}}
{{end}} +{{$result := try .FS "Stat" $path}} +{{if not $result.OK}} +Path {{$path}}
doesn't exist
{{$path}}
:
+