Files
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.5 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_router function go infra 1.0.0 impure func HTTPRouter(routes []Route) *http.ServeMux Crea un http.ServeMux y registra las rutas dadas usando la sintaxis de Go 1.22+ (METHOD /path). Retorna el mux listo para usar con http.ListenAndServe.
http
router
mux
server
routes
infra
Route_go_infra
false error_go_core
fmt
net/http
name desc
routes slice de Route con Method, Path y Handler a registrar en el mux
http.ServeMux configurado con todas las rutas true
registra una ruta y responde correctamente
registra multiples rutas en el mismo mux
ruta no registrada retorna 404
functions/infra/http_server_test.go functions/infra/http_router.go

Ejemplo

mux := HTTPRouter([]Route{
    {Method: "GET",  Path: "/api/health", Handler: http.HandlerFunc(healthHandler)},
    {Method: "POST", Path: "/api/users",  Handler: http.HandlerFunc(createUserHandler)},
    {Method: "GET",  Path: "/api/users/{id}", Handler: http.HandlerFunc(getUserHandler)},
})
http.ListenAndServe(":8080", mux)

Notas

Usa la sintaxis "METHOD /path" de Go 1.22+ para registrar rutas con metodo especifico. Soporta parametros de path como {id} accesibles via r.PathValue("id"). Sin rutas registradas retorna un mux vacio que responde 404 a todo.