summaryrefslogtreecommitdiff
path: root/content/posts/using_go_vanity_url_with_cgit.md
diff options
context:
space:
mode:
authorclaw0ry <me@claw0ry.net>2024-12-11 13:56:52 +0100
committerclaw0ry <me@claw0ry.net>2024-12-11 13:56:52 +0100
commit4719cc03837490ed4bf1b9725d75a686e56e5a6a (patch)
tree769dd3a3a87153df049b3043196bd131495b10ad /content/posts/using_go_vanity_url_with_cgit.md
fresh start
Diffstat (limited to 'content/posts/using_go_vanity_url_with_cgit.md')
-rw-r--r--content/posts/using_go_vanity_url_with_cgit.md46
1 files changed, 46 insertions, 0 deletions
diff --git a/content/posts/using_go_vanity_url_with_cgit.md b/content/posts/using_go_vanity_url_with_cgit.md
new file mode 100644
index 0000000..6eca271
--- /dev/null
+++ b/content/posts/using_go_vanity_url_with_cgit.md
@@ -0,0 +1,46 @@
+---
+title: 'Using Go vanity URL with cgit'
+description: 'How to setup Go vanity URLs with cgit to allow custom domain packages'
+date: '2024-10-25T16:00:00+02:00'
+tags: ['debian', 'cgit', 'nginx', 'git', 'go']
+---
+
+This year I wanted to experiment with moving away from Github (and other cloud based VCS). So I setup my own server with [git](https://git-scm.com/book/en/v2/Git-on-the-Server-Setting-Up-the-Server), [cgit](https://git.zx2c4.com/cgit/) as the interface and [nginx](https://nginx.org/) as the web server. The basic setup was fairly easy.
+
+<!--more-->
+
+I recently published one of my tools (snowy) and when I tried to install it from my repository I got an error message:
+
+```
+unrecognized import path "git.claw0ry.net/snowy" (parse https://git.claw0ry.net/snowy?go-get=1: no go-import meta tags ())
+```
+
+I went to the internet and did some research. Apparently, Go treats github.com (and other popular vcs) specially, so if we want to our custom domain and cgit hosted repository to work with `go install` and `go get` we need to do some extra work on our end. You can read the official documentation about the subject here: [https://pkg.go.dev/cmd/go#hdr-Remote_import_paths](https://pkg.go.dev/cmd/go#hdr-Remote_import_paths)
+
+Basically what we need is an HTML meta-tag to tell go how to map a package name to a repository. For instance, go does not know that my package name `git.claw0ry.net/snowy` should resolve to `https://git.claw0ry.net/snowy` by itself.
+
+This is the tag that go will look for.
+
+```
+<meta name="go-import" content="git.claw0ry.net/snowy git https://git.claw0ry.net/snowy">
+```
+
+To use this meta-tag cgit has a `repo.extra-head-content` option that we can use to inject the meta-tag. The downside of this is that it must be done pr repository. Instead we can use NGINX `sub_filter` module to inject this meta-tag on very repository.
+
+So in your NGINX configuration for our domain we will replace a part of the HTML before returning it.
+
+```conf
+server {
+ server_name git.claw0ry.net;
+ # ...
+
+ location / {
+ # ...
+
+ sub_filter '</head>' '<meta name="go-import" content="$host$uri git https://$host$uri"></head>';
+ sub_filter_once on;
+ }
+}
+```
+
+The next time you do a `go install` it should work.