set up redirects
Marco Andronaco andronacomarco@gmail.com
Fri, 18 Oct 2024 16:39:57 +0200
2 files changed,
19 insertions(+),
3 deletions(-)
M
src/app/handlers.go
→
src/app/handlers.go
@@ -55,7 +55,7 @@ return
} } -func videoHandler(w http.ResponseWriter, r *http.Request) { +func redirectHandler(w http.ResponseWriter, r *http.Request) { videoID := r.URL.Query().Get("v") if videoID == "" { videoID = r.PathValue("videoID")@@ -63,6 +63,22 @@ if videoID == "" {
http.Error(w, "Missing video ID", http.StatusBadRequest) return } + } + + if !videoRegex.MatchString(videoID) { + log.Println("Invalid video ID:", videoID) + http.Error(w, err404, http.StatusNotFound) + return + } + + http.Redirect(w, r, "/"+videoID, http.StatusFound) +} + +func videoHandler(w http.ResponseWriter, r *http.Request) { + videoID := r.PathValue("videoID") + if videoID == "" { + http.Error(w, "Missing video ID", http.StatusBadRequest) + return } if !videoRegex.MatchString(videoID) {
M
src/app/main.go
→
src/app/main.go
@@ -56,8 +56,8 @@ }
r.HandleFunc("GET /", indexHandler) - r.HandleFunc("GET /watch", videoHandler) - r.HandleFunc("GET /shorts/{videoID}", videoHandler) + r.HandleFunc("GET /watch", redirectHandler) + r.HandleFunc("GET /shorts/{videoID}", redirectHandler) r.HandleFunc("GET /{videoID}", videoHandler) r.HandleFunc("GET /{videoID}/{formatID}", videoHandler)