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

Commit

Permalink
fix URL Prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
Xu Dongyan committed Feb 28, 2023
1 parent 23ae76c commit 98b4330
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 9 deletions.
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
4 changes: 2 additions & 2 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,8 +746,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

0 comments on commit 98b4330

Please sign in to comment.