From 2b907c152034ed6869590e894ef3d5c13fe42bf3 Mon Sep 17 00:00:00 2001 From: snmed Date: Sun, 24 Feb 2019 21:50:27 +0100 Subject: [PATCH] Fix createDirFile --- example/webapp-backend/main.go | 1 + example/webapp-frontend/package-lock.json | 43 +++++++---------------- templ.go | 9 +++-- 3 files changed, 19 insertions(+), 34 deletions(-) diff --git a/example/webapp-backend/main.go b/example/webapp-backend/main.go index c8ab956..d9f7bf2 100644 --- a/example/webapp-backend/main.go +++ b/example/webapp-backend/main.go @@ -39,6 +39,7 @@ func main() { http.HandleFunc("/api/time", timeHandler) http.Handle("/files/", http.StripPrefix("/files/", http.FileServer(loader))) + http.Handle("/files2/", http.StripPrefix("/files2/", http.FileServer(http.Dir("../webapp-frontend/build")))) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fp := r.URL.Path diff --git a/example/webapp-frontend/package-lock.json b/example/webapp-frontend/package-lock.json index 73e1e21..737d28e 100644 --- a/example/webapp-frontend/package-lock.json +++ b/example/webapp-frontend/package-lock.json @@ -2756,7 +2756,7 @@ }, "duplexer": { "version": "0.1.1", - "resolved": "http://registry.npmjs.org/duplexer/-/duplexer-0.1.1.tgz", + "resolved": "https://registry.npmjs.org/duplexer/-/duplexer-0.1.1.tgz", "integrity": "sha1-rOb/gIwc5mtX0ev5eXessCM0z8E=" }, "duplexer3": { @@ -3882,8 +3882,7 @@ }, "ansi-regex": { "version": "2.1.1", - "bundled": true, - "optional": true + "bundled": true }, "aproba": { "version": "1.2.0", @@ -3901,13 +3900,11 @@ }, "balanced-match": { "version": "1.0.0", - "bundled": true, - "optional": true + "bundled": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, - "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -3920,18 +3917,15 @@ }, "code-point-at": { "version": "1.1.0", - "bundled": true, - "optional": true + "bundled": true }, "concat-map": { "version": "0.0.1", - "bundled": true, - "optional": true + "bundled": true }, "console-control-strings": { "version": "1.1.0", - "bundled": true, - "optional": true + "bundled": true }, "core-util-is": { "version": "1.0.2", @@ -4034,8 +4028,7 @@ }, "inherits": { "version": "2.0.3", - "bundled": true, - "optional": true + "bundled": true }, "ini": { "version": "1.3.5", @@ -4045,7 +4038,6 @@ "is-fullwidth-code-point": { "version": "1.0.0", "bundled": true, - "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -4058,20 +4050,17 @@ "minimatch": { "version": "3.0.4", "bundled": true, - "optional": true, "requires": { "brace-expansion": "^1.1.7" } }, "minimist": { "version": "0.0.8", - "bundled": true, - "optional": true + "bundled": true }, "minipass": { "version": "2.2.4", "bundled": true, - "optional": true, "requires": { "safe-buffer": "^5.1.1", "yallist": "^3.0.0" @@ -4088,7 +4077,6 @@ "mkdirp": { "version": "0.5.1", "bundled": true, - "optional": true, "requires": { "minimist": "0.0.8" } @@ -4161,8 +4149,7 @@ }, "number-is-nan": { "version": "1.0.1", - "bundled": true, - "optional": true + "bundled": true }, "object-assign": { "version": "4.1.1", @@ -4172,7 +4159,6 @@ "once": { "version": "1.4.0", "bundled": true, - "optional": true, "requires": { "wrappy": "1" } @@ -4248,8 +4234,7 @@ }, "safe-buffer": { "version": "5.1.1", - "bundled": true, - "optional": true + "bundled": true }, "safer-buffer": { "version": "2.1.2", @@ -4279,7 +4264,6 @@ "string-width": { "version": "1.0.2", "bundled": true, - "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -4297,7 +4281,6 @@ "strip-ansi": { "version": "3.0.1", "bundled": true, - "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -4336,13 +4319,11 @@ }, "wrappy": { "version": "1.0.2", - "bundled": true, - "optional": true + "bundled": true }, "yallist": { "version": "3.0.2", - "bundled": true, - "optional": true + "bundled": true } } }, diff --git a/templ.go b/templ.go index c7f2289..47e93a1 100644 --- a/templ.go +++ b/templ.go @@ -268,19 +268,22 @@ func (l loader) Open(name string) (http.File, error) { if !strings.HasPrefix(name, "/") { name = "/" + name } - name = strings.TrimRight(name, "/") + + if len(name) > 1 && name[len(name)-1] == '/' { + name = strings.TrimRight(name, "/") + } if v, ok := l.fm[name]; ok { return &v, nil } for _, v := range l.fm { - if v.path == name { + if strings.HasPrefix(v.path, name) { return createDirFile(name, l.fm), nil } } - return nil, ErrNotFound + return nil, os.ErrNotExist } // New{{.Suffix}}Loader returns a new AssetLoader for the {{.Suffix}} resources.