summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--roseh.moe.go42
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
}