diff --git a/functions/infra/http_error.go b/functions/infra/http_error.go new file mode 100644 index 00000000..41351706 --- /dev/null +++ b/functions/infra/http_error.go @@ -0,0 +1,8 @@ +package infra + +// HTTPError representa un error HTTP estructurado para respuestas JSON. +type HTTPError struct { + Status int // codigo HTTP (ej: 400, 404, 500) + Code string // codigo de error legible por maquina (ej: "invalid_input") + Message string // mensaje de error legible por humano +} diff --git a/functions/infra/http_middleware.go b/functions/infra/http_middleware.go new file mode 100644 index 00000000..7a280a57 --- /dev/null +++ b/functions/infra/http_middleware.go @@ -0,0 +1,6 @@ +package infra + +import "net/http" + +// Middleware es una funcion que envuelve un http.Handler para añadir comportamiento. +type Middleware func(http.Handler) http.Handler diff --git a/functions/infra/http_route.go b/functions/infra/http_route.go new file mode 100644 index 00000000..eb10bff5 --- /dev/null +++ b/functions/infra/http_route.go @@ -0,0 +1,10 @@ +package infra + +import "net/http" + +// Route define una ruta HTTP con metodo, path y handler. +type Route struct { + Method string // metodo HTTP (GET, POST, PUT, DELETE, PATCH, etc.) + Path string // path de la ruta (ej: /api/users/{id}) + Handler http.Handler // handler que procesa la peticion +} diff --git a/types/infra/http_error.md b/types/infra/http_error.md new file mode 100644 index 00000000..2581e515 --- /dev/null +++ b/types/infra/http_error.md @@ -0,0 +1,32 @@ +--- +name: HTTPError +lang: go +domain: infra +version: "1.0.0" +algebraic: product +definition: | + type HTTPError struct { + Status int + Code string + Message string + } +description: "Error HTTP estructurado para respuestas JSON. Combina status code HTTP, codigo de error legible por maquina y mensaje para el usuario." +tags: [http, error, server, json, infra] +uses_types: [] +file_path: "functions/infra/http_error.go" +--- + +## Ejemplo + +```go +err := HTTPError{ + Status: 400, + Code: "invalid_input", + Message: "el campo email es requerido", +} +HTTPErrorResponse(w, err) +``` + +## Notas + +Tipo producto — los tres campos son complementarios. Status es el codigo HTTP numerico que se usa en la respuesta. Code es un identificador maquina (snake_case) para el cliente. Message es texto libre legible por humanos. Se serializa a JSON con HTTPErrorResponse. diff --git a/types/infra/http_middleware.md b/types/infra/http_middleware.md new file mode 100644 index 00000000..5d0eee38 --- /dev/null +++ b/types/infra/http_middleware.md @@ -0,0 +1,28 @@ +--- +name: Middleware +lang: go +domain: infra +version: "1.0.0" +algebraic: product +definition: "type Middleware func(http.Handler) http.Handler" +description: "Funcion que envuelve un http.Handler para añadir comportamiento (logging, auth, CORS, etc.)." +tags: [http, middleware, server, handler, infra] +uses_types: [] +file_path: "functions/infra/http_middleware.go" +--- + +## Ejemplo + +```go +// Middleware que añade un header a cada respuesta +func myMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("X-Custom", "value") + next.ServeHTTP(w, r) + }) +} +``` + +## Notas + +Tipo funcion — aplica el patron decorator sobre http.Handler. Se compone con HTTPMiddlewareChain para apilar comportamiento en orden. Cada Middleware recibe el siguiente handler y retorna un nuevo handler que puede modificar request y response. diff --git a/types/infra/http_route.md b/types/infra/http_route.md new file mode 100644 index 00000000..5894122a --- /dev/null +++ b/types/infra/http_route.md @@ -0,0 +1,31 @@ +--- +name: Route +lang: go +domain: infra +version: "1.0.0" +algebraic: product +definition: | + type Route struct { + Method string + Path string + Handler http.Handler + } +description: "Define una ruta HTTP con metodo, path y handler. Se usa con HTTPRouter para registrar rutas en un ServeMux." +tags: [http, route, server, router, infra] +uses_types: [] +file_path: "functions/infra/http_route.go" +--- + +## Ejemplo + +```go +routes := []Route{ + {Method: "GET", Path: "/api/health", Handler: http.HandlerFunc(healthHandler)}, + {Method: "POST", Path: "/api/users", Handler: http.HandlerFunc(createUserHandler)}, +} +mux := HTTPRouter(routes) +``` + +## Notas + +Tipo producto — los tres campos son obligatorios. Method debe ser un verbo HTTP valido en mayusculas. Path sigue la sintaxis del ServeMux de Go 1.22+ (soporta parametros como {id}). Handler puede ser cualquier http.Handler o http.HandlerFunc.