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>
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
---
|
||||
name: http_logger_middleware
|
||||
kind: function
|
||||
lang: go
|
||||
domain: infra
|
||||
version: "1.0.0"
|
||||
purity: impure
|
||||
signature: "func HTTPLoggerMiddleware(logger io.Writer) Middleware"
|
||||
description: "Retorna un Middleware que loguea metodo, path, status code y duracion de cada request HTTP. El formato es: METHOD /path STATUS DURACIONms"
|
||||
tags: [http, logger, middleware, logging, server, infra]
|
||||
uses_functions: []
|
||||
uses_types: [Middleware_go_infra]
|
||||
returns: [Middleware_go_infra]
|
||||
returns_optional: false
|
||||
error_type: "error_go_core"
|
||||
imports: [fmt, io, net/http, time]
|
||||
params:
|
||||
- name: logger
|
||||
desc: "io.Writer donde se escriben las lineas de log (ej: os.Stderr, un archivo, bytes.Buffer)"
|
||||
output: "Middleware que intercepta cada request, llama al siguiente handler, y loguea los detalles al terminar"
|
||||
tested: true
|
||||
tests: ["loguea metodo y path del request", "loguea el status code de la respuesta", "loguea la duracion en milisegundos"]
|
||||
test_file_path: "functions/infra/http_server_test.go"
|
||||
file_path: "functions/infra/http_logger_middleware.go"
|
||||
---
|
||||
|
||||
## Ejemplo
|
||||
|
||||
```go
|
||||
logger := HTTPLoggerMiddleware(os.Stderr)
|
||||
chain := HTTPMiddlewareChain(logger)
|
||||
// Cada request imprime: GET /api/users 200 5ms
|
||||
http.ListenAndServe(":8080", chain(mux))
|
||||
```
|
||||
|
||||
## Notas
|
||||
|
||||
Captura el status code con un responseWriter envolvente que intercepta WriteHeader. Si el handler no llama WriteHeader explicitamente, el status por defecto es 200. El logger recibe cualquier io.Writer — util para tests usando bytes.Buffer o para escribir a archivos de log.
|
||||
Reference in New Issue
Block a user