diff --git a/http/codegen/server.go b/http/codegen/server.go index bbaf5ca4d8..a06fb247b6 100644 --- a/http/codegen/server.go +++ b/http/codegen/server.go @@ -88,7 +88,20 @@ func serverFile(genpkg string, svc *expr.HTTPServiceExpr) *codegen.File { sections = append(sections, &codegen.SectionTemplate{Name: "server-handler-init", Source: readTemplate("server_handler_init"), FuncMap: funcs, Data: e}) } if len(data.FileServers) > 0 { - sections = append(sections, &codegen.SectionTemplate{Name: "append-fs", Source: readTemplate("append_fs"), FuncMap: funcs, Data: data}) + mappedFiles := make(map[string]string) + for _, fs := range data.FileServers { + if !fs.IsDir { + for _, p := range fs.RequestPaths { + baseFilePath := "/" + filepath.Base(fs.FilePath) + baseRequestPath := "/" + filepath.Base(p) + if baseFilePath == baseRequestPath { + continue + } + mappedFiles[baseRequestPath] = baseFilePath + } + } + } + sections = append(sections, &codegen.SectionTemplate{Name: "append-fs", Source: readTemplate("append_fs"), FuncMap: funcs, Data: mappedFiles}) } for _, s := range data.FileServers { sections = append(sections, &codegen.SectionTemplate{Name: "server-files", Source: readTemplate("file_server"), FuncMap: funcs, Data: s}) diff --git a/http/codegen/templates/append_fs.go.tpl b/http/codegen/templates/append_fs.go.tpl index 08233822b8..0be80c6ff9 100644 --- a/http/codegen/templates/append_fs.go.tpl +++ b/http/codegen/templates/append_fs.go.tpl @@ -8,6 +8,12 @@ type appendFS struct { // Open opens the named file, appending the prefix to the file path before // passing it to the underlying fs.FS. func (s appendFS) Open(name string) (http.File, error) { + switch name { + {{- range $requested, $embedded := . }} + case {{ printf "%q" $requested }}: + name = {{ printf "%q" $embedded }} + {{- end }} + } return s.fs.Open(path.Join(s.prefix, name)) }