#1206·buffalo

`app.ServeFiles` should always be accessed last, regardless of when/where it's defined

Author: byungjikrohCreated Aug 2, 2018Updated Sep 29, 2022
Labelshelp wantedproposal

Steps to Reproduce the Problem

I added nested routes without groups' names like categories and products.

actions/app.go

...
cr := &CategoriesResource{}
...
pr := &ProductsResource{}
a.GET("/{category_name}", pr.List)
a.GET("/{category_name}/{product_name}", pr.Show)
...
app.ServeFiles("/", assetsBox)

actions/render.go

var assetsBox = packr.NewBox("../public")

Then, I wanted to serve 'robots.txt' files in public directory but server even couldn't serve assets files in 'public/assets' directory because it tried to fetch a category record named 'robots.txt' or 'assets' which are placed at 'category name' of routes.

So I reverted 'app.go' and 'render.go' like under Buffalo v0.12.4 (which supports public folder's files like robots.txt) and render with plain on products' List action.

actions/app.go

app.ServeFiles("/assets", assetsBox)
...
cr := &CategoriesResource{}
...
pr := &ProductsResource{}
a.GET("/{category_name}", pr.List)
a.GET("/{category_name}/{product_name}", pr.Show)
...

actions/render.go

var assetsBox = packr.NewBox("../public/assets")

actions/products.go

func (v ProductsResource) List(c buffalo.Context) error {
  if c.Param("category_name") == "robots.txt" {
    return c.Render(200, r.Plain("robots.txt"))
  }
  ...
}

Expected Behavior

Expect the server to serve files on public directory like robots.txt or ads.txt properly even without groups on routes.

So I want a feature to pre-define public directory files to serve files before matching routes params.

Elixir web framework Phoenix did it like below.

# Serve at "/" the static files from "priv/static" directory.
#
# You should set gzip to true if you are running phoenix.digest
# when deploying your static files in production.
plug Plug.Static,
  at: "/", from: :example, gzip: false,
  only: ~w(css fonts images js icons favicon.ico robots.txt ads.txt sitemaps)

Actual Behavior

The server can't distinguish static files on public directory and routes params.

Info

### Buffalo Version v0.12.4

App Information

Pwd=C:\Users\byungjik\go\src\example Root=C:\Users\byungjik\go\src\example GoPath=C:\Users\byungjik\go Name=example Bin=bin\example.exe PackagePkg=example ActionsPkg=example/actions ModelsPkg=example/models GriftsPkg=example/grifts VCS=git WithPop=true WithSQLite=false WithDep=false WithWebpack=true WithYarn=true WithDocker=true WithGrifts=true

Go Version

go version go1.9.1 windows/amd64

Go Env

set GOARCH=amd64 set GOBIN= set GOEXE=.exe set GOHOSTARCH=amd64 set GOHOSTOS=windows set GOOS=windows set GOPATH=C:\Users\byungjik\go set GORACE= set GOROOT=C:\Go set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64 set GCCGO=gccgo set CC=gcc set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 set CXX=g++ set CGO_ENABLED=1 set CGO_CFLAGS=-g -O2 set CGO_CPPFLAGS= set CGO_CXXFLAGS=-g -O2 set CGO_FFLAGS=-g -O2 set CGO_LDFLAGS=-g -O2 set PKG_CONFIG=pkg-config

Node Version

v8.11.3

NPM Version

5.6.0

Yarn Version

1.9.2

PostgreSQL Version

PostgreSQL Not Found (But installed 9.6)

MySQL Version

MySQL Not Found

SQLite Version

SQLite Not Found

Dep Version

could not find a Gopkg.toml file

Dep Status

could not find a Gopkg.toml file