Skip to content
 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cf-go-vanity-imports

Proof of concept for a tiny Cloudflare Worker that serves Go vanity import paths with just ~25 lines of JavaScript.

Currently powering go.carr.sh.

Go vanity imports let you use a custom domain (e.g. go.carr.sh/litmus) as the canonical import path for your Go modules, while the source code lives on GitHub. This worker handles the handshake between the Go toolchain and your repos.

How it works

When the Go toolchain fetches a module it sends a ?go-get=1 query parameter. The worker responds with an HTML page containing a <meta name="go-import"> tag that points to the corresponding GitHub repository:

$ curl 'https://go.carr.sh/litmus?go-get=1'
<!DOCTYPE html><meta name="go-import" content="go.carr.sh/litmus git https://github.com/lukecarr/litmus">

If a regular browser hits the same URL (without ?go-get=1), the worker redirects to the GitHub repo instead:

$ curl -is 'https://go.carr.sh/litmus' | grep location
location: https://github.com/lukecarr/litmus

Adding a module

Modules are registered in the MODULES object in src/index.js. Each property associates a module name (i.e. go.carr.sh/<module name>) with a repo.

In the below example, the litmus module is served at go.carr.sh/litmus and points to the lukecarr/litmus GitHub repo:

const MODULES = {
  litmus: "https://github.com/lukecarr/litmus",
};

Getting started

Prerequisites

The only prerequisite is Bun. The specific version of Bun is pinned in the packageManager entry of the package.json file.

Install dependencies

Install the worker's dependencies with Bun:

bun install

Run locally

Run the worker in development mode with hot reloading:

bun run dev

Run tests

Run tests with coverage reporting (we strive for 100% coverage):

bun test --coverage

Lint & format

Lint and format the codebase (with auto-fix) using Biome:

bun run check

Deployment

The worker is deployed to Cloudflare via Wrangler:

bun run deploy

Routing is configured in wrangler.jsonc to serve requests on the go.carr.sh custom domain.

License

This repo is licensed under the MIT License.

About

Vanity import URLs for Go modules on Cloudflare Workers.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages