all repos — gopipe @ 6eff4ae6fb13714b5c8a946da03292c0f530ac4e

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	// read env vars
20	g.Debug = parseBool(os.Getenv("APP_DEBUG"))
21	if g.Debug {
22		log.Println("Debug mode enabled.")
23	}
24
25	g.Proxy = parseBool(os.Getenv("APP_PROXY"))
26	if g.Proxy {
27		g.PKS = myks.New[bytes.Buffer](3 * time.Minute)
28		log.Println("Proxy mode enabled.")
29	}
30
31	g.Port = getEnvDefault("APP_PORT", "3000")
32
33	// set up extemplate
34	err := g.XT.ParseDir("templates", []string{".tmpl"})
35	if err != nil {
36		log.Fatal(err)
37	}
38
39	// set up http server
40	r := http.NewServeMux()
41
42	var serveMux http.Handler
43	if g.Debug {
44		serveMux = r
45	} else {
46		limiter := rate.NewLimiter(rate.Limit(1), 3) // TODO: use env vars
47		serveMux = limit(limiter, r)
48	}
49
50	r.HandleFunc("GET /", indexHandler)
51
52	r.HandleFunc("GET /watch", videoHandler)
53	r.HandleFunc("GET /shorts/{videoID}", videoHandler)
54	r.HandleFunc("GET /{videoID}", videoHandler)
55	r.HandleFunc("GET /{videoID}/{formatID}", videoHandler)
56
57	r.HandleFunc("GET /proxy/{videoID}", proxyHandler)
58	r.HandleFunc("GET /proxy/{videoID}/{formatID}", proxyHandler)
59	r.HandleFunc("GET /sub/{videoID}/{language}", subHandler)
60
61	// r.HandleFunc("GET /robots.txt", robotsHandler)
62
63	log.Println("Serving on port " + g.Port)
64	log.Fatal(http.ListenAndServe(":"+g.Port, serveMux))
65}