feat: tipos HTTP — Middleware, Route, HTTPError
Tres tipos Go en el paquete infra para construir servidores HTTP: - Middleware: funcion que envuelve http.Handler (patron decorator) - Route: struct con Method, Path y Handler para registrar rutas - HTTPError: struct con Status, Code y Message para respuestas JSON de error Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -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.
|
||||
@@ -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.
|
||||
@@ -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.
|
||||
Reference in New Issue
Block a user