Files
fn_registry/functions/infra/file_serve.md
T

1.7 KiB

name, kind, lang, domain, version, purity, signature, description, tags, uses_functions, uses_types, returns, returns_optional, error_type, imports, params, output, tested, tests, test_file_path, file_path
name kind lang domain version purity signature description tags uses_functions uses_types returns returns_optional error_type imports params output tested tests test_file_path file_path
file_serve function go infra 1.0.0 impure func FileServe(dir string, pathPrefix string, maxAge int) http.Handler Retorna un http.Handler que sirve archivos estaticos desde dir, stripeando pathPrefix del URL. Setea Cache-Control con max-age. Rechaza paths con "..".
http
file
serve
static
cache
security
infra
false error_go_core
fmt
net/http
strings
name desc
dir directorio raiz desde donde se sirven los archivos
name desc
pathPrefix prefijo del URL a remover antes de buscar (ej: "/files/")
name desc
maxAge segundos para el header Cache-Control max-age
http.Handler listo para registrar en un mux. No retorna error directo; el handler responde 400 si detecta path traversal y delega al http.FileServer en otros casos true
sirve archivo existente con headers de cache
responde 404 para archivo inexistente
rechaza path con .. con 400
functions/infra/file_serve_test.go functions/infra/file_serve.go

Ejemplo

mux := http.NewServeMux()
mux.Handle("/files/", FileServe("./uploads", "/files/", 3600))
http.ListenAndServe(":8080", mux)

Notas

Wrapper sobre http.FileServer con dos refuerzos: rechazo explicito de paths con .. y header Cache-Control configurable. http.FileServer ya normaliza paths, pero la doble verificacion es barata y reduce la superficie de ataque. Para servir archivos generados dinamicamente o detras de auth, no usar esta funcion — usar handlers custom.