aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 160230b4cad828c5aecbff9dd88d6a8812a8505d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main

import (
	"fmt"
	"html/template"
	"io"
	"log"
	"net/http"
	"os"
	"strings"
	"github.com/joho/godotenv"
)

const youtubeAPI = "https://www.googleapis.com/youtube/v3/"
var tmpl = template.Must(template.ParseFiles("templates/watch.html"))

func apiRequest(w http.ResponseWriter, r *http.Request, endpoint string, videoIdParam string, part string) {
	videoID := r.URL.Query().Get("id")
	if videoID == "" {
		msg := "Missing ?id=VIDEO_ID parameter"
		http.Error(w, msg, http.StatusBadRequest)
		return
	}

	apiKey := os.Getenv("API_KEY")
	if apiKey == "" {
		msg := "API_KEY environment variable not set"
		http.Error(w, msg, http.StatusInternalServerError)
		log.Println(msg)
		return
	}

	//url := fmt.Sprintf("%s?part=snippet&id=%s&key=%s", youtubeAPI, videoID, apiKey)
	url := fmt.Sprintf("%s?part=%s&%s=%s&key=%s", youtubeAPI + endpoint, part, videoIdParam, videoID, apiKey)
	resp, err := http.Get(url)
	if err != nil {
		msg := "Failed to fetch video info: " + err.Error()
		http.Error(w, msg, http.StatusInternalServerError)
		log.Println(msg)
		return
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		msg := "YouTube API error: " + resp.Status
		http.Error(w, msg, http.StatusBadGateway)
		log.Println(msg)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	io.Copy(w, resp.Body)
}

func videoDetailsHandler(w http.ResponseWriter, r *http.Request) {
	apiRequest(w, r, "videos", "id", "snippet,statistics,topicDetails")
}

func commentThreadsHandler(w http.ResponseWriter, r *http.Request) {
	apiRequest(w, r, "commentThreads", "videoId", "snippet,replies&maxResults=100")
}

func handler(w http.ResponseWriter, r *http.Request) {
	path := strings.Trim(r.URL.Path, "/")
	if path == "" {
		path = "world"
	}
	data := struct {
		Id string
	}{
		Id: path,
	}
	err := tmpl.Execute(w, data)
	if err != nil {
		msg := "Template execution error"
		http.Error(w, msg, http.StatusInternalServerError)
		log.Println(msg)
	}
}

func main() {
	// load .env file if it exists
	godotenv.Load()

	// setup routes
	http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
	http.HandleFunc("/api/details", videoDetailsHandler)
	http.HandleFunc("/api/comments", commentThreadsHandler)

	http.HandleFunc("/", handler)
	log.Println("Listening on http://localhost:8080")
	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		log.Fatal(err)
	}
}