Last active
April 5, 2023 07:36
-
-
Save diamondburned/b3397fda89d15325283bb4ecaa10761b to your computer and use it in GitHub Desktop.
Caddyfile for GitHub redirection with Go modules support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ config, lib, pkgs, ... }: | |
let | |
user = "diamondburned"; | |
go-get-html = pkgs.writeTextFile { | |
name = "libdb.so-go-get"; | |
text = '' | |
<!DOCTYPE html> | |
{{ $path := .OriginalReq.URL.Path }} | |
{{ $name := $path | splitList "/" | rest | first }} | |
<meta charset="utf-8"> | |
<meta name="go-import" content="libdb.so/{{ $name }} git https://github.com/${user}/{{ $name }}.git"> | |
<meta http-equiv="refresh" content="0; url=https://github.com/${user}{{ $path }}"> | |
<a href="https://github.com/${user}{{ $path }}"> | |
Click here if you are not redirected. | |
</a> | |
''; | |
destination = "/index.html"; | |
}; | |
in | |
{ | |
services.diamondburned.caddy.sites."example.com" = '' | |
# Specifically handle ?go-get for Go modules. | |
@go_get query go-get=1 | |
handle @go_get { | |
rewrite * / | |
root * ${go-get-html} | |
file_server | |
templates | |
header Cache-Control "public, max-age=604800, immutable" | |
} | |
# Redirect to GitHub otherwise. | |
handle { | |
redir https://github.com/${user}{path} | |
} | |
''; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment