From bcf66d92d664dd707937ae866830a6bee0751745 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Sat, 24 May 2025 12:52:32 -0500 Subject: cleanup all go code and add an index/home page that is composed from the readme file --- main.go | 90 +++++++++++++---------------------------------------------------- 1 file changed, 18 insertions(+), 72 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 511d7d6..e85649d 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,6 @@ package main import ( - "fmt" - "html/template" - "io" "log" "net/http" "os" @@ -11,86 +8,35 @@ import ( "github.com/joho/godotenv" ) -const youtubeAPI = "https://www.googleapis.com/youtube/v3/" -var tmpl = template.Must(template.ParseFiles("templates/watch.html")) +var debug = false +var apiKey = "" -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) { - id := strings.Trim(r.URL.Path, "/") - //if path == "" { - // TODO landing page - //} - if id == "watch" { - id = r.URL.Query().Get("v") - } - data := struct { - Id string - }{ - Id: id, - } - err := tmpl.Execute(w, data) - if err != nil { - msg := "Template execution error" - http.Error(w, msg, http.StatusInternalServerError) - log.Println(msg) +/* root handler */ +func rootHandler(w http.ResponseWriter, r *http.Request) { + path := strings.Trim(r.URL.Path, "/") + if path == "" { + //http.ServeFile(w, r, "static/index.html") + renderIndexTemplate(w) + } else { + renderWatchTemplate(w, path) } } +/* main */ func main() { // load .env file if it exists godotenv.Load() + debug = os.Getenv("DEBUG") != "" + apiKey = os.Getenv("API_KEY") // setup routes + setupRoutesAPI() + setupRoutesWatch() + setupHome() http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) - http.HandleFunc("/api/details", videoDetailsHandler) - http.HandleFunc("/api/comments", commentThreadsHandler) + http.HandleFunc("/", rootHandler) - http.HandleFunc("/", handler) + // start http server log.Println("Listening on http://localhost:8080") err := http.ListenAndServe(":8080", nil) if err != nil { -- cgit v1.2.3