summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authordavy <davy.wybiral@gmail.com>2019-06-27 14:22:47 -0500
committerdavy <davy.wybiral@gmail.com>2019-06-27 14:22:47 -0500
commitc615ea6b141571cfb04331d853af1acab829a9aa (patch)
tree630f37afd425c6c14be0607496048d23c57124c5 /main.go
parente1090a7583cfbb75fa03eb56d2b26d68732282cf (diff)
added pkg/app
Diffstat (limited to 'main.go')
-rw-r--r--main.go88
1 files changed, 6 insertions, 82 deletions
diff --git a/main.go b/main.go
index 9a470bc..df26d77 100644
--- a/main.go
+++ b/main.go
@@ -1,97 +1,21 @@
package main
import (
- "html/template"
"log"
- "net/http"
- "github.com/gorilla/mux"
- "github.com/wybiral/tube/pkg/media"
+ "github.com/wybiral/tube/pkg/app"
)
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")
+ a, err := app.NewApp()
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)
+ log.Printf("Serving at http://%s", addr)
+ err = a.Run(addr)
+ if err != nil {
+ log.Fatal(err)
}
}