Skip to content

Commit

Permalink
Don't add terminal path pattern automatically, support user-added ins…
Browse files Browse the repository at this point in the history
…tead; add directory browser example to tests
  • Loading branch information
infogulch committed May 19, 2024
1 parent 0022b53 commit b5e37b9
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 21 deletions.
11 changes: 7 additions & 4 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Empty file added test/data/subdir/world.txt
Empty file.
11 changes: 0 additions & 11 deletions test/templates/fs/index.html

This file was deleted.

5 changes: 3 additions & 2 deletions test/templates/fs/serve.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<!DOCTYPE html> <!-- servecontent ignores previously rendered content -->

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}}
Expand Down
19 changes: 19 additions & 0 deletions test/templates/fs/{filepath...}.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
{{$path := .Req.PathValue "filepath"}}
{{if ne $path ""}}<p><a href="/fs/{{dir $path}}">Go up</a></p>{{end}}
{{$result := try .FS "Stat" $path}}
{{if not $result.OK}}
<p>Path <code>{{$path}}</code>&nbsp;doesn't exist</p>
{{.Resp.ReturnStatus 404}}
{{end}}
{{$stat := $result.Value}}
{{if $stat.IsDir}}
File listing for <code>{{$path}}</code>:
<ul>
{{range .FS.List $path}}
<li><a href="/fs/{{$path}}/{{.}}">{{.}}</a></li>
{{end}}
</ul>
{{else}}
File size: {{$stat.Size}}
{{end}}
File renamed without changes.
6 changes: 3 additions & 3 deletions test/tests/fs.hurl
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# reading files from fs
GET http://localhost:8080/fs
GET http://localhost:8080/fs/

HTTP 200
[Asserts]
body contains "<!doctype html>"
body contains "bar"
body contains "listing"

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

HTTP 200
Content-Type: text/customcontenttype; charset=utf-8
Content-Type: text/plain; charset=utf-8
[Asserts]
body not contains "doctype"
body contains "bar"
Expand Down

0 comments on commit b5e37b9

Please sign in to comment.