{"id":42318,"date":"2024-12-01T08:14:10","date_gmt":"2024-12-01T00:14:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/42318\/"},"modified":"2024-12-01T08:14:10","modified_gmt":"2024-12-01T00:14:10","slug":"%e5%bc%ba%e5%88%b6-gitlab-%e5%9c%a8-go-%e5%a4%b1%e8%b4%a5%e6%97%b6%e9%87%8d%e8%af%95-webhook","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/42318\/","title":{"rendered":"\u5f3a\u5236 Gitlab \u5728 Go \u5931\u8d25\u65f6\u91cd\u8bd5 Webhook"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>\u5f3a\u5236 Gitlab \u5728 Go \u5931\u8d25\u65f6\u91cd\u8bd5 Webhook<\/span><\/p>\n<h1>\u5f3a\u5236 Gitlab \u5728 Go \u5931\u8d25\u65f6\u91cd\u8bd5 Webhook<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-23 15:03:33<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5f3a\u5236 Gitlab \u5728 Go \u5931\u8d25\u65f6\u91cd\u8bd5 Webhook\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u60f3\u76d1\u89c6 Gitlab \u9879\u76ee\u4e2d\u7684\u6bcf\u4e2a\u4e8b\u4ef6\u5e76\u5c06\u5b83\u4eec\u5b58\u50a8\u5728\u5916\u90e8\u670d\u52a1\u4e2d\u3002\u4e3a\u6b64\uff0c\u6211\u4f7f\u7528 Gitlab Webhooks\u3002\u6211\u7528 Go \u521b\u5efa\u4e86\u4e00\u4e2a\u5c0f\u578b\u672c\u5730 HTTP \u670d\u52a1\u5668\uff0c\u7528\u4e8e\u76d1\u542c Gitlab \u7684 POST \u5e76\u5c06\u5176\u8f6c\u53d1\u5230\u5916\u90e8\u670d\u52a1\u3002 Hooks \u5305\u542b\u6211\u9700\u8981\u7684\u6240\u6709\u4fe1\u606f\uff0c\u56e0\u6b64\u770b\u8d77\u6765\u8fd9\u4e2a\u67b6\u6784\u5f88\u597d\uff1a<\/p>\n<p> Gitlab &gt; HTTPServer &gt; \u5916\u90e8\u670d\u52a1\u3002 <\/p>\n<p>\u6211\u7684\u95ee\u9898\u662f\u5f53\u5916\u90e8\u670d\u52a1\u5173\u95ed\u65f6\uff0c\u6211\u65e0\u6cd5\u8bbe\u6cd5\u8ba9 Gitlab \u91cd\u8bd5\u5931\u8d25\u7684\u8bf7\u6c42\u3002\u6b63\u5982\u6587\u6863\u6240\u8ff0\uff1a<\/p>\n<ul>\n<li><em>GitLab \u4f1a\u5ffd\u7565\u60a8\u7684\u7aef\u70b9\u8fd4\u56de\u7684 HTTP \u72b6\u6001\u4ee3\u7801\u3002<\/em><\/li>\n<li><em>\u60a8\u7684\u7aef\u70b9\u5e94\u59cb\u7ec8\u8fd4\u56de\u6709\u6548\u7684 HTTP \u54cd\u5e94\u3002\u5982\u679c\u60a8\u4e0d\u8fd9\u6837\u505a\uff0cGitLab \u5c06\u8ba4\u4e3a\u6302\u94a9\u5931\u8d25\u5e76\u91cd\u8bd5\u3002<\/em><\/li>\n<\/ul>\n<p>\u4ee4\u4eba\u60ca\u8bb6\u7684\u662f\uff0cGitlab \u6ca1\u6709\u9002\u5f53\u7684\u65b9\u6cd5\u6765\u8bf7\u6c42 webhook \u91cd\u8bd5\u3002\u6211\u5fc5\u987b\u660e\u786e\u8fd4\u56de\u65e0\u6548\u7684 http \u54cd\u5e94\u3002\u6b64\u5916\uff0c\u6211\u627e\u4e0d\u5230 API \u7aef\u70b9\u6765\u5217\u51fa\u6240\u6709\u5931\u8d25\u7684 Webhook \u5e76\u8bf7\u6c42\u91cd\u65b0\u53d1\u9001\u3002<\/p>\n<p>\u95ee\u9898\uff1a\u5982\u4f55\u4f7f\u7528\u6807\u51c6\u201cnet\/http\u201d\u5e93\u663e\u5f0f\u8fd4\u56de\u65e0\u6548\u7684 HTTP \u54cd\u5e94\uff0c\u4ee5\u5f3a\u5236 Gitlab \u91cd\u8bd5 Webhooks\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u6b63\u5982\u8bc4\u8bba\u4e2d\u6240\u5199\uff0cwebhook \u53ea\u662f\u4e8b\u4ef6\u53d1\u751f\u7684\u901a\u77e5\uff0c\u5e76\u4e14\u53ef\u80fd\u4f1a\u53d1\u9001\u4e00\u4e9b\u6570\u636e\uff08\u901a\u5e38\u4e3a json \u6570\u636e\uff09\u3002<\/p>\n<p>\u60a8\u6709\u8d23\u4efb\u4fdd\u7559\u4e8b\u4ef6\u672c\u8eab\u4ee5\u53ca\u60a8\u60f3\u8981\/\u9700\u8981\u5904\u7406\u968f\u4e8b\u4ef6\u4e00\u8d77\u53d1\u9001\u7684\u6570\u636e\u3002\u60a8\u5c06\u5728\u4e0b\u9762\u627e\u5230\u4e00\u4e2a\u5e26\u6ce8\u91ca\u7684\u793a\u4f8b\u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u4e0d\u5305\u62ec\u589e\u91cf\u9000\u907f\uff0c\u4f46\u8fd9\u5e94\u8be5\u5f88\u5bb9\u6613\u6dfb\u52a0\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"encoding\/json\"\n    \"flag\"\n    \"io\"\n    \"log\"\n    \"net\/http\"\n    \"os\"\n    \"path\/filepath\"\n\n    \"github.com\/joncrlsn\/dque\"\n)\n\nvar (\n    bind        string\n    queuedir    string\n    segmentsize int\n)\n\n\/\/ you might want to add request headers and stuff\ntype webhookcontent struct {\n    foo string\n    bar int\n}\n\nfunc init() {\n    flag.stringvar(&amp;bind, \"bind\", \":8080\", \"the address to bind to\")\n    flag.stringvar(&amp;queuedir, \"path\", \".\/queue\", \"path to store the queue in\")\n    flag.intvar(&amp;segmentsize, \"size\", 50, \"number of entries for the queue\")\n}\n\n\/\/ the \"webserver\" component\nfunc runserver(q *dque.dque) {\n\n    http.handlefunc(\"\/webhook\", func(w http.responsewriter, r *http.request) {\n        \/\/ a new decoder for each call, as we want to have a new limitreader\n        \/\/ for each call. this is a simple, albeit a bit crude method to prevent\n        \/\/ accidental or malicious overload of your server.\n        dec := json.newdecoder(io.limitreader(r.body, 4096))\n\n        defer r.body.close()\n\n        c := &amp;webhookcontent{}\n        if err := dec.decode(c); err != nil {\n            log.printf(\"reading body: %s\", err)\n            http.error(w, \"internal error\", http.statusinternalservererror)\n            return\n        }\n\n        \/\/ when the content is successfully decoded, we can persist it into\n        \/\/ our queue.\n        if err := q.enqueue(c); err != nil {\n            log.printf(\"enqueueing webhook data: %s\", err)\n            \/\/ proper error handling is missing here\n        }\n    })\n\n    http.listenandserve(bind, nil)\n}\n\nfunc main() {\n    flag.parse()\n\n    var (\n        q   *dque.dque\n        err error\n    )\n\n    if !direxists(queuedir) {\n        if err = os.mkdirall(queuedir, 0750); err != nil {\n            log.fatalf(\"creating queue dir: %s\", err)\n        }\n    }\n\n    if !direxists(filepath.join(queuedir, \"webhooks\")) {\n        q, err = dque.new(\"webhooks\", queuedir, segmentsize, func() interface{} { return &amp;webhookcontent{} })\n    } else {\n        q, err = dque.open(\"webhooks\", queuedir, segmentsize, func() interface{} { return &amp;webhookcontent{} })\n    }\n\n    if err != nil {\n        log.fatalf(\"setting up queue: %s\", err)\n    }\n\n    defer q.close()\n\n    go runserver(q)\n\n    var (\n        \/\/ placeholder during event loop\n        i interface{}\n        \/\/ payload\n        w *webhookcontent\n        \/\/ did the type assertion succeed\n        ok bool\n    )\n\n    for {\n        \/\/ we peek only. the semantic of this is that\n        \/\/ you can already access the next item in the queue\n        \/\/ without removing it from the queue and \"mark\" it as read.\n        \/\/ we use peekblock since we want to wait for an item in the\n        \/\/ queue to be available.\n        if i, err = q.peekblock(); err != nil {\n            \/\/ if we can not peek, something is seriously wrong.\n            log.fatalf(\"reading from queue: %s\", err)\n        }\n\n        if w, ok = i.(*webhookcontent); !ok {\n            \/\/ if the type assertion fails, something is seriously wrong, too.\n            log.fatalf(\"reading from queue: %s\", err)\n        }\n\n        if err = dosomethinguseful(w); err != nil {\n            log.printf(\"something went wrong: %s\", err)\n            log.println(\"i strongly suggest entering an incremental backoff!\")\n            continue\n        }\n\n        \/\/ we did something useful, so we can dequeue the item we just processed from the queue.\n        q.dequeue()\n    }\n\n}\n\nfunc dosomethinguseful(w *webhookcontent) error {\n    log.printf(\"instead of this log message, you can do something useful with: %#v\", w)\n    return nil\n}\n\nfunc direxists(path string) bool {\n    fileinfo, err := os.stat(path)\n    if err == nil {\n        return fileinfo.isdir()\n    }\n    return false\n}<\/pre>\n<p>\u73b0\u5728\uff0c\u5f53\u60a8\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\u65f6\uff1a<\/p>\n<pre>$ curl -x post --data '{\"foo\":\"baz\",\"bar\":42}' http:\/\/localhost:8080\/webhook\n<\/pre>\n<p>\u4f60\u5e94\u8be5\u5f97\u5230\u4e00\u4e2a\u7c7b\u4f3c\u7684\u65e5\u5fd7\u6761\u76ee<\/p>\n<pre>2020\/04\/18 11:34:23 Instead of this log message, you can do something useful with: &amp;main.webhookContent{Foo:\"Baz\", Bar:42}<\/pre>\n<p>\u8bf7\u6ce8\u610f\uff0csee \uff082022 \u5e74 12 \u6708\uff09\u5b9e\u73b0\u4e86<em>\u76f8\u53cd<\/em>\u65b9\u6cd5\uff1a<\/p>\n<p>\u56e0\u6b64\uff0c\u4ece gitlab 15.7+ \u5f00\u59cb\uff0c\u4e0d\u4ec5\u53d1\u56de\u201c\u65e0\u6548\u7684 http \u54cd\u5e94\u201d\u4e0d\u8d77\u4f5c\u7528\uff0c\u8fd8\u4f1a\u5bfc\u81f4 webhook \u88ab\u7981\u7528\u3002<\/p>\n<p>\uff082023 \u5e74 3 \u6708\uff09\u5bf9\u6b64\u8fdb\u884c\u4e86\u6539\u8fdb<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5f3a\u5236 Gitlab \u5728 Go \u5931\u8d25\u65f6\u91cd\u8bd5 Webhook\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; \u5f3a\u5236 Gitlab \u5728 Go \u5931\u8d25\u65f6\u91cd\u8bd5 Webhook \u5f3a\u5236 Gitlab \u5728 Go \u5931\u8d25\u65f6\u91cd\u8bd5 Webhook \u6765\u6e90\uff1astackoverflow 2024-04-23 15:03:33 0\u6d4f\u89c8 \u6536\u85cf \u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\u300a\u5f3a\u5236 Gitlab \u5728 Go \u5931\u8d25\u65f6\u91cd\u8bd5 Webhook\u300b\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01 \u95ee\u9898\u5185\u5bb9 \u6211\u60f3\u76d1\u89c6 Gitlab \u9879\u76ee\u4e2d\u7684\u6bcf\u4e2a\u4e8b\u4ef6\u5e76\u5c06\u5b83\u4eec\u5b58\u50a8\u5728\u5916\u90e8\u670d\u52a1\u4e2d\u3002\u4e3a\u6b64\uff0c\u6211\u4f7f\u7528 Gitlab Webhooks\u3002\u6211\u7528 Go \u521b\u5efa\u4e86\u4e00\u4e2a\u5c0f\u578b\u672c\u5730 HTTP \u670d\u52a1\u5668\uff0c\u7528\u4e8e\u76d1\u542c Gitlab \u7684 POST \u5e76\u5c06\u5176\u8f6c\u53d1\u5230\u5916\u90e8\u670d\u52a1\u3002 Hooks \u5305\u542b\u6211\u9700\u8981\u7684\u6240\u6709\u4fe1\u606f\uff0c\u56e0\u6b64\u770b\u8d77\u6765\u8fd9\u4e2a\u67b6\u6784\u5f88\u597d\uff1a Gitlab &gt; HTTPServer &gt; \u5916\u90e8\u670d\u52a1\u3002 \u6211\u7684\u95ee\u9898\u662f\u5f53\u5916\u90e8\u670d\u52a1\u5173\u95ed\u65f6\uff0c\u6211\u65e0\u6cd5\u8bbe\u6cd5\u8ba9 Gitlab \u91cd\u8bd5\u5931\u8d25\u7684\u8bf7\u6c42\u3002\u6b63\u5982\u6587\u6863\u6240\u8ff0\uff1a GitLab \u4f1a\u5ffd\u7565\u60a8\u7684\u7aef\u70b9\u8fd4\u56de\u7684 HTTP [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-42318","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=42318"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42318\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=42318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=42318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=42318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}