summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authordavy <davy.wybiral@gmail.com>2019-06-26 14:02:31 -0500
committerdavy <davy.wybiral@gmail.com>2019-06-26 14:02:31 -0500
commite1090a7583cfbb75fa03eb56d2b26d68732282cf (patch)
tree624e842a7a61b76330f8381c519f8e6e37a942a0 /main.go
parenta946d2b96618ff3d49364d272cb637af19e6d54e (diff)
import code
Diffstat (limited to 'main.go')
-rw-r--r--main.go97
1 files changed, 97 insertions, 0 deletions
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..9a470bc
--- /dev/null
+++ b/main.go
@@ -0,0 +1,97 @@
+package main
+
+import (
+ "html/template"
+ "log"
+ "net/http"
+
+ "github.com/gorilla/mux"
+ "github.com/wybiral/tube/pkg/media"
+)
+
+const addr = "127.0.0.1:40404"
+
+var templates *template.Template
+
+var library *media.Library
+var playlist media.Playlist
+
+func main() {
+ library = media.NewLibrary()
+ err := library.Import("./videos")
+ if err != nil {
+ log.Fatal(err)
+ }
+ playlist = library.Playlist()
+ if len(playlist) == 0 {
+ log.Fatal("No valid videos found")
+ }
+ templates = template.Must(template.ParseGlob("templates/*"))
+ r := mux.NewRouter().StrictSlash(true)
+ r.HandleFunc("/", index).Methods("GET")
+ r.HandleFunc("/v/{id}.mp4", video).Methods("GET")
+ r.HandleFunc("/t/{id}", thumb).Methods("GET")
+ r.HandleFunc("/{id}", page).Methods("GET")
+ r.PathPrefix("/static/").Handler(
+ http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))),
+ ).Methods("GET")
+ log.Printf("Serving at %s", addr)
+ http.ListenAndServe(addr, r)
+}
+
+func index(w http.ResponseWriter, r *http.Request) {
+ log.Printf("/index")
+ http.Redirect(w, r, "/"+playlist[0].ID, 302)
+}
+
+func page(w http.ResponseWriter, r *http.Request) {
+ vars := mux.Vars(r)
+ id := vars["id"]
+ log.Printf(id)
+ playing, ok := library.Videos[id]
+ if !ok {
+ log.Print(ok)
+ return
+ }
+ w.Header().Set("Content-Type", "text/html; charset=utf-8")
+ templates.ExecuteTemplate(w, "index.html", &struct {
+ Playing *media.Video
+ Playlist media.Playlist
+ }{
+ Playing: playing,
+ Playlist: playlist,
+ })
+}
+
+func video(w http.ResponseWriter, r *http.Request) {
+ vars := mux.Vars(r)
+ id := vars["id"]
+ log.Print("/v/" + id)
+ m, ok := library.Videos[id]
+ if !ok {
+ return
+ }
+ title := m.Title
+ disposition := "attachment; filename=\"" + title + ".mp4\""
+ w.Header().Set("Content-Disposition", disposition)
+ w.Header().Set("Content-Type", "video/mp4")
+ http.ServeFile(w, r, "./videos/"+id+".mp4")
+}
+
+func thumb(w http.ResponseWriter, r *http.Request) {
+ vars := mux.Vars(r)
+ id := vars["id"]
+ log.Printf("/t/" + id)
+ m, ok := library.Videos[id]
+ if !ok {
+ return
+ }
+ w.Header().Set("Cache-Control", "public, max-age=7776000")
+ if m.ThumbType == "" {
+ w.Header().Set("Content-Type", "image/jpeg")
+ http.ServeFile(w, r, "static/defaulticon.jpg")
+ } else {
+ w.Header().Set("Content-Type", m.ThumbType)
+ w.Write(m.Thumb)
+ }
+}