all repos — site @ 72812b762c6aba86db69fc05d6eda26bb7575aab

source for my site, found at icyphox.sh

pages/blog/go-get-cgit.md (view raw)

 1---
 2template:
 3slug: go-get-cgit
 4title: Make cgit go gettable
 5subtitle: go get git.icyphox.sh/* works!
 6date: 2021-07-14
 7---
 8
 9`go get` requires the presence of the `go-import` meta tag[^1] on the
10repository's web page. cgit doesn't support it out of the box; instead,
11we can make nginx inject it into every page. Enter: `sub_filter`.[^2]
12
13`sub_filter` is a function that simply performs a string replace. For
14example:
15```nginx
16location / {
17  sub_filter '<img src=dog.png>' '<img src=cat.png>';
18  sub_filter_once on;
19}
20```
21
22In our case, we want to have the meta tag injected inside `<head>`.
23
24```nginx
25server {
26  listen 443 ssl;
27  server_name git.icyphox.sh;
28
29  location / {
30    ...
31
32    sub_filter '</head>'
33      '<meta name="go-import" content="$host$uri git https://$host$uri"></head>';
34    sub_filter_once on;
35  }
36}
37```
38
39The closing `</head>` tag gets replaced -- injecting the meta tag inside
40`<head>`. This can also be extended to add the `go-source` meta tag as
41well.
42
43[^1]: https://godocs.io/cmd/go#hdr-Remote_import_paths
44[^2]: http://nginx.org/en/docs/http/ngx_http_sub_module.html