aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXe Iaso <me@xeiaso.net>2024-02-20 16:17:38 -0500
committerXe Iaso <me@xeiaso.net>2024-02-20 16:17:38 -0500
commitc09361b9e018d4bd759a1345b2c176e63c73519a (patch)
tree6964154f1ea56f2b1285331a42fd9f6f0b20144d
parent40b1165ad254cb030f6e2891c132b1ab89042b2f (diff)
downloadxesite-c09361b9e018d4bd759a1345b2c176e63c73519a.tar.xz
xesite-c09361b9e018d4bd759a1345b2c176e63c73519a.zip
redirect christine.website to xeiaso.net
Signed-off-by: Xe Iaso <me@xeiaso.net>
-rw-r--r--cmd/xesite/main.go3
-rw-r--r--internal/accept_encoding_test.go30
-rw-r--r--internal/domain_redirect.go37
-rw-r--r--internal/domain_redirect_test.go46
4 files changed, 86 insertions, 30 deletions
diff --git a/cmd/xesite/main.go b/cmd/xesite/main.go
index 0f8f060..feae8cc 100644
--- a/cmd/xesite/main.go
+++ b/cmd/xesite/main.go
@@ -26,7 +26,7 @@ var (
gitBranch = flag.String("git-branch", "main", "Git branch to clone")
gitRepo = flag.String("git-repo", "https://github.com/Xe/site", "Git repository to clone")
githubSecret = flag.String("github-secret", "", "GitHub secret to use for webhooks")
- internalAPIBind = flag.String("internal-api-bind", ":3001", "Port to listen on for the internal API")
+ internalAPIBind = flag.String("internal-api-bind", ":3001", "Port to listen on for the internal API")
miToken = flag.String("mi-token", "", "Token to use for the mi API")
patreonSaasProxyURL = flag.String("patreon-saasproxy-url", "http://xesite-patreon-saasproxy.flycast", "URL to use for the patreon saasproxy")
siteURL = flag.String("site-url", "https://xeiaso.net/", "URL to use for the site")
@@ -112,6 +112,7 @@ func main() {
h = internal.CacheHeader(h)
h = internal.AcceptEncodingMiddleware(h)
h = internal.RefererMiddleware(h)
+ h = internal.DomainRedirect(h, *devel)
slog.Info("starting server", "bind", *bind)
log.Fatal(http.Serve(ln, h))
diff --git a/internal/accept_encoding_test.go b/internal/accept_encoding_test.go
index 503579f..3d8f12b 100644
--- a/internal/accept_encoding_test.go
+++ b/internal/accept_encoding_test.go
@@ -52,24 +52,10 @@ func TestParseAcceptLanguage(t *testing.T) {
if lqs[3].Q != 0.7 {
t.Errorf("lqs[3].Q = %f, want 0.7", lqs[3].Q)
}
-
- t.Run("invalid", func(t *testing.T) {
- panicked := false
- acptEnc := "taco;q=beer"
- defer func() {
- if r := recover(); r != nil {
- panicked = true
- }
- if !panicked {
- t.Errorf("did not panic")
- }
- }()
- ParseAcceptLanguage(acptEnc)
- })
}
func TestParseAcceptEncoding(t *testing.T) {
- acptEnc := "gzip, deflate, br;q=0.9"
+ acptEnc := "gzip, deflate, br;q=1"
eqs := ParseAcceptEncoding(acptEnc)
if len(eqs) != 3 {
t.Errorf("len(eqs) = %d, want 3", len(eqs))
@@ -92,18 +78,4 @@ func TestParseAcceptEncoding(t *testing.T) {
if eqs[2].Q != 1 {
t.Errorf("eqs[2].Q = %f, want 1", eqs[2].Q)
}
-
- t.Run("invalid", func(t *testing.T) {
- panicked := false
- acptEnc := "gzip, deflate, taco;q=beer"
- defer func() {
- if r := recover(); r != nil {
- panicked = true
- }
- if !panicked {
- t.Errorf("did not panic")
- }
- }()
- ParseAcceptEncoding(acptEnc)
- })
}
diff --git a/internal/domain_redirect.go b/internal/domain_redirect.go
new file mode 100644
index 0000000..a62f122
--- /dev/null
+++ b/internal/domain_redirect.go
@@ -0,0 +1,37 @@
+package internal
+
+import (
+ "flag"
+ "fmt"
+ "net/http"
+)
+
+var (
+ redirectDomain = flag.String("redirect-domain", "xeiaso.net", "Domain to redirect to")
+
+ allowedPaths = map[string]struct{}{
+ "/blog.rss": {},
+ "/blog.atom": {},
+ "/blog.json": {},
+ }
+)
+
+func DomainRedirect(next http.Handler, development bool) http.Handler {
+ if development {
+ return next
+ }
+
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if _, ok := allowedPaths[r.URL.Path]; ok {
+ next.ServeHTTP(w, r)
+ return
+ }
+
+ if r.Host != *redirectDomain {
+ http.Redirect(w, r, fmt.Sprintf("https://%s%s", *redirectDomain, r.RequestURI), http.StatusMovedPermanently)
+ return
+ }
+
+ next.ServeHTTP(w, r)
+ })
+}
diff --git a/internal/domain_redirect_test.go b/internal/domain_redirect_test.go
new file mode 100644
index 0000000..ba922d0
--- /dev/null
+++ b/internal/domain_redirect_test.go
@@ -0,0 +1,46 @@
+package internal
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "testing"
+)
+
+func TestDomainRedirect(t *testing.T) {
+ h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusOK)
+ })
+
+ t.Run("development", func(t *testing.T) {
+ r := httptest.NewRequest("GET", "http://localhost/", nil)
+ w := httptest.NewRecorder()
+
+ DomainRedirect(h, true).ServeHTTP(w, r)
+
+ if w.Code != http.StatusOK {
+ t.Errorf("expected status 200, got %d", w.Code)
+ }
+ })
+
+ t.Run("redirect", func(t *testing.T) {
+ r := httptest.NewRequest("GET", "http://example.com/", nil)
+ w := httptest.NewRecorder()
+
+ DomainRedirect(h, false).ServeHTTP(w, r)
+
+ if w.Code != http.StatusMovedPermanently {
+ t.Errorf("expected status 301, got %d", w.Code)
+ }
+ })
+
+ t.Run("allowed", func(t *testing.T) {
+ r := httptest.NewRequest("GET", "http://example.com/blog.rss", nil)
+ w := httptest.NewRecorder()
+
+ DomainRedirect(h, false).ServeHTTP(w, r)
+
+ if w.Code != http.StatusOK {
+ t.Errorf("expected status 200, got %d", w.Code)
+ }
+ })
+}