{"id":1765,"date":"2020-10-12T06:04:32","date_gmt":"2020-10-11T22:04:32","guid":{"rendered":"https:\/\/www.develop-note.com\/blog\/?p=1765"},"modified":"2022-02-16T21:50:59","modified_gmt":"2022-02-16T13:50:59","slug":"iris-routing-2021ironman","status":"publish","type":"post","link":"https:\/\/www.develop-note.com\/blog\/2020\/10\/12\/iris-routing-2021ironman\/","title":{"rendered":"Iris \u9019\u500b\u5728 go \u8a9e\u8a00\u4e0a\u5730\u8868\u6700\u5feb\u7684\u7db2\u9801\u6846\u67b6-iris\u7684routing"},"content":{"rendered":"<h1>routing<\/h1>\n<p>\u5728\u4ecb\u7d39\u5b8cmodel\u9a57\u8b49\u4e4b\u5f8c\u8981\u958b\u59cb\u4ecb\u7d39\u7db2\u9801\u7a0b\u5f0f\u8a2d\u8a08\u7684\u53e6\u4e00\u500b\u89c0\u5ff5\u8def\u7531<code>routing<\/code>\uff0c\u66f4\u660e\u78ba\u7684\u8aaa\u660e\u61c9\u8a72\u662f\u7db2\u5740\u8def\u7531\u624d\u5c0d\uff0c\u6240\u4ee5\u63a5\u4e0b\u4f86\u958b\u59cb\u8ddf\u5927\u5bb6\u4ecb\u7d39\u7db2\u5740\u8def\u7531\u3002<\/p>\n<p><!--more--><\/p>\n<h2>routing<\/h2>\n<p>\u8def\u7531\u5e38\u6703\u8b93\u4eba\u806f\u60f3\u5230\u7684\u662f\u7db2\u8def\u5c64\u7684\u8def\u7531\uff0c\u7c21\u55ae\u4f86\u8aaa\u5c31\u5982<a href=\"https:\/\/zh.m.wikipedia.org\/zh-tw\/\u8def\u7531\" title=\"\u8def\u7531\" rel=\"nofollow noopener\" target=\"_blank\">\u7dad\u57fa\u767e\u79d1\u4e0a\u5c0d\u8def\u7531\u7684\u8aaa\u660e<\/a>\uff0c\u5c07\u7db2\u8def\u8a0a\u606f\u50b3\u5230\u5c0d\u61c9\u7684\u5730\u65b9\uff0c\u4e0d\u904e\u9019\u88e1\u5b9a\u7fa9\u7684\u8def\u7531\u660e\u78ba\u4f86\u8aaa\u662f\u7db2\u5740\u8def\u7531\uff0c\u63a5\u4e0b\u4f86\u8ddf\u5927\u5bb6\u5148\u8aaa\u660e\u4e00\u4e0b\u7db2\u5740\u8def\u7531\uff0c\u518d\u8ddf\u5927\u5bb6\u8aaa\u660e\u5982\u4f55\u5728iris\u4e0a\u900f\u904e\u7db2\u5740\u8def\u7531\u4f86\u8655\u7406\u4f7f\u7528\u8005\u7684\u8acb\u6c42\u3002<\/p>\n<h3>\u4ec0\u9ebc\u662frouting<\/h3>\n<p>\u4ec0\u9ebc\u662f\u7db2\u5740\u8def\u7531\u5462\uff0c\u7c21\u55ae\u8b1b\u5c31\u662f\u5728\u7db2\u5740\u4e0a\u50b3\u905e\u6709\u610f\u7fa9\u7684\u8cc7\u8a0a\uff0c\u4f8b\u5982\u4e0b\u5217\u7db2\u5740<\/p>\n<pre><code class=\"language-bash\">example.com\/api\/users\/2345\/profile<\/code><\/pre>\n<p>\u5982\u6b64\u53ef\u4ee5\u77e5\u9053\u4ed6\u662f\u8981\u88ab\u5c0e\u5230users\u7684profile\u7684\u9019\u500b<code>handler<\/code>\u4e26\u4e14\u544a\u8a34\u4f3a\u670d\u5668\u4f7f\u7528\u8005\u7de8\u865f\u662f2345\u3002<br \/>\n\u6240\u4ee5\u63a5\u4e0b\u4f86\u8981\u544a\u8a34\u5927\u5bb6\u5728iris\u8981\u5982\u4f55\u8655\u7406\u9019\u4e00\u985e\u7684\u8acb\u6c42\uff0c\u4e26\u80fd\u5c0e\u5230\u6b63\u78ba\u7684<code>handler<\/code>\uff0c\u4e26\u4e14\u80fd\u53d6\u5f97\u6b63\u78ba\u7684\u4f7f\u7528\u8005\u7de8\u865f2345\u3002<\/p>\n<h3>\u600e\u9ebc\u5728iris\u4e0a\u8655\u7406routing\u554f\u984c<\/h3>\n<p>\u9019\u88e1\u6211\u5011\u62ff<a href=\"https:\/\/docs.iris-go.com\/iris\/contents\/routing\" title=\"iris-go routing\" rel=\"nofollow noopener\" target=\"_blank\">iris-go routing<\/a>\u7684\u7bc4\u4f8b\u4f86\u8ddf\u5927\u5bb6\u8aaa\u660e\u5982\u4f55\u4f7f\u7528iris\u4f86\u8655\u7406\u7db2\u5740\u8def\u7531\u7684\u8acb\u6c42\uff0c\u9996\u5148\u5927\u5bb6\u5148\u770b\u770b\u4e0b\u5217\u4f8b\u5b50<\/p>\n<pre><code class=\"language-go\">app := iris.New()\n\n\/\/ http:\/\/localhost:8080\/api\/users\/2345\/profile\napp.Get(&quot;api\/users\/{id:uint64}\/profile&quot;, userProfileHandler)<\/code><\/pre>\n<p>\u5982\u6b64\u5c31\u80fd\u5920\u5c07\u524d\u9762\u8aaa\u660e\u7684\u7db2\u5740\u7d81\u5b9a\u5230userProfileHandler\u9019\u500b<code>handler<\/code>\u4e0a\uff0c\u9664\u4e86\u9019\u6a23\u5b50\u7d81\u5b9a\u5230\u5c0d\u61c9\u7684<code>handler<\/code>\u4e4b\u5916iris\u9084\u53ef\u4ee5\u5c07<code>handler<\/code>\u5206\u7fa4\uff0c\u63a5\u4e0b\u4f86\u5728\u8aaa\u660e<code>handler<\/code>\u5206\u7fa4\u3002<\/p>\n<h3>group routing<\/h3>\n<p>\u4e0a\u8ff0\u4f8b\u5b50\u8aaa\u660e\u4e86\u5982\u4f55\u8655\u7406\u7db2\u5740\u8def\u7531\uff0c\u5982\u679c\u4eca\u5929\u53c8\u5728\u591a\u4e00\u500b\u7db2\u5740\u5982\u4e0b\u90a3\u8a72\u5982\u4f55\u8655\u7406\u5462<\/p>\n<pre><code class=\"language-bash\">example.com\/api\/users\/messages\/1<\/code><\/pre>\n<p>\u7531\u65bc\u76ee\u524d\u7684\u7db2\u5740\u8207\u4e0a\u8ff0\u7684\u7db2\u5740\u6709\u4e00\u5b9a\u7684\u76f8\u4f3c\u5ea6\uff0c\u6240\u5b9a\u53ef\u4ee5\u5c07\u5169\u500b\u7db2\u5740\u8def\u7531\u5206\u5728\u4e00\u500b\u7fa4\u7d44\uff0c\u6240\u4ee5\u5728iris\u7684\u8655\u7406\u5982\u4e0b\u6240\u793a<\/p>\n<pre><code class=\"language-go\">app := iris.New()\n\nusers := app.Party(&quot;api\/users&quot;, myAuthMiddlewareHandler)\n\n\/\/ http:\/\/localhost:8080\/api\/users\/42\/profile\nusers.Get(&quot;\/{id:uint64}\/profile&quot;, userProfileHandler)\n\/\/ http:\/\/localhost:8080\/api\/users\/messages\/1\nusers.Get(&quot;\/messages\/{id:uint64}&quot;, userMessageHandler)<\/code><\/pre>\n<p>\u9664\u4e86\u4e0a\u8ff0\u7684\u7d81\u5b9a\u8a72\u7db2\u5740\u5230<code>handler<\/code>\u4e4b\u5916\uff0c\u9084\u6709\u53e6\u5916\u4e00\u7a2e<code>PartyFunc<\/code>\u7684\u5beb\u6cd5\uff0c\u5982\u4e0b\u5217\u4f8b\u5b50<\/p>\n<pre><code class=\"language-go\">app := iris.New()\n\napp.PartyFunc(&quot;api\/users&quot;, func(users iris.Party) {\n    users.Use(myAuthMiddlewareHandler)\n\n    \/\/ http:\/\/localhost:8080\/api\/users\/42\/profile\n    users.Get(&quot;\/{id:uint64}\/profile&quot;, userProfileHandler)\n    \/\/ http:\/\/localhost:8080\/api\/users\/messages\/1\n    users.Get(&quot;\/messages\/{id:uint64}&quot;, userMessageHandler)\n})<\/code><\/pre>\n<p>\u9019\u5169\u7a2e\u5206\u7fa4\u7684\u65b9\u5f0f\u7d50\u679c\u90fd\u4e00\u6a23\uff0c\u5c31\u770b\u5927\u5bb6\u7fd2\u6163\u600e\u9ebc\u5beb\uff0c\u6240\u4ee5\u5c0d\u65bc\u7db2\u5740\u8def\u7531\u7d81\u5b9a\u5230\u5c0d\u61c9\u7684<code>handler<\/code>\u5230\u6b64\u544a\u4e00\u6bb5\u843d\uff0c\u4e0d\u904e\u6211\u5011\u9084\u6709\u4e00\u500b\u6c92\u6709\u8aaa\u660e\u5230\uff0c\u63a5\u4e0b\u4f86\u8ddf\u5927\u5bb6\u8aaa\u660e\u5982\u4f55\u53d6\u5f97\u7db2\u5740\u4e0a\u5c0d\u61c9\u7684\u53c3\u6578\u3002<\/p>\n<h3>path parameters<\/h3>\n<p>\u4e0a\u9762\u4f8b\u5b50\u90fd\u662f\u8aaa\u660e\u8a72\u5982\u4f55\u7d81\u5b9a\u5230\u5c0d\u61c9\u7684<code>handler<\/code>\uff0c\u4f46\u662f\u6c92\u6709\u8aaa\u660e\u8a72\u5982\u4f55\u5f9e\u7db2\u5740\u4e0a\u53d6\u51fa\u53c3\u6578\uff0c\u6240\u4ee5\u63a5\u4e0b\u4f86\u5148\u770b\u4e00\u4e0b\u4e0b\u9762\u4f8b\u5b50<\/p>\n<pre><code class=\"language-go\">func userProfileHandler(ctx iris.Context) {\n    ctx.Writef(&quot;id (uint64): %s&quot;, ctx.Params().GetUint64(&quot;id&quot;))\n}<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4f8b\u5b50\u662f\u5be6\u4f5c\u4e00\u500b<code>handler<\/code>\u5c08\u9580\u8655\u7406\u4e0b\u5217\u7db2\u5740<\/p>\n<pre><code class=\"language-bash\">example.com\/api\/users\/2345\/profile<\/code><\/pre>\n<p>\u5176\u4e2d\u5c31\u662f\u900f\u904ectx.Params().GetUint64(&quot;id&quot;)\u4f86\u53d6\u5f97\u53c3\u65782345\u3002<\/p>\n<h3>\u7db2\u5740\u8def\u7531\u7684\u6ce8\u610f\u4e8b\u9805<\/h3>\n<p>\u5728iris\u7684\u7db2\u5740\u8def\u7531\u7684\u8655\u7406\u80fd\u5920\u6b63\u78ba\u7684\u7d81\u5b9a\u5230\u5c0d\u61c9\u7684<code>handler<\/code>\uff0c\u4f46\u662f\u9019\u90e8\u5206\u8981\u6ce8\u610f\u7684\u662f\u5c0d\u65bc\u7db2\u5740\u7d81\u5b9a\u7684\u908f\u8f2f\u8a8d\u77e5\u932f\u8aa4\u7684\u72c0\u6cc1\uff0c\u8acb\u5927\u5bb6\u770b\u770b\u4ee5\u4e0b\u5169\u7a2e\u7d81\u5b9a\u65b9\u5f0f<\/p>\n<pre><code class=\"language-go\">app.Get(&quot;\/profile\/{username:string}&quot;, userHandler)\napp.Get(&quot;\/profile\/me&quot;, userHandler2)<\/code><\/pre>\n<p>\u4f9d\u64da\u4e0a\u9762\u7684\u7d81\u5b9a\u65b9\u5f0f\uff0c\u5176\u4e2d\/profile\/me\u6703\u88ab\u7d81\u5b9a\u5230userHandler2\uff0c\u5176\u4ed6\u7684\/profile\/*\u6703\u88ab\u7d81\u5b9a\u5230userHandler\uff0c\u82e5\u662f\u4e0d\u662f\u9019\u6a23\u7684\u8a8d\u77e5\u5c31\u6703\u9020\u6210\u7db2\u9801\u908f\u8f2f\u7684\u8aa4\u5224\u3002\u6700\u5f8c\u8acb\u770b\u4e00\u4e0b<a href=\"https:\/\/github.com\/kataras\/iris\/wiki\/Routing-path-parameter-types\" title=\"\u7db2\u5740\u8def\u7531\u53c3\u6578\u7684\u985e\u5225\" rel=\"nofollow noopener\" target=\"_blank\">\u7db2\u5740\u8def\u7531\u53c3\u6578\u7684\u985e\u5225<\/a>\u5c0d\u65bc\u7db2\u8def\u8def\u7531\u7d81\u5b9a\u6709\u5f88\u5927\u7684\u5e6b\u52a9\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u4eca\u5929\u8ddf\u5927\u5bb6\u5206\u4eab\u5982\u4f55\u5728iris\u4e0a\u8655\u7406\u900f\u904e\u7db2\u5740\u800c\u767c\u51fa\u4f86\u7684\u8acb\u6c42\uff0c\u5e0c\u671b\u5927\u5bb6\u5c0d\u65bc\u7db2\u5740\u8def\u7531\u5df2\u7d93iris\u5982\u4f55\u8655\u7406\u7db2\u5740\u8def\u7531\u6709\u66f4\u9032\u4e00\u6b65\u7684\u77ad\u89e3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>routing \u5728\u4ecb\u7d39\u5b8cmodel\u9a57\u8b49\u4e4b\u5f8c\u8981\u958b\u59cb\u4ecb\u7d39\u7db2\u9801\u7a0b\u5f0f\u8a2d\u8a08\u7684\u53e6\u4e00\u500b\u89c0\u5ff5\u8def\u7531routing\uff0c\u66f4\u660e\u78ba\u7684\u8aaa\u660e\u61c9 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.develop-note.com\/blog\/2020\/10\/12\/iris-routing-2021ironman\/\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008Iris \u9019\u500b\u5728 go \u8a9e\u8a00\u4e0a\u5730\u8868\u6700\u5feb\u7684\u7db2\u9801\u6846\u67b6-iris\u7684routing\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[158,81,84],"class_list":["post-1765","post","type-post","status-publish","format-standard","hentry","category-develop","tag-2020ironman","tag-go","tag-iris"],"_links":{"self":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1765","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/comments?post=1765"}],"version-history":[{"count":25,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1765\/revisions"}],"predecessor-version":[{"id":2952,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1765\/revisions\/2952"}],"wp:attachment":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/media?parent=1765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/categories?post=1765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/tags?post=1765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}