all repos — gopipe @ main

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	g.AdminUser = getEnvDefault("APP_ADMIN_USER", "admin")
34	g.AdminPass = getEnvDefault("APP_ADMIN_PASS", "admin")
35
36	if g.AdminUser == "admin" && g.AdminPass == "admin" {
37		log.Println("Admin credentials not set. Please set APP_ADMIN_USER and APP_ADMIN_PASS.")
38	}
39
40	// set up extemplate
41	err := g.XT.ParseDir("templates", []string{".tmpl"})
42	if err != nil {
43		log.Fatal(err)
44	}
45
46	// set up http server
47	r := http.NewServeMux()
48
49	var serveMux http.Handler
50	if g.Debug {
51		serveMux = r
52	} else {
53		limiter := rate.NewLimiter(rate.Limit(1), 3) // TODO: use env vars
54		serveMux = limit(limiter, r)
55	}
56
57	r.HandleFunc("GET /", indexHandler)
58
59	r.HandleFunc("GET /watch", redirectHandler)
60	r.HandleFunc("GET /shorts/{videoID}", redirectHandler)
61	r.HandleFunc("GET /{videoID}", videoHandler)
62	r.HandleFunc("GET /{videoID}/{formatID}", videoHandler)
63
64	r.HandleFunc("GET /proxy/{videoID}", proxyHandler)
65	r.HandleFunc("GET /proxy/{videoID}/{formatID}", proxyHandler)
66	r.HandleFunc("GET /sub/{videoID}/{language}", subHandler)
67
68	r.HandleFunc("POST /download", downloadHandler)
69
70	r.HandleFunc("GET /cache", cacheHandler)
71
72	log.Println("Serving on port " + g.Port)
73	log.Fatal(http.ListenAndServe(":"+g.Port, serveMux))
74}