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

Fix URL prefix #2367

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 3 additions & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ func New(opts Options) *App {
routes: RouteList{},
children: []*App{},

RouteNamer: baseRouteNamer{},
RouteNamer: baseRouteNamer{
Prefix: opts.Prefix,
},
}
a.Home.app = a // replace root.
a.Home.appSelf = a // temporary, reverse reference to the group app.
Expand Down
3 changes: 3 additions & 0 deletions render/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,7 @@ type Options struct {
// DefaultContentType instructs the engine what it should fall back to if
// the "content-type" is unknown
DefaultContentType string

// Prefix inherits the global single prefix from buffalo.Options
Prefix string
}
6 changes: 3 additions & 3 deletions render/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,18 +266,18 @@ func (s *templateRenderer) assetPath(file string) (string, error) {
if err != nil {
manifest, err = s.AssetsFS.Open("assets/manifest.json")
if err != nil {
return assetPathFor(file), nil
return assetPathFor(s.Prefix, file), nil
}
}
defer manifest.Close()

err = loadManifest(manifest)
if err != nil {
return assetPathFor(file), fmt.Errorf("your manifest.json is not correct: %s", err)
return assetPathFor(s.Prefix, file), fmt.Errorf("your manifest.json is not correct: %s", err)
}
}

return assetPathFor(file), nil
return assetPathFor(s.Prefix, file), nil
}

// Template renders the named files using the specified
Expand Down
4 changes: 2 additions & 2 deletions render/template_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ func (s *templateRenderer) addAssetsHelpers(helpers Helpers) Helpers {

var assetMap = stringMap{}

func assetPathFor(file string) string {
func assetPathFor(prefix string, file string) string {
filePath, ok := assetMap.Load(file)
if filePath == "" || !ok {
filePath = file
}
return path.Join("/assets", filePath)
return path.Join(prefix, "/assets", filePath)
}

func loadManifest(manifest io.Reader) error {
Expand Down
8 changes: 7 additions & 1 deletion routenamer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ type RouteNamer interface {
}

// BaseRouteNamer is the default route namer used by apps.
type baseRouteNamer struct{}
type baseRouteNamer struct {
Prefix string
}

func (drn baseRouteNamer) NameRoute(p string) string {
if drn.Prefix != "" {
p = strings.TrimPrefix(p, drn.Prefix)
}

if p == "/" || p == "" {
return "root"
}
Expand Down
25 changes: 23 additions & 2 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,27 @@ func Test_Resource_ParamKey(t *testing.T) {
r.Contains(paths, "/foo/{bazKey}/edit/")
}

func Test_RouteNameWithPrefix(t *testing.T) {
r := require.New(t)
cases := map[string]string{
"/": "root",
"/users": "users",
"/users/new": "newUsers",
}

a := New(Options{Prefix: ""})
for input, expected := range cases {
actual := a.RouteNamer.NameRoute(input)
r.Equal(expected, actual, input)
}

a = New(Options{Prefix: "/prefix"})
for input, expected := range cases {
actual := a.RouteNamer.NameRoute(input)
r.Equal(expected, actual, input)
}
}

type mwResource struct {
WebResource
}
Expand Down Expand Up @@ -746,8 +767,8 @@ func Test_buildRouteName(t *testing.T) {

a = New(Options{Prefix: "/test"})
cases = map[string]string{
"/test": "test",
"/test/users": "testUsers",
"/test": "root",
"/test/users": "users",
}

for input, result := range cases {
Expand Down