b074313c01
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>
18 lines
400 B
Go
18 lines
400 B
Go
package infra
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
// HTTPRouter crea un http.ServeMux y registra las rutas dadas.
|
|
// Usa la sintaxis de Go 1.22+: "METHOD /path" como patron del mux.
|
|
func HTTPRouter(routes []Route) *http.ServeMux {
|
|
mux := http.NewServeMux()
|
|
for _, route := range routes {
|
|
pattern := fmt.Sprintf("%s %s", route.Method, route.Path)
|
|
mux.Handle(pattern, route.Handler)
|
|
}
|
|
return mux
|
|
}
|