diff options
| author | Rose Hogenson <rosehogenson@posteo.net> | 2025-10-07 15:07:37 -0700 |
|---|---|---|
| committer | Rose Hogenson <rosehogenson@posteo.net> | 2025-10-07 15:07:37 -0700 |
| commit | 0f0f5f600c6141fe5f8067e59ec2880903855227 (patch) | |
| tree | e06f7d11de4d970ff6e9f10501250d3136c6b49c | |
| parent | 53ed7667f336a5676389f266feb520623679ebf0 (diff) | |
| download | roseh.moe-0f0f5f600c6141fe5f8067e59ec2880903855227.tar.zst | |
Make redirects configurable
| -rw-r--r-- | roseh.moe.go | 42 |
1 files changed, 27 insertions, 15 deletions
diff --git a/roseh.moe.go b/roseh.moe.go index 9f63d65..81285e7 100644 --- a/roseh.moe.go +++ b/roseh.moe.go @@ -1,6 +1,7 @@ package main import ( + "bufio" "bytes" "crypto/hmac" "crypto/rand" @@ -17,6 +18,7 @@ import ( "log" "net/http" "os" + "path/filepath" "strings" "sync" "time" @@ -26,9 +28,10 @@ import ( ) var ( - port = flag.Int("port", 42069, "port to listen on") - selfURL = flag.String("self-url", "http://localhost:42069", "base URL of the server") - secretsFile = flag.String("secrets", "secrets", "secrets file") + port = flag.Int("port", 42069, "port to listen on") + selfURL = flag.String("self-url", "http://localhost:42069", "base URL of the server") + secretsFile = flag.String("secrets", "secrets", "secrets file") + redirectsDir = flag.String("redirect", "redirect", "directory containing Go package redirects") serverStartTime = time.Now() ) @@ -452,14 +455,26 @@ func autosave(w http.ResponseWriter, r *http.Request) { } } +func findRedirect(kind, name string) (string, bool) { + f, err := os.Open(filepath.Join(*redirectsDir, kind)) + if err != nil { + return "", false + } + defer f.Close() + scanner := bufio.NewScanner(f) + pfx := []byte(name + "\t") + for scanner.Scan() { + line := scanner.Bytes() + if bytes.HasPrefix(line, pfx) { + return string(line[len(pfx):]), true + } + } + return "", false +} + func cmd(w http.ResponseWriter, r *http.Request) { - var source string - switch r.PathValue("cmd") { - case "hole": - source = "git https://gitlab.com/rhogenson/hole.git" - case "trash": - source = "git https://github.com/rhogenson/trash.git" - default: + source, ok := findRedirect("cmd", r.PathValue("cmd")) + if !ok { notFound(w, r) return } @@ -471,11 +486,8 @@ func cmd(w http.ResponseWriter, r *http.Request) { } func pkg(w http.ResponseWriter, r *http.Request) { - var source string - switch r.PathValue("pkg") { - case "wordlist": - source = "git https://gitlab.com/rhogenson/wordlist.git" - default: + source, ok := findRedirect("pkg", r.PathValue("pkg")) + if !ok { notFound(w, r) return } |
