serveFileDirectly serves a file directly from an embed.FS to avoid redirect loops when serving directory paths that end with "/"
(w http.ResponseWriter, r *http.Request, embeddedFS fs.FS, requestPath, fileName string)
| 520 | // serveFileDirectly serves a file directly from an embed.FS to avoid redirect loops |
| 521 | // when serving directory paths that end with "/" |
| 522 | func serveFileDirectly(w http.ResponseWriter, r *http.Request, embeddedFS fs.FS, requestPath, fileName string) bool { |
| 523 | if !strings.HasSuffix(requestPath, "/") { |
| 524 | return false |
| 525 | } |
| 526 | |
| 527 | // Try to serve the specified file from that directory |
| 528 | var filePath string |
| 529 | if requestPath == "/" { |
| 530 | filePath = fileName |
| 531 | } else { |
| 532 | filePath = strings.TrimPrefix(requestPath, "/") + fileName |
| 533 | } |
| 534 | |
| 535 | file, err := embeddedFS.Open(filePath) |
| 536 | if err != nil { |
| 537 | return false |
| 538 | } |
| 539 | defer file.Close() |
| 540 | |
| 541 | // Get file info for modification time |
| 542 | fileInfo, err := file.Stat() |
| 543 | if err != nil { |
| 544 | return false |
| 545 | } |
| 546 | |
| 547 | // Serve the file directly with proper mod time |
| 548 | http.ServeContent(w, r, fileName, fileInfo.ModTime(), file.(io.ReadSeeker)) |
| 549 | return true |
| 550 | } |
| 551 | |
| 552 | func (h *httpHandlers) handleStaticFiles(embeddedFS fs.FS) http.HandlerFunc { |
| 553 | fileServer := http.FileServer(http.FS(embeddedFS)) |
no test coverage detected