<\/figure><\/li><\/ul><\/figure>\n\n\n\nBelow is the corresponding mapping UML diagram with the practical example of nginx and application server which was described above.<\/p>\n\n\n\n <\/figure><\/li><\/ul><\/figure>\n\n\n\n<\/span>Mapping <\/strong><\/span><\/h2>\n\n\n\n The below table represents the mapping from the UML diagram actors to actual implementation actors in code <\/p>\n\n\n\nsubject<\/td> server.go<\/td><\/tr> proxy<\/td> nginx.go<\/td><\/tr> realSubject<\/td> application.go<\/td><\/tr> client<\/td> main.go<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<\/span>Practical Example:<\/strong><\/span><\/h2>\n\n\n\nserver.go<\/strong><\/p>\n\n\n\npackage main\n\ntype server interface {\n handleRequest(string, string) (int, string)\n}<\/code><\/pre>\n\n\n\nnginx.go<\/strong><\/p>\n\n\n\npackage main\n\ntype nginx struct {\n application *application\n maxAllowedRequest int\n rateLimiter map[string]int\n}\n\nfunc newNginxServer() *nginx {\n return &nginx{\n application: &application{},\n maxAllowedRequest: 2,\n rateLimiter: make(map[string]int),\n }\n}\n\nfunc (n *nginx) handleRequest(url, method string) (int, string) {\n allowed := n.checkRateLimiting(url)\n if !allowed {\n return 403, \"Not Allowed\"\n }\n return n.application.handleRequest(url, method)\n}\n\nfunc (n *nginx) checkRateLimiting(url string) bool {\n if n.rateLimiter[url] == 0 {\n n.rateLimiter[url] = 1\n }\n if n.rateLimiter[url] > n.maxAllowedRequest {\n return false\n }\n n.rateLimiter[url] = n.rateLimiter[url] + 1\n return true\n}<\/code><\/pre>\n\n\n\napplication.go<\/strong><\/p>\n\n\n\npackage main\n\ntype application struct {\n}\n\nfunc (a *application) handleRequest(url, method string) (int, string) {\n if url == \"\/app\/status\" && method == \"GET\" {\n return 200, \"Ok\"\n }\n if url == \"\/create\/user\" && method == \"POST\" {\n return 201, \"User Created\"\n }\n return 404, \"Not Ok\"\n}<\/code><\/pre>\n\n\n\nmain.go<\/strong><\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n nginxServer := newNginxServer()\n appStatusURL := \"\/app\/status\"\n createuserURL := \"\/create\/user\"\n httpCode, body := nginxServer.handleRequest(appStatusURL, \"GET\")\n fmt.Printf(\"\\nUrl: %s\\nHttpCode: %d\\nBody: %s\\n\", appStatusURL, httpCode, body)\n httpCode, body = nginxServer.handleRequest(appStatusURL, \"GET\")\n fmt.Printf(\"\\nUrl: %s\\nHttpCode: %d\\nBody: %s\\n\", appStatusURL, httpCode, body)\n httpCode, body = nginxServer.handleRequest(appStatusURL, \"GET\")\n fmt.Printf(\"\\nUrl: %s\\nHttpCode: %d\\nBody: %s\\n\", appStatusURL, httpCode, body)\n httpCode, body = nginxServer.handleRequest(createuserURL, \"POST\")\n fmt.Printf(\"\\nUrl: %s\\nHttpCode: %d\\nBody: %s\\n\", appStatusURL, httpCode, body)\n httpCode, body = nginxServer.handleRequest(createuserURL, \"GET\")\n fmt.Printf(\"\\nUrl: %s\\nHttpCode: %d\\nBody: %s\\n\", appStatusURL, httpCode, body)\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\nUrl: \/app\/status\nHttpCode: 200\nBody: Ok\n\nUrl: \/app\/status\nHttpCode: 200\nBody: Ok\n\nUrl: \/app\/status\nHttpCode: 403\nBody: Not Allowed\n\nUrl: \/app\/status\nHttpCode: 201\nBody: User Created\n\nUrl: \/app\/status\nHttpCode: 404\nBody: Not Ok<\/code><\/pre>\n\n\n\n<\/span>Full Working Code:<\/strong><\/span><\/h2>\n\n\n\npackage main\n\nimport \"fmt\"\n\ntype server interface {\n handleRequest(string, string) (int, string)\n}\n\ntype nginx struct {\n application *application\n maxAllowedRequest int\n rateLimiter map[string]int\n}\n\nfunc newNginxServer() *nginx {\n return &nginx{\n application: &application{},\n maxAllowedRequest: 2,\n rateLimiter: make(map[string]int),\n }\n}\n\nfunc (n *nginx) handleRequest(url, method string) (int, string) {\n allowed := n.checkRateLimiting(url)\n if !allowed {\n return 403, \"Not Allowed\"\n }\n return n.application.handleRequest(url, method)\n}\n\nfunc (n *nginx) checkRateLimiting(url string) bool {\n if n.rateLimiter[url] == 0 {\n n.rateLimiter[url] = 1\n }\n if n.rateLimiter[url] > n.maxAllowedRequest {\n return false\n }\n n.rateLimiter[url] = n.rateLimiter[url] + 1\n return true\n}\n\ntype application struct {\n}\n\nfunc (a *application) handleRequest(url, method string) (int, string) {\n if url == \"\/app\/status\" && method == \"GET\" {\n return 200, \"Ok\"\n }\n if url == \"\/create\/user\" && method == \"POST\" {\n return 201, \"User Created\"\n }\n return 404, \"Not Ok\"\n}\n\nfunc main() {\n nginxServer := newNginxServer()\n appStatusURL := \"\/app\/status\"\n createuserURL := \"\/create\/user\"\n httpCode, body := nginxServer.handleRequest(appStatusURL, \"GET\")\n fmt.Printf(\"\\nUrl: %s\\nHttpCode: %d\\nBody: %s\\n\", appStatusURL, httpCode, body)\n httpCode, body = nginxServer.handleRequest(appStatusURL, \"GET\")\n fmt.Printf(\"\\nUrl: %s\\nHttpCode: %d\\nBody: %s\\n\", appStatusURL, httpCode, body)\n httpCode, body = nginxServer.handleRequest(appStatusURL, \"GET\")\n fmt.Printf(\"\\nUrl: %s\\nHttpCode: %d\\nBody: %s\\n\", appStatusURL, httpCode, body)\n httpCode, body = nginxServer.handleRequest(createuserURL, \"POST\")\n fmt.Printf(\"\\nUrl: %s\\nHttpCode: %d\\nBody: %s\\n\", appStatusURL, httpCode, body)\n httpCode, body = nginxServer.handleRequest(createuserURL, \"GET\")\n fmt.Printf(\"\\nUrl: %s\\nHttpCode: %d\\nBody: %s\\n\", appStatusURL, httpCode, body)\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\nUrl: \/app\/status\nHttpCode: 200\nBody: Ok\n\nUrl: \/app\/status\nHttpCode: 200\nBody: Ok\n\nUrl: \/app\/status\nHttpCode: 403\nBody: Not Allowed\n\nUrl: \/app\/status\nHttpCode: 201\nBody: User Created\n\nUrl: \/app\/status\nHttpCode: 404\nBody: Not Ok<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Note: Interested in understanding how all other design patterns can be implemented in GO. Please see this full reference – All Design Patterns in Go (Golang) Table of Contents Introduction: UML Diagram:Mapping Practical Example:Full…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[25,3,4,71],"class_list":["post-599","post","type-post","status-publish","format-standard","hentry","category-tech","tag-design-pattern","tag-go","tag-golang","tag-proxy-design-pattern"],"yoast_head":"\n
Proxy Design Pattern in Go (Golang) - Welcome To Golang By Example<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n