Files
fn_registry/functions/infra/http_serve.md
T
egutierrez b074313c01 feat: funciones impuras HTTP — response, parse, logger, router, serve
Seis funciones de servidor HTTP con tests usando httptest:
- HTTPJSONResponse: escribe JSON con Content-Type y status code
- HTTPErrorResponse: escribe HTTPError como JSON estructurado
- HTTPParseBody: decode JSON con limite de bytes y campos estrictos
- HTTPLoggerMiddleware: loguea method/path/status/duration a io.Writer
- HTTPRouter: crea ServeMux con rutas Go 1.22+ (METHOD /path)
- HTTPServe: ListenAndServe con graceful shutdown por contexto

23 tests pasando, solo stdlib net/http.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-13 01:57:47 +02:00

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
http_serve function go infra 1.0.0 impure func HTTPServe(addr string, handler http.Handler, ctx context.Context) error Arranca un servidor HTTP en addr y realiza graceful shutdown cuando ctx se cancela. Espera hasta 30 segundos para que las conexiones activas cierren.
http
server
serve
graceful
shutdown
context
infra
false error_go_core
context
fmt
net/http
time
name desc
addr direccion donde escuchar (ej: ":8080", "0.0.0.0:9090")
name desc
handler http.Handler que procesa las peticiones (normalmente un *http.ServeMux con middlewares)
name desc
ctx contexto que al cancelarse dispara el graceful shutdown del servidor
nil si el servidor se detuvo correctamente por cancelacion de contexto, error si fallo al arrancar o al hacer shutdown true
servidor arranca y responde peticiones
graceful shutdown al cancelar contexto
functions/infra/http_server_test.go functions/infra/http_serve.go

Ejemplo

ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()

mux := HTTPRouter(routes)
chain := HTTPMiddlewareChain(HTTPLoggerMiddleware(os.Stderr))
if err := HTTPServe(":8080", chain(mux), ctx); err != nil {
    log.Fatal(err)
}

Notas

Arranca ListenAndServe en una goroutine y espera en un select: si el servidor falla devuelve el error inmediatamente, si ctx se cancela hace Shutdown con timeout de 30s. El patron estandar es usar signal.NotifyContext con SIGINT/SIGTERM para el graceful shutdown.