Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Allow for the serving of the public directory fixes #852
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Jan 12, 2018
1 parent 4ddc0a1 commit 3a883a3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
3 changes: 2 additions & 1 deletion generators/newapp/templates/actions/app.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 -}}
}

Expand Down
2 changes: 1 addition & 1 deletion generators/newapp/templates/actions/render.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 2 additions & 0 deletions generators/newapp/templates/public/robots.txt.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
User-agent: *
Disallow: /
5 changes: 4 additions & 1 deletion render/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 16 additions & 1 deletion router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3a883a3

Please sign in to comment.