Golang vanity URLs simple server
  • Go 95.5%
  • Dockerfile 4.5%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
surdeus efd6ae16ee feat: link the repository URL on discovery pages
The repository line was inert text, so a human landing on a discovery page had
to copy the URL to see the code. HTTP(S) repositories are now links. ssh://,
git:// and hg+ssh:// stay as plain code on purpose: a link there would offer a
click that leads nowhere in a browser.

repoHTML also replaces the hand-written anchor in the landing page's override
table, which linked every URL unconditionally and so produced dead ssh links.

The link lives in <body>, and Go's parser stops at <body>, so the discovery
contract is untouched; a test asserts the page still carries exactly one
go-import tag.

Link text stays the clone URL that appears in the tag, so text and href agree
rather than pointing at a different "browse" URL. Forgejo serves .git URLs as
the repository page, verified across three URL forms.

Version 0.5.0.
2026-10-31 14:04:59 +05:00
deploy feat: link the repository URL on discovery pages 2026-10-31 14:04:59 +05:00
.dockerignore container: scratch image, K3S manifests, config path discovery 2026-10-31 13:21:21 +05:00
.gitignore Init 2026-10-31 12:56:52 +05:00
config.go Init 2026-10-31 12:56:52 +05:00
Dockerfile feat: report the running version 2026-10-31 13:55:03 +05:00
emit.go feat: report the running version 2026-10-31 13:55:03 +05:00
go.mod Init 2026-10-31 12:56:52 +05:00
main.go feat: link the repository URL on discovery pages 2026-10-31 14:04:59 +05:00
readme.md feat: link the repository URL on discovery pages 2026-10-31 14:04:59 +05:00
resolve.go Init 2026-10-31 12:56:52 +05:00
server.go feat: link the repository URL on discovery pages 2026-10-31 14:04:59 +05:00
vanity.json Init 2026-10-31 12:56:52 +05:00
vanity_test.go feat: link the repository URL on discovery pages 2026-10-31 14:04:59 +05:00

vanity

Publishes Go module paths that outlive the forge.

go.mojosa.su/lib/goss  ->  https://git.mojosa.su/lib/goss.git

The mapping is derived from the path, so the configuration file is normally empty. Overrides exist for the day one repository moves somewhere else.

Why

A go-import meta tag is the only thing Go asks before cloning: where does this import path live? Whoever answers that question owns the module path. Answering it yourself means the module path stays go.mojosa.su/... no matter which forge is behind it — Codeberg, this Forgejo, the next one.

The default rule

<domain>/<org>/<repo> maps to <default_repo_base>/<org>/<repo>:

go.mojosa.su/lib/goss              -> https://git.mojosa.su/lib/goss.git
go.mojosa.su/service/mapmark       -> https://git.mojosa.su/service/mapmark.git
go.mojosa.su/lib/goss/geom         -> https://git.mojosa.su/lib/goss.git   (subpackage)
go.mojosa.su/wiki/root             -> https://git.mojosa.su/wiki/root.git

Because the answer is derived, vanity.json can stay empty forever — including for repositories that do not exist yet. The server does not know which repositories exist and never asks, so it also cannot leak that information: unknown paths get the same tag as real ones and fail later at git clone.

Subpackages resolve to the repository root in a single request: the tag's prefix is the first two path elements, so Go then resolves the rest of the package path inside the module.

Overrides

One entry per exception, never one per repository:

{
  "version": 1,
  "domain": "go.mojosa.su",
  "default_repo_base": "https://git.mojosa.su",
  "rules": [
    {"prefix": "lib/gg", "repo": "https://git.mojosa.su/mono", "subdir": "gg",
     "note": "folded into the mono repo 2026-11"},
    {"prefix": "lib/goss", "repo": "https://elsewhere.example/goss",
     "note": "moved off the forge"}
  ]
}

Rules are matched longest-prefix-first on path segments (lib/gg never captures lib/gg2), so an override can point at any subtree without disturbing its siblings. A rule with subdir declares a module that lives in a subdirectory of the repository.

Precedence: the longest matching rule wins; with no match, the default rule applies.

Configuration reference

  • version — must be 1.
  • domain — bare hostname this server answers for, e.g. go.mojosa.su.
  • default_repo_base — absolute URL prefixed to derived repo paths.
  • repo_suffix — appended to derived repo paths; default ".git". Set to "" for hosts that serve bare repositories without a suffix.
  • source_template — go-source URL for derived repositories. Default {default_repo_base}/{prefix}/src/branch/{branch}. Placeholders: {prefix}, {branch}, {default_repo_base}. Set to "" to omit go-source.
  • default_branch — used by source_template; default "main".
  • rules — array of overrides. Each has prefix, repo, optional vcs (default git), subdir, source, and note.
  • Unknown fields are rejected, duplicate prefixes are rejected, and uppercase or traversing prefixes are rejected (module paths escape uppercase, so lib/Goss would be a different module).

Use

# serve discovery
vanity --config vanity.json --addr :8080

# what does a path resolve to, and which rule decided it?
vanity --explain lib/goss
vanity --explain lib/gg/sprite

# validate the deploy config and exit (the form CI needs)
vanity --config deploy/vanity.json --check-targets --check-only

# bake a static tree instead of running a service
fjclone --list > repos.txt
vanity --emit ./public --repos repos.txt

--check-targets runs git ls-remote against every override, so a typo in a repository URL is a startup failure rather than a mystery three weeks later. On its own it checks and then serves; add --check-only to exit after checking, which is what a pipeline needs.

Static mode

--emit writes DIR/<prefix>/index.html for every repository in the list plus every override prefix, so nginx, a CDN or object storage can serve discovery with nothing running. Static hosts cannot derive paths, which is why the repository list is required here and not when serving.

Baked pages are byte-identical to served ones (asserted in the tests), so the two modes cannot drift.

Deployment

DNS is a wildcard (*.mojosa.su → the same host as git.mojosa.su), so one Traefik router and one certificate are enough:

http:
  routers:
    vanity:
      rule: Host(`go.mojosa.su`)
      service: vanity
      tls: {}
  services:
    vanity:
      loadBalancer:
        servers:
          - url: http://127.0.0.1:8080

The root page is for humans: it links default_repo_base first, so anyone who lands on the domain can see where code lives when it has not been moved, then lists any overrides with their notes. It deliberately carries no go-import tag — the root is not a module path, and a tag there would make a bare-host go get resolve to something.

Pin the domain with --domain (or domain in the config). The server then rejects a mismatched Host header with 421 Misdirected Request, so a cached response can never be built from an attacker-chosen host. With no domain pinned it reflects the request host and sends Cache-Control: no-store; that mode is for local testing only, since a module path cannot contain a port.

Caching: discovery responses are public, max-age=300. Short enough that an override edit propagates within minutes, long enough that a CI fleet does not hammer the service.

Container

The image is FROM scratch: a static binary and a default config, nothing else. 9.8 MiB on disk, about 3 MB compressed on the registry, ~7 MiB resident.

docker build -t vanity:0.5.0 .
docker run --rm vanity:0.5.0 --explain lib/goss
docker run --rm -p 8080:8080 vanity:0.5.0

With no --config, the binary looks for ./vanity.json and then /etc/vanity/vanity.json, so the same image works from a checkout and from the container without arguments.

The version is stamped at build time so the running process names the tag it was built from:

docker build --build-arg VERSION=$(git describe --tags --dirty) -t vanity:0.5.0 .

Without the argument the value from main.go is used, so a bare docker build . still works. The stamp matters because it makes "what is deployed" a single answer instead of three (manifest tag, image tag, printed version).

Confirming a rollout

/healthz reports the running version in its body, so one request answers the question after a deploy:

curl -s https://go.mojosa.su/healthz      # ok v0.5.0

The status stays 200 and the response stays no-store, so probes are unaffected. The same string appears in a <footer> on the landing page — cached for 5 minutes, so /healthz is the one to trust immediately after a rollout.

Note that --emit bakes the version into the generated root page: a static tree reports the version that generated it, not the version of any later binary.

Build arguments and targets:

  • --build-arg TARGETARCH=arm64 cross-compiles (BuildKit sets it automatically for multi-platform builds).
  • --target debug adds a shell for troubleshooting, at ~22 MiB.
  • The default target is the scratch image; debug is declared first on purpose, since the last stage in a Dockerfile becomes the default.

Kubernetes manifests for the K3S cluster live in deploy/ as a kustomization: vanity.json, deployment.yaml, service.yaml, ingress.yaml, and kustomization.yaml tying them together. The pod runs as UID 65532 with a read-only root filesystem, all capabilities dropped and RuntimeDefault seccomp, which the scratch image fully supports because nothing in it needs to write or escalate.

kubectl apply -k deploy/
kubectl rollout status deployment/vanity

The config is a real file at deploy/vanity.json, not a YAML block inside a ConfigMap, so it is the same artifact the binary consumes and can be validated before it reaches the cluster:

vanity --config deploy/vanity.json --check-targets --check-only

kustomization.yaml generates the ConfigMap with a content hash in its name. Editing vanity.json therefore changes the pod template's volume reference, which is a real spec change — so kubectl apply -k rolls the pods on its own. Without that, a config-only edit would update the ConfigMap while every running pod kept serving the old values, because the process reads its config once at startup. A rollout that stalls means old behaviour: if the new config is invalid the process exits instead of starting, readiness never passes, and the previous pod keeps serving. Check with kubectl rollout status, and kubectl rollout undo deployment/vanity to back out.

A release is a one-line change in kustomization.yaml:

images:
  - name: vanity
    newTag: 0.5.1

Then rebuild, and for an offline cluster (K3S runs containerd, not Docker):

docker build --build-arg VERSION=v0.5.1 -t vanity:0.5.1 .
docker save vanity:0.5.1 | k3s ctr images import -
kubectl apply -k deploy/

Include the VERSION argument: without it the image reports whatever main.go says, which is how the printed version and the deployed tag drift apart.

Probes hit /healthz, which needs no config and never touches the network, so the pod is never marked ready on the strength of an upstream being reachable.

Bootstrap

The module path is go.mojosa.su/service/vanity, so go install cannot be used until this service is serving that domain: the module's declared path and the forge path would disagree, and Go refuses the mismatch.

Build from a checkout instead. That works before the service exists and while it is broken:

git clone https://git.mojosa.su/service/vanity
cd vanity
go build -o vanity .          # produces ./vanity

Once the domain is live, go install go.mojosa.su/service/vanity@latest also works, which is the ordinary way to upgrade afterwards.

Tests

go test ./...

Coverage includes strict config parsing (unknown fields, duplicates, traversal, case, unknown VCS), prefix matching and precedence, subpackage resolution, subdir handling, host pinning, cache headers, static emission, and an end-to-end test that takes the repository URL out of a served discovery page and actually clones it with git.

Two limits worth knowing:

  • A literal go get end-to-end test needs to bind port 443 (module paths cannot contain a port). That requires privileges this project does not assume, so the tests instead parse the page with the same rules cmd/go uses (parseMetaGoImports, 3-or-4 field tags, stop at <body>) and clone the advertised URL for real.

  • The emitted tag is byte-identical to what Forgejo serves natively for the same repository apart from the domain, which makes the two independently verifiable:

    forgejo: go-import content="git.mojosa.su/lib/goss git https://git.mojosa.su/lib/goss.git"
    vanity:  go-import content="go.mojosa.su/lib/goss git https://git.mojosa.su/lib/goss.git"
    

Limits

  • Private repositories: the tag is public by definition. Discovery never needs credentials; the clone that follows does. Point default_repo_base at a host the client can authenticate to.
  • Go treats go.mojosa.su/lib/goss and git.mojosa.su/lib/goss as different modules. Adopting the vanity path for a module is a one-way decision per module: move it fully or not at all, or you will end up with two copies of the same code in one build.
  • go-source only affects documentation links. It is derived for the default rule and omitted for overrides unless the rule sets source.