summaryrefslogtreecommitdiff
path: root/content/posts/using_go_vanity_url_with_cgit.md
blob: 6eca271a89573493956c7b80d709f89eb911ea11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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.