From 15bf56a1e742790d77f8b13bbf9771187d2b80dc Mon Sep 17 00:00:00 2001
From: Manuel <git@huesers.de>
Date: Thu, 20 Mar 2025 07:40:00 +0100
Subject: [PATCH] feat(web): Support redirecting to custom url for index page

---
 config.example.toml  | 8 +++++++-
 pkg/config/config.go | 1 +
 pkg/web/web.go       | 7 +++++++
 3 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/config.example.toml b/config.example.toml
index 36f09a9..fdcd679 100644
--- a/config.example.toml
+++ b/config.example.toml
@@ -1,13 +1,19 @@
 username = ""
 password = "asd123"
 #proxyaddr = "socks5://localhost:5555"
-hostURL = "https://my.domain.tld"
+
+#usewebserver = false
+indextarget = "https://discord.gg/Kw4MFGxYEj" # Optional
+hostURL = "https://my.domain.tld" # Can be omitted if not using webserver
+
 webhook = "https://domain.tld/api/webhooks/"
+
 channels = [
   "NinEverything",
   "NintendoEurope",
   "NintendoAmerica",
 ]
+
 # Binary representation for efficient filtering
 # Bit from left to right (most to least significant bit): IsSelfThread, IsRetweet, IsReply, IsPin, IsQuoted
 filter = [
diff --git a/pkg/config/config.go b/pkg/config/config.go
index dd540fb..499badb 100644
--- a/pkg/config/config.go
+++ b/pkg/config/config.go
@@ -20,6 +20,7 @@ type Config struct {
 	WebPort      uint16
 	UserAgents   []string
 	NitterBase   string
+	IndexTarget  string
 }
 
 func ConfigFromFile(filePath string) (*Config, error) {
diff --git a/pkg/web/web.go b/pkg/web/web.go
index 11b99f1..a8b5a5a 100644
--- a/pkg/web/web.go
+++ b/pkg/web/web.go
@@ -72,12 +72,19 @@ func New(config *config.Config, scraper *ts.Scraper) (*WebServer, error) {
 			WriteTimeout: 30 * time.Second,
 		},
 	}
+	sm.HandleFunc("GET /", ws.handleIndex)
 	sm.HandleFunc("GET /avatar/{username}", ws.handleAvatar)
 	sm.HandleFunc("GET /tweet/{id}", ws.handleTweet)
 	sm.HandleFunc("GET /video/{id}", ws.handleVideo)
 	return ws, nil
 }
 
+func (ws WebServer) handleIndex(w http.ResponseWriter, r *http.Request) {
+	if ws.config.IndexTarget != "" {
+		http.Redirect(w, r, ws.config.IndexTarget, http.StatusPermanentRedirect)
+	}
+}
+
 func (ws WebServer) handleAvatar(w http.ResponseWriter, r *http.Request) {
 	username := r.PathValue("username")