Vanity Go Import Paths for Caddy 2

Maybe the following is useful for somebody else, too.

The general idea is to have a Go package at example.com/mypackage whose VCS is at https://github.com/exampleuser/mypackage.

For Caddy 1, there is a dedicated module: https://github.com/zikes/gopkg. However, it is somewhat unmaintained nowadays. Also there is no equivalent module for Caddy 2 around. That kept me on Caddy 1… until today.

My first impulse was to re-implement the functionality as a Caddy 2 module. When I immersed in the documentation to familiarize myself with extending Caddy 2 I became aware that I could express everything I need with the standard modules and building blocks in a Caddyfile. Here is what I came up with.

example.com {
    root * /var/html/www
    file_server
    handle_path /mypackage {
        route {
            @goget {
                query go-get=1
            }
            respond @goget "<meta name="go-import" content="example.com/mypackage git https://github.com/exampleuser/mypackage">
            redir https://pkg.go.dev/example.com/mypackage
    }
}
2 Likes

Neat! You can simplify this a bit:

example.com {
	root * /var/www/html
	file_server

	route /mypackage {
		@goget query go-get=1
		respond @goget `<meta name="go-import" content="example.com/mypackage git https://github.com/exampleuser/mypackage">`

		redir https://pkg.go.dev/example.com/mypackage
	}
}
1 Like