package infra import ( "fmt" "net/http" "strings" ) // FileServe retorna un http.Handler que sirve archivos estaticos desde dir. // Stripea pathPrefix del request URL antes de buscar el archivo, y setea el // header Cache-Control con max-age=maxAge segundos. // // El handler rechaza cualquier path que contenga ".." para mitigar path traversal, // aunque http.FileServer ya hace su propia normalizacion. func FileServe(dir string, pathPrefix string, maxAge int) http.Handler { fs := http.FileServer(http.Dir(dir)) cacheControl := fmt.Sprintf("public, max-age=%d", maxAge) return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Rechazar path traversal explicito if strings.Contains(r.URL.Path, "..") { http.Error(w, "path traversal not allowed", http.StatusBadRequest) return } w.Header().Set("Cache-Control", cacheControl) http.StripPrefix(pathPrefix, fs).ServeHTTP(w, r) }) }