(staticFS fs.FS)
| 617 | } |
| 618 | |
| 619 | func (h *httpHandlers) handleStaticPathFiles(staticFS fs.FS) http.HandlerFunc { |
| 620 | return func(w http.ResponseWriter, r *http.Request) { |
| 621 | defer func() { |
| 622 | panicErr := util.PanicHandler("handleStaticPathFiles", recover()) |
| 623 | if panicErr != nil { |
| 624 | http.Error(w, fmt.Sprintf("internal server error: %v", panicErr), http.StatusInternalServerError) |
| 625 | } |
| 626 | }() |
| 627 | |
| 628 | // Strip /static/ prefix from the path |
| 629 | filePath := strings.TrimPrefix(r.URL.Path, "/static/") |
| 630 | if filePath == "" { |
| 631 | // Handle requests to "/static/" directly |
| 632 | if serveFileDirectly(w, r, staticFS, "/", "index.html") { |
| 633 | return |
| 634 | } |
| 635 | http.NotFound(w, r) |
| 636 | return |
| 637 | } |
| 638 | |
| 639 | // Handle directory paths ending with "/" to avoid redirect loops |
| 640 | strippedPath := "/" + filePath |
| 641 | if serveFileDirectly(w, r, staticFS, strippedPath, "index.html") { |
| 642 | return |
| 643 | } |
| 644 | |
| 645 | // Check if file exists in staticFS |
| 646 | _, err := staticFS.Open(filePath) |
| 647 | if err != nil { |
| 648 | http.NotFound(w, r) |
| 649 | return |
| 650 | } |
| 651 | |
| 652 | // Create a file server and serve the file |
| 653 | fileServer := http.FileServer(http.FS(staticFS)) |
| 654 | |
| 655 | // Temporarily modify the URL path for the file server |
| 656 | originalPath := r.URL.Path |
| 657 | r.URL.Path = "/" + filePath |
| 658 | fileServer.ServeHTTP(w, r) |
| 659 | r.URL.Path = originalPath |
| 660 | } |
| 661 | } |
no test coverage detected