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: GitHub - zikes/gopkg: A Caddy plugin to add gopkg-like functionality to your own web sites. 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
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
}
}
Note that the route
has the effect of overriding the Caddyfile’s default directive order, because otherwise, the redir
would always happen before the respond
.