all repos — gopipe @ 83cd0bd9237351f9da3ef236617c2f915f2277c4

Embed YouTube videos on Telegram, Discord and more!

src/app/main.go (view raw)

 1package app
 2
 3import (
 4	"bytes"
 5	"log"
 6	"net/http"
 7	"os"
 8	"time"
 9
10	g "github.com/birabittoh/gopipe/src/globals"
11	"github.com/birabittoh/myks"
12	"github.com/joho/godotenv"
13	"golang.org/x/time/rate"
14)
15
16func Main() {
17	godotenv.Load()
18
19	g.Debug = parseBool(os.Getenv("APP_DEBUG"))
20	if g.Debug {
21		log.Println("Debug mode enabled.")
22	}
23
24	g.Proxy = parseBool(os.Getenv("APP_PROXY"))
25	if g.Proxy {
26		g.PKS = myks.New[bytes.Buffer](3 * time.Minute)
27		log.Println("Proxy mode enabled.")
28	}
29
30	g.Port = getEnvDefault("APP_PORT", "3000")
31
32	r := http.NewServeMux()
33
34	var serveMux http.Handler
35	if g.Debug {
36		serveMux = r
37	} else {
38		limiter := rate.NewLimiter(rate.Limit(1), 3) // TODO: use env vars
39		serveMux = limit(limiter, r)
40	}
41
42	r.HandleFunc("GET /", indexHandler)
43
44	// video handlers
45	r.HandleFunc("GET /watch", videoHandler)
46	r.HandleFunc("GET /shorts/{videoID}", videoHandler)
47	r.HandleFunc("GET /{videoID}", videoHandler)
48	r.HandleFunc("GET /{videoID}/{formatID}", videoHandler)
49
50	r.HandleFunc("GET /proxy/{videoID}", proxyHandler)
51	r.HandleFunc("GET /proxy/{videoID}/{formatID}", proxyHandler)
52
53	// r.HandleFunc("GET /robots.txt", robotsHandler)
54
55	log.Println("Serving on port " + g.Port)
56	log.Fatal(http.ListenAndServe(":"+g.Port, serveMux))
57}