Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly handle serving renamed files #3593

Merged
merged 1 commit into from
Sep 21, 2024
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
15 changes: 14 additions & 1 deletion http/codegen/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
6 changes: 6 additions & 0 deletions http/codegen/templates/append_fs.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down