If you want to host your own Go projects and share them. I use the caddy webserver to simplify secure access over HTTPS (via letsencrypt).
/etc/caddy/Caddyfile
git.example.com {
# Git smart HTTP (fetch / clone)
@git {
path_regexp git ^/(.+)\.git(/.*)?$
}
handle @git {
root * /var/lib/git
reverse_proxy unix//run/fcgiwrap.socket {
transport fastcgi {
env SCRIPT_FILENAME /usr/lib/git-core/git-http-backend
env GIT_PROJECT_ROOT /var/lib/git
env GIT_HTTP_EXPORT_ALL ""
}
}
}
# Everything else
respond 404
}
www.example.com, example.com {
root * /var/www/www.example.com
#################################
# Go vanity imports (dynamic)
#################################
@go-get {
query go-get=1
}
handle @go-get {
respond `
` 200
}
#################################
# Normal website
#################################
file_server
}
Use a script like this to setup bare repository when you want to share a new project. Note! run it with sudo
/usr/local/bin/init-new-repo.sh
#!/usr/bin/env bash
set -euo pipefail
if [ "$#" -ne 1 ]; then
echo "Usage: $0 "
exit 1
fi
NAME="$1"
BASE="/var/lib/git"
REPO="$BASE/$NAME.git"
if [[ "$NAME" == *"/"* ]]; then
echo "Error: repository name must not contain slashes"
exit 1
fi
if [ -e "$REPO" ]; then
echo "Error: $REPO already exists"
exit 1
fi
echo "Creating bare repository: $REPO"
mkdir -p "$REPO"
git init --bare --initial-branch=main "$REPO"
# Ensure HEAD is correct (belt & suspenders)
git --git-dir="$REPO" symbolic-ref HEAD refs/heads/main
# Set ownership for git-http-backend
chown -R www-data:www-data "$REPO"
chmod -R g+rwX "$REPO"
echo "Repository '$NAME' initialized successfully."
echo "Setup your project with:"
echo "git remote add origin ssh://$(hostname).local:$REPO"
In my setup I host the projects on my workstation, so git push is
just a local push.