From 3a883a388d652b9fa7ffe125f21513a5e1f050b7 Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Fri, 12 Jan 2018 15:37:44 -0500 Subject: [PATCH] Allow for the serving of the public directory fixes #852 --- generators/newapp/templates/actions/app.go.tmpl | 3 ++- .../newapp/templates/actions/render.go.tmpl | 2 +- .../newapp/templates/public/robots.txt.tmpl | 2 ++ render/template.go | 5 ++++- router.go | 17 ++++++++++++++++- 5 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 generators/newapp/templates/public/robots.txt.tmpl diff --git a/generators/newapp/templates/actions/app.go.tmpl b/generators/newapp/templates/actions/app.go.tmpl index d7d89e1d3..a3eb86c73 100644 --- a/generators/newapp/templates/actions/app.go.tmpl +++ b/generators/newapp/templates/actions/app.go.tmpl @@ -82,7 +82,8 @@ func App() *buffalo.App { app.GET("/", HomeHandler) {{ if .opts.AsWeb -}} - app.ServeFiles("/assets", assetsBox) + // serve files from the public directory: + app.ServeFiles("/", assetsBox) {{ end -}} } diff --git a/generators/newapp/templates/actions/render.go.tmpl b/generators/newapp/templates/actions/render.go.tmpl index 0ff65b9f6..ae557012a 100644 --- a/generators/newapp/templates/actions/render.go.tmpl +++ b/generators/newapp/templates/actions/render.go.tmpl @@ -9,7 +9,7 @@ import ( var r *render.Engine {{ if .opts.AsWeb -}} -var assetsBox = packr.NewBox("../public/assets") +var assetsBox = packr.NewBox("../public") {{ end -}} func init() { diff --git a/generators/newapp/templates/public/robots.txt.tmpl b/generators/newapp/templates/public/robots.txt.tmpl new file mode 100644 index 000000000..1f53798bb --- /dev/null +++ b/generators/newapp/templates/public/robots.txt.tmpl @@ -0,0 +1,2 @@ +User-agent: * +Disallow: / diff --git a/render/template.go b/render/template.go index 9e2b12038..b6d5588c3 100644 --- a/render/template.go +++ b/render/template.go @@ -140,7 +140,10 @@ func (s templateRenderer) assetPath(file string) (string, error) { manifest, err := s.AssetsBox.MustString("manifest.json") if err != nil { - return assetPathFor(file), nil + manifest, err = s.AssetsBox.MustString("assets/manifest.json") + if err != nil { + return assetPathFor(file), nil + } } err = loadManifest(manifest) diff --git a/router.go b/router.go index cc09b2d4a..e154afe0d 100644 --- a/router.go +++ b/router.go @@ -3,12 +3,14 @@ package buffalo import ( "fmt" "net/http" + "os" "path" "reflect" "sort" "strings" "github.com/markbates/inflect" + "github.com/pkg/errors" ) // GET maps an HTTP "GET" request to the path and the specified handler. @@ -83,7 +85,20 @@ func (a *App) Mount(p string, h http.Handler) { */ func (a *App) ServeFiles(p string, root http.FileSystem) { path := path.Join(a.Prefix, p) - a.router.PathPrefix(path).Handler(http.StripPrefix(path, http.FileServer(root))) + a.router.PathPrefix(path).Handler(http.StripPrefix(path, a.fileServer(root))) +} + +func (a *App) fileServer(fs http.FileSystem) http.Handler { + fsh := http.FileServer(fs) + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _, err := fs.Open(path.Clean(r.URL.Path)) + if os.IsNotExist(err) { + eh := a.ErrorHandlers.Get(404) + eh(404, errors.Errorf("could not find %s", r.URL.Path), a.newContext(RouteInfo{}, w, r)) + return + } + fsh.ServeHTTP(w, r) + }) } // Resource maps an implementation of the Resource interface