all repos — fixyoutube-go @ c0b6900ba8c94a0dba08b736131ca72692a9f8d9

A better way to embed YouTube videos everywhere (inspired by FixTweet).

fix cached buffers, remove clear function
Marco Andronaco andronacomarco@gmail.com
Sat, 23 Nov 2024 23:57:58 +0100
commit

c0b6900ba8c94a0dba08b736131ca72692a9f8d9

parent

4fde0f88bf0b2a1271263fadb19eebd2c2caa622

3 files changed, 10 insertions(+), 39 deletions(-)

jump to
M fixyoutube.gofixyoutube.go

@@ -11,7 +11,6 @@ "golang.org/x/time/rate"

) var ( - apiKey string debugSwitch = false logger = logrus.New() )

@@ -56,14 +55,12 @@ logger.Debug("Debug mode enabled (rate limiting is disabled)")

debugSwitch = true } - apiKey = getenvDefault("API_KEY", "itsme") port := getenvDefault("PORT", "3000") burstTokens := getenvDefaultParse("BURST_TOKENS", "3") rateLimit := getenvDefaultParse("RATE_LIMIT", "1") r := http.NewServeMux() r.HandleFunc("/", indexHandler) - r.HandleFunc("/clear", clearHandler) r.HandleFunc("/watch", watchHandler) r.HandleFunc("/proxy/{videoId}", proxyHandler) r.HandleFunc("/{videoId}", shortHandler)
M handlers.gohandlers.go

@@ -17,11 +17,11 @@ const templatesDirectory = "templates/"

var ( //go:embed templates/index.html templates/video.html - templates embed.FS - indexTemplate = template.Must(template.ParseFS(templates, templatesDirectory+"index.html")) - videoTemplate = template.Must(template.ParseFS(templates, templatesDirectory+"video.html")) - userAgentRegex = regexp.MustCompile(`(?i)bot|facebook|embed|got|firefox\/92|firefox\/38|curl|wget|go-http|yahoo|generator|whatsapp|preview|link|proxy|vkshare|images|analyzer|index|crawl|spider|python|cfnetwork|node`) - videoRegex = regexp.MustCompile(`(?i)^[a-z0-9_-]{11}$`) + templates embed.FS + indexTemplate = template.Must(template.ParseFS(templates, templatesDirectory+"index.html")) + videoTemplate = template.Must(template.ParseFS(templates, templatesDirectory+"video.html")) + // userAgentRegex = regexp.MustCompile(`(?i)bot|facebook|embed|got|firefox\/92|firefox\/38|curl|wget|go-http|yahoo|generator|whatsapp|preview|link|proxy|vkshare|images|analyzer|index|crawl|spider|python|cfnetwork|node`) + videoRegex = regexp.MustCompile(`(?i)^[a-z0-9_-]{11}$`) ) func indexHandler(w http.ResponseWriter, r *http.Request) {

@@ -34,31 +34,6 @@ return

} buf.WriteTo(w) -} - -func clearHandler(w http.ResponseWriter, r *http.Request) { - if r.Method != http.MethodPost { - http.Redirect(w, r, "/", http.StatusFound) - return - } - - err := r.ParseForm() - if err != nil { - logger.Error("Failed to parse form in /clear.") - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - providedKey := r.PostForm.Get("apiKey") - if providedKey != apiKey { - logger.Debug("Wrong API key: ", providedKey) - http.Error(w, "Wrong or missing API key.", http.StatusForbidden) - return - } - - // rabbitpipe.ClearDB() - logger.Info("Cache cleared.") - http.Error(w, "Done.", http.StatusOK) } func videoHandler(videoID string, w http.ResponseWriter, r *http.Request) {

@@ -109,7 +84,6 @@

func shortHandler(w http.ResponseWriter, r *http.Request) { videoId := r.PathValue("videoId") videoHandler(videoId, w, r) - return } func proxyHandler(w http.ResponseWriter, r *http.Request) {
M invidious/proxy.goinvidious/proxy.go

@@ -55,19 +55,19 @@

func getBuffer(video rabbitpipe.Video) (*VideoBuffer, int) { vb, err := buffers.Get(video.VideoID) if err != nil { - // no cache entry + // cached buffer not found vb, s := urlToBuffer(GetVideoURL(video)) if vb != nil { if s == http.StatusOK && vb.Length > 0 { - buffers.Set(video.VideoID, *vb, 5*time.Minute) + buffers.Set(video.VideoID, *vb.Clone(), 5*time.Minute) return vb, s } } return nil, s } - //cache entry - videoBuffer := vb.Clone() - return videoBuffer, http.StatusOK + + // cached buffer found + return vb.Clone(), http.StatusOK } func ProxyVideoId(videoID string) (*VideoBuffer, int) {