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

Allow for the serving of the public directory fixes #852 #853

Merged
merged 2 commits into from
Jan 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion generators/newapp/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func (a Generator) Run(root string, data makr.Data) error {
if a.AsAPI {
defer os.RemoveAll(filepath.Join(a.Root, "templates"))
defer os.RemoveAll(filepath.Join(a.Root, "locales"))
defer os.RemoveAll(filepath.Join(a.Root, "public"))
}
if a.Force {
os.RemoveAll(a.Root)
Expand All @@ -42,7 +43,7 @@ func (a Generator) Run(root string, data makr.Data) error {

for _, f := range files {
if a.AsAPI {
if strings.Contains(f.WritePath, "locales") || strings.Contains(f.WritePath, "templates") {
if strings.Contains(f.WritePath, "locales") || strings.Contains(f.WritePath, "templates") || strings.Contains(f.WritePath, "public") {
continue
}
g.Add(makr.NewFile(f.WritePath, f.Body))
Expand Down
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