如何使用Bitbucket Server支持”go get”命令?

huangapple go评论106阅读模式
英文:

How do you support 'go get' using bitbucket server?

问题

我想支持以下形式的请求:

go get acme.com/component

其中 component 是存储在 Bitbucket Server 中的一个 git 项目,可以通过以下方式获取:

git clone http://acme.dev:7600/scm/project/component

go 1.8 客户端不知道如何根据 go get 参数获取组件,因此它会执行 HTTPS get 请求(参考 https://golang.org/cmd/go/#hdr-Remote_import_paths),请求的 URL 是:

https://acme.com/component?go-get=1

它期望得到一个 HTTP 200 响应,响应内容如下:

<head>
  <meta name="go-import" content="acme.com/component git http://acme.dev:7600/scm/project/component">
</head>

HAProxy 很有吸引力,因为 Bitbucket Server 的 SSL 终止过程使用它。它有一个 monitor-uri 的 hack,如果它检测到后端正在响应特定的 URI,它会返回一个 HTTP 响应,但我不认为它足够灵活以处理不同的组件名称。

有人知道如何使用 HAProxy 或者其他工具(比如 NGinx)来处理这个问题吗?

英文:

I would like to support requests of the form:

go get acme.com/component

where component is a git project stored in Bitbucket Server which can be retrieved via:

git clone http://acme.dev:7600/scm/project/component

The go 1.8 client does not know how to fetch the component based on the go get parameters so it does an HTTPS get (as per https://golang.org/cmd/go/#hdr-Remote_import_paths) of:

https://acme.com/component?go-get=1

In response it expects an HTTP 200 containing the following:

<head>
  <meta name="go-import" content="acme.com/component git http://acme.dev:7600/scm/project/component">
</head>

HAProxy is attractive because the Bitbucket Server SSL termination procedure uses it. It has a monitor-uri hack to return an HTTP response if it detects that the backend is responding to a specific URI but I don't think it's sufficiently flexible to handle different component names.

Does anyone know a way to handle this using HAProxy or, perhaps, another tool like NGinx?

答案1

得分: 1

根据您的需求,您可以使用仅带有Lua支持的HAProxy进行自定义响应。在这里,我使用HAProxy 1.6.12和Lua 5.3.4,将请求路径直接复制到响应中进行自定义处理,也就是说任何路径都会被复制进去。如果需要修改路径,您还可以使用string.gsub()函数。

以下是一个简单的Lua脚本:

-- /etc/haproxy/lua/custom-response.lua

core.register_service("magic-endpoint", "http", function(applet)
   local response =
         "<head>\r\n" ..
         "  <meta name=\"go-import\" content=\"example.com" .. applet.path ..
         " git http://example.dev:7600/scm/project" .. applet.path ..
         "\">\r\n</head>\r\n";
   applet:set_status(200);
   applet:add_header("Content-Length", string.len(response));
   applet:add_header("Content-Type", "text/whatevs");
   applet:start_response();
   applet:send(response);
end)

以下是配置文件中的相关行。我只是使用主机名将请求路由到后端,但您当然可以根据需要使用任何逻辑来触发它。

-- /etc/haproxy.cfg 

global
    lua-load /etc/haproxy/lua/custom-response.lua

frontend main-frontend
    mode http
    bind :80
    use_backend example if { hdr(host) -i example.com }

backend example
    mode http
    http-request use-service lua.magic-endpoint

然后进行测试:

$ curl -v http://example.com/project1
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to example.com (127.0.0.1) port 80 (#0)
> GET /project1 HTTP/1.1
> User-Agent: curl/7.35.0
> Host: example.com
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: text/whatevs
< Content-Length: 124
<
<head>
  <meta name="go-import" content="example.com/project1 git http://example.dev:7600/scm/project/project1">
</head>

看起来没问题。

英文:

Depending on how you need to customize the response, this is possible using only HAProxy, compiled with Lua support. Here, using HAProxy 1.6.12 with Lua 5.3.4, I'm taking the request path and dropping it into the the response as-is, to do the customization -- essentially any path is just getting copied in. You could also use string.gsub() if you needed to modify it.

A small Lua script:

<!-- lang:lua -->

-- /etc/haproxy/lua/custom-response.lua

core.register_service(&quot;magic-endpoint&quot;, &quot;http&quot;, function(applet)
   local response =
         &quot;&lt;head&gt;\r\n&quot; ..
         &quot;  &lt;meta name=\&quot;go-import\&quot; content=\&quot;example.com&quot; .. applet.path ..
         &quot; git http://example.dev:7600/scm/project&quot; .. applet.path ..
         &quot;\&quot;&gt;\r\n&lt;/head&gt;\r\n&quot;;
   applet:set_status(200);
   applet:add_header(&quot;Content-Length&quot;, string.len(response));
   applet:add_header(&quot;Content-Type&quot;, &quot;text/whatevs&quot;);
   applet:start_response();
   applet:send(response);
end)

And the relevant lines from config. I'm just using the hostname to route the request to the backend, but you could of course trigger it using whatever logic you need.

-- /etc/haproxy.cfg 

global
    lua-load /etc/haproxy/lua/custom-response.lua

frontend main-frontend
    mode http
    bind :80
    use_backend example if { hdr(host) -i example.com }

backend example
    mode http
    http-request use-service lua.magic-endpoint

And, test:

$ curl -v http://example.com/project1
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to example.com (127.0.0.1) port 80 (#0)
&gt; GET /project1 HTTP/1.1
&gt; User-Agent: curl/7.35.0
&gt; Host: example.com
&gt; Accept: */*
&gt;
&lt; HTTP/1.1 200 OK
&lt; Content-Type: text/whatevs
&lt; Content-Length: 124
&lt;
&lt;head&gt;
  &lt;meta name=&quot;go-import&quot; content=&quot;example.com/project1 git http://example.dev:7600/scm/project/project1&quot;&gt;
&lt;/head&gt;

Seems legit.

答案2

得分: 0

为了进行对话并且因为灵活生成HTTP响应是有用的,我尝试使用nginx进行了一次尝试 - 并切换到了ssh。配置文件的非标准部分有一个规则,将go客户端指向Bitbucket服务器:

location ~ "^(/[^/]+)(/[^/]+)?" {
    if ($arg_go-get = "1") {
        return 200 '<html><head><meta name="go-import" content="acme.com$1$2 git ssh://acme.dev:7999$1$2"></head></html>';
    }
}
英文:

For the purpose of conversation and because flexible generation of HTTP responses is useful I gave it a try using nginx - with a switch to ssh. The non-standard part of the configuration file has a rule to point the go client to the Bitbucket server:

location ~ &quot;^(/[^/]+)(/[^/]+)?&quot; {
    if ($arg_go-get = &quot;1&quot;) {
        return 200 &#39;&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;go-import&quot; content=&quot;acme.com$1$2 git ssh://acme.dev:7999$1$2&quot;&gt;&lt;/head&gt;&lt;/html&gt;&#39;;
    }
}

huangapple
  • 本文由 发表于 2017年5月6日 22:56:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/43821889.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定