aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrégoire Duchêne <gduchene@awhk.org>2022-06-04 16:20:04 +0100
committerGrégoire Duchêne <gduchene@awhk.org>2022-06-04 16:20:04 +0100
commitd90994408fed47a8fb7a2d82e2f751d8da7067e4 (patch)
tree5ad0d4570f81877873f5b7dc98a3f2791130b1ed
parentde889e3a7e198c6a47fa6c47a4e2526259f0ee98 (diff)
Generate destinations with a regular expression
-rw-r--r--main.go3
-rw-r--r--resp.go20
-rw-r--r--resp_test.go25
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)
- }
- }
-}