go packages on your own domain

go get has a nice feature where it will fetch the package URL and, based on a meta tag, redirect to the actual location.

Here’s the nginx config I use to redirect go get juju.net.nz/x/package to either my private repo (for unreleased stuff) or my cgit instance for everything else. This is similar to how golang.org/x/... and gobot.io/x/... operate.

server {
        ...
        location ~ /x/(.*) {
                if (-d /home/juju/p/git/$1.git) {
                        return 200 '<meta name="go-import" content="$host$uri git ssh://juju@$host/~juju/p/git/$1.git">\n';
                }
                if (-d /home/juju/git/$1.git) {
                        return 200 '<meta name="go-import" content="$host$uri git $scheme://$host/src/$1.git">\n';
                }
                return 404;
        }
}

As a bonus this means I could, say, shift the code to github without breaking existing code.