冬のセール!
Proxy

Proxy を Go で

Proxy 構造に関するデザインパターンの一つで クライアントが使う本物のサービス・オブジェクトの代理として機能するオブジェクト プロキシー を提供します プロキシーは アクセス制御 キャッシングなど 何らかの作業を行なった後 リクエストをサービス・オブジェクトに渡します

プロキシー・オブジェクトはサービスと同じインターフェースを持ち クライアントにとっては 本物のオブジェクトと交換可能です

概念的な例

Nginx のようなウェブ・サーバーは アプリケーション・サーバーのプロキシーの役を果たせます

  • アプリケーション・サーバーに対するアクセス制御を行う
  • レート制限を行える
  • リクエストのキャッシングを行える

server.go: サーバー

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

nginx.go: プロキシー

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: 実際のサーバー

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: クライアント・コード

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: 実行結果

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

Proxy を C# で Proxy を C++ で Proxy を Java で Proxy を PHP で Proxy を Python で Proxy を Ruby で Proxy を Rust で Proxy を Swift で Proxy を TypeScript で