From d90994408fed47a8fb7a2d82e2f751d8da7067e4 Mon Sep 17 00:00:00 2001 From: GrĂ©goire DuchĂȘne Date: Sat, 4 Jun 2022 16:20:04 +0100 Subject: Generate destinations with a regular expression --- main.go | 3 ++- resp.go | 20 ++++++++------------ resp_test.go | 25 ++----------------------- 3 files changed, 12 insertions(+), 36 deletions(-) diff --git a/main.go b/main.go index 71d40de..10e1703 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,7 @@ import ( "net/http" "os" "os/signal" + "regexp" "time" "golang.org/x/sys/unix" @@ -33,7 +34,7 @@ func main() { done := make(chan os.Signal, 1) signal.Notify(done, os.Interrupt, unix.SIGTERM) - srv := http.Server{Handler: &redirector{*from, *to, *vcs}} + srv := http.Server{Handler: &redirector{regexp.MustCompile(*from), *to, *vcs}} go func() { ln, err := listenSD() if err != nil { diff --git a/resp.go b/resp.go index 2bbe434..b213f58 100644 --- a/resp.go +++ b/resp.go @@ -8,7 +8,7 @@ import ( "log" "net/http" "path" - "strings" + "regexp" "text/template" ) @@ -21,9 +21,11 @@ var ( type bodyData struct{ Package, Repository, VCS string } -type redirector struct{ from, to, vcs string } - -var _ http.Handler = &redirector{} +type redirector struct { + re *regexp.Regexp + repl string + vcs string +} func (h *redirector) ServeHTTP(w http.ResponseWriter, req *http.Request) { if req.Method != http.MethodGet { @@ -38,17 +40,11 @@ func (h *redirector) ServeHTTP(w http.ResponseWriter, req *http.Request) { w.WriteHeader(http.StatusFound) return } - dest := h.getRepo(pkg) + + dest := h.re.ReplaceAllString(pkg, h.repl) w.Header().Set("Content-Type", "text/html; charset=utf-8") w.WriteHeader(http.StatusOK) if err := body.Execute(w, bodyData{pkg, dest, h.vcs}); err != nil { log.Println(err) } } - -func (h *redirector) getRepo(pkg string) string { - from := strings.TrimRight(h.from, "/") - to := strings.TrimRight(h.to, "/") - path := strings.TrimLeft(strings.TrimPrefix(pkg, from), "/") - return to + "/" + strings.Split(path, "/")[0] -} diff --git a/resp_test.go b/resp_test.go index 2185403..1f8c458 100644 --- a/resp_test.go +++ b/resp_test.go @@ -7,11 +7,12 @@ import ( "io" "net/http" "net/http/httptest" + "regexp" "testing" ) func TestRedirector_ServeHTTP(t *testing.T) { - r := &redirector{"src.example.com/x", "https://example.com/git", "git"} + r := &redirector{regexp.MustCompile(`src\.example\.com/x`), "https://example.com/git", "git"} t.Run("GoVisit", func(t *testing.T) { req := httptest.NewRequest(http.MethodGet, "https://src.example.com/x/foo?go-get=1", nil) @@ -56,25 +57,3 @@ func TestRedirector_ServeHTTP(t *testing.T) { } }) } - -func TestRedirector_getRepo(t *testing.T) { - r := &redirector{"src.example.com/x/", "https://example.com/git/", "git"} - for _, tc := range []struct{ pkg, expected string }{ - {"src.example.com/x/foo", "https://example.com/git/foo"}, - {"src.example.com/x/foo/bar", "https://example.com/git/foo"}, - } { - if actual := r.getRepo(tc.pkg); actual != tc.expected { - t.Errorf("expected %q, got %q", tc.expected, actual) - } - } - - r = &redirector{"src.example.com/x", "https://example.com/git", "git"} - for _, tc := range []struct{ pkg, expected string }{ - {"src.example.com/x/foo", "https://example.com/git/foo"}, - {"src.example.com/x/foo/bar", "https://example.com/git/foo"}, - } { - if actual := r.getRepo(tc.pkg); actual != tc.expected { - t.Errorf("expected %q, got %q", tc.expected, actual) - } - } -} -- cgit v1.2.3-70-g09d2