added pkg/app
This commit is contained in:
parent
e1090a7583
commit
c615ea6b14
2 changed files with 112 additions and 82 deletions
88
main.go
88
main.go
|
@ -1,97 +1,21 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"html/template"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/wybiral/tube/pkg/app"
|
||||||
"github.com/wybiral/tube/pkg/media"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const addr = "127.0.0.1:40404"
|
const addr = "127.0.0.1:40404"
|
||||||
|
|
||||||
var templates *template.Template
|
|
||||||
|
|
||||||
var library *media.Library
|
|
||||||
var playlist media.Playlist
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
library = media.NewLibrary()
|
a, err := app.NewApp()
|
||||||
err := library.Import("./videos")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
playlist = library.Playlist()
|
log.Printf("Serving at http://%s", addr)
|
||||||
if len(playlist) == 0 {
|
err = a.Run(addr)
|
||||||
log.Fatal("No valid videos found")
|
if err != nil {
|
||||||
}
|
log.Fatal(err)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
106
pkg/app/app.go
Normal file
106
pkg/app/app.go
Normal file
|
@ -0,0 +1,106 @@
|
||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"html/template"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
"github.com/wybiral/tube/pkg/media"
|
||||||
|
)
|
||||||
|
|
||||||
|
type App struct {
|
||||||
|
Library *media.Library
|
||||||
|
Playlist media.Playlist
|
||||||
|
Templates *template.Template
|
||||||
|
Router *mux.Router
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewApp() (*App, error) {
|
||||||
|
a := &App{}
|
||||||
|
lib := media.NewLibrary()
|
||||||
|
err := lib.Import("./videos")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
a.Library = lib
|
||||||
|
pl := lib.Playlist()
|
||||||
|
if len(pl) == 0 {
|
||||||
|
return nil, errors.New("No valid videos found")
|
||||||
|
}
|
||||||
|
a.Playlist = pl
|
||||||
|
a.Templates = template.Must(template.ParseGlob("templates/*"))
|
||||||
|
r := mux.NewRouter().StrictSlash(true)
|
||||||
|
r.HandleFunc("/", a.indexHandler).Methods("GET")
|
||||||
|
r.HandleFunc("/v/{id}.mp4", a.videoHandler).Methods("GET")
|
||||||
|
r.HandleFunc("/t/{id}", a.thumbHandler).Methods("GET")
|
||||||
|
r.HandleFunc("/{id}", a.pageHandler).Methods("GET")
|
||||||
|
fsHandler := http.StripPrefix(
|
||||||
|
"/static/",
|
||||||
|
http.FileServer(http.Dir("./static/")),
|
||||||
|
)
|
||||||
|
r.PathPrefix("/static/").Handler(fsHandler).Methods("GET")
|
||||||
|
a.Router = r
|
||||||
|
return a, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *App) Run(addr string) error {
|
||||||
|
return http.ListenAndServe(addr, a.Router)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *App) indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
log.Printf("/")
|
||||||
|
http.Redirect(w, r, "/"+a.Playlist[0].ID, 302)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *App) pageHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
vars := mux.Vars(r)
|
||||||
|
id := vars["id"]
|
||||||
|
log.Printf("/%s", id)
|
||||||
|
playing, ok := a.Library.Videos[id]
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
|
a.Templates.ExecuteTemplate(w, "index.html", &struct {
|
||||||
|
Playing *media.Video
|
||||||
|
Playlist media.Playlist
|
||||||
|
}{
|
||||||
|
Playing: playing,
|
||||||
|
Playlist: a.Playlist,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *App) videoHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
vars := mux.Vars(r)
|
||||||
|
id := vars["id"]
|
||||||
|
log.Printf("/v/%s", id)
|
||||||
|
m, ok := a.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 (a *App) thumbHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
vars := mux.Vars(r)
|
||||||
|
id := vars["id"]
|
||||||
|
log.Printf("/t/%s", id)
|
||||||
|
m, ok := a.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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue