Autumn SALE
Proxy

Proxy en Go

Proxy es un patrón de diseño estructural que proporciona un objeto que actúa como sustituto de un objeto de servicio real utilizado por un cliente. Un proxy recibe solicitudes del cliente, realiza parte del trabajo (control de acceso, almacenamiento en caché, etc.) y después pasa la solicitud a un objeto de servicio.

El objeto proxy tiene la misma interfaz que un servicio, lo que lo hace intercambiable con un objeto real cuando se pasa a un cliente.

Ejemplo conceptual

Un servidor web como Nginx puede actuar como proxy del servidor de tu aplicación:

  • Ofrece un acceso controlado al servidor de tu aplicación.
  • Puede realizar limitación de velocidad.
  • Puede guardar solicitudes en caché.

server.go: Sujeto

package main type server interface {	handleRequest(string, string) (int, string) } 

nginx.go: Proxy

package main type Nginx struct {	application *Application	maxAllowedRequest int	rateLimiter map[string]int } func newNginxServer() *Nginx {	return &Nginx{	application: &Application{},	maxAllowedRequest: 2,	rateLimiter: make(map[string]int),	} } func (n *Nginx) handleRequest(url, method string) (int, string) {	allowed := n.checkRateLimiting(url)	if !allowed {	return 403, "Not Allowed"	}	return n.application.handleRequest(url, method) } func (n *Nginx) checkRateLimiting(url string) bool {	if n.rateLimiter[url] == 0 {	n.rateLimiter[url] = 1	}	if n.rateLimiter[url] > n.maxAllowedRequest {	return false	}	n.rateLimiter[url] = n.rateLimiter[url] + 1	return true } 

application.go: Sujeto real

package main type Application struct { } func (a *Application) handleRequest(url, method string) (int, string) {	if url == "/app/status" && method == "GET" {	return 200, "Ok"	}	if url == "/create/user" && method == "POST" {	return 201, "User Created"	}	return 404, "Not Ok" } 

main.go: Código cliente

package main import "fmt" func main() {	nginxServer := newNginxServer()	appStatusURL := "/app/status"	createuserURL := "/create/user"	httpCode, body := nginxServer.handleRequest(appStatusURL, "GET")	fmt.Printf("\nUrl: %s\nHttpCode: %d\nBody: %s\n", appStatusURL, httpCode, body)	httpCode, body = nginxServer.handleRequest(appStatusURL, "GET")	fmt.Printf("\nUrl: %s\nHttpCode: %d\nBody: %s\n", appStatusURL, httpCode, body)	httpCode, body = nginxServer.handleRequest(appStatusURL, "GET")	fmt.Printf("\nUrl: %s\nHttpCode: %d\nBody: %s\n", appStatusURL, httpCode, body)	httpCode, body = nginxServer.handleRequest(createuserURL, "POST")	fmt.Printf("\nUrl: %s\nHttpCode: %d\nBody: %s\n", appStatusURL, httpCode, body)	httpCode, body = nginxServer.handleRequest(createuserURL, "GET")	fmt.Printf("\nUrl: %s\nHttpCode: %d\nBody: %s\n", appStatusURL, httpCode, body) } 

output.txt: Resultado de la ejecución

Url: /app/status HttpCode: 200 Body: Ok Url: /app/status HttpCode: 200 Body: Ok Url: /app/status HttpCode: 403 Body: Not Allowed Url: /app/status HttpCode: 201 Body: User Created Url: /app/status HttpCode: 404 Body: Not Ok 

Proxy en otros lenguajes

Proxy en C# Proxy en C++ Proxy en Java Proxy en PHP Proxy en Python Proxy en Ruby Proxy en Rust Proxy en Swift Proxy en TypeScript