-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyt_fetch.go
48 lines (40 loc) · 1.16 KB
/
yt_fetch.go
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
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"os"
generate "github.com/kangkyu/yt_fetch/generate_csv"
)
func main() {
port := "8080"
if value, ok := os.LookupEnv("PORT"); ok {
port = value
}
http.HandleFunc("GET /", pageHandler)
http.HandleFunc("POST /fetches", fetchHandler)
log.Println("Listen on localhost:" + port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
func pageHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
t, err := template.ParseFiles("page.html")
if err != nil {
http.Error(w, "file not found", 404)
return
}
t.Execute(w, "")
}
func fetchHandler(w http.ResponseWriter, r *http.Request) {
channelID := r.FormValue("uuid")
// TODO: need validation channelID presence
w.Header().Set("Content-Type", "text/csv")
w.Header().Set("Content-Disposition", "attachment;filename=videosof"+channelID+".csv")
if err := generate.GenerateCSV(w, channelID); err != nil {
fmt.Fprint(w, "could not generate CSV:\n")
fmt.Fprint(w, err.Error())
// TODO: error response should be 400 or 500 by cases, how do you tell?
// http.Error(w, "internal error", 500)
}
}