summaryrefslogtreecommitdiff
path: root/pkg/app
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/app')
-rw-r--r--pkg/app/app.go15
-rw-r--r--pkg/app/config.go5
-rw-r--r--pkg/app/listener.go4
3 files changed, 24 insertions, 0 deletions
diff --git a/pkg/app/app.go b/pkg/app/app.go
index e423359..1de26ce 100644
--- a/pkg/app/app.go
+++ b/pkg/app/app.go
@@ -17,6 +17,7 @@ import (
"github.com/wybiral/tube/pkg/media"
)
+// App represents main application.
type App struct {
Config *Config
Library *media.Library
@@ -26,6 +27,7 @@ type App struct {
Router *mux.Router
}
+// NewApp returns a new instance of App from Config.
func NewApp(cfg *Config) (*App, error) {
if cfg == nil {
cfg = DefaultConfig()
@@ -33,24 +35,30 @@ func NewApp(cfg *Config) (*App, error) {
a := &App{
Config: cfg,
}
+ // Setup Library
a.Library = media.NewLibrary()
+ // Setup Watcher
w, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
a.Watcher = w
+ // Setup Listener
ln, err := newListener(cfg.Server)
if err != nil {
return nil, err
}
a.Listener = ln
+ // Setup Templates
a.Templates = template.Must(template.ParseGlob("templates/*"))
+ // Setup Router
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("/v/{id}", a.pageHandler).Methods("GET")
r.HandleFunc("/feed.xml", a.rssHandler).Methods("GET")
+ // Static file handler
fsHandler := http.StripPrefix(
"/static/",
http.FileServer(http.Dir("./static/")),
@@ -60,6 +68,7 @@ func NewApp(cfg *Config) (*App, error) {
return a, nil
}
+// Run imports the library and starts server.
func (a *App) Run() error {
path := a.Config.LibraryPath
err := a.Library.Import(path)
@@ -71,6 +80,7 @@ func (a *App) Run() error {
return http.Serve(a.Listener, a.Router)
}
+// Watch the library path and update Library with changes.
func (a *App) watch() {
for {
e, ok := <-a.Watcher.Events
@@ -92,6 +102,7 @@ func (a *App) watch() {
}
}
+// HTTP handler for /
func (a *App) indexHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("/")
pl := a.Library.Playlist()
@@ -108,6 +119,7 @@ func (a *App) indexHandler(w http.ResponseWriter, r *http.Request) {
}
}
+// HTTP handler for /v/id
func (a *App) pageHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
@@ -133,6 +145,7 @@ func (a *App) pageHandler(w http.ResponseWriter, r *http.Request) {
})
}
+// HTTP handler for /v/id.mp4
func (a *App) videoHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
@@ -149,6 +162,7 @@ func (a *App) videoHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, path)
}
+// HTTP handler for /t/id
func (a *App) thumbHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
@@ -167,6 +181,7 @@ func (a *App) thumbHandler(w http.ResponseWriter, r *http.Request) {
}
}
+// HTTP handler for /feed.xml
func (a *App) rssHandler(w http.ResponseWriter, r *http.Request) {
cfg := a.Config.Feed
now := time.Now()
diff --git a/pkg/app/config.go b/pkg/app/config.go
index 12cf21d..f8ca2e8 100644
--- a/pkg/app/config.go
+++ b/pkg/app/config.go
@@ -5,17 +5,20 @@ import (
"os"
)
+// Config settings for main App.
type Config struct {
LibraryPath string `json:"library"`
Server *ServerConfig `json:"server"`
Feed *FeedConfig `json:"feed"`
}
+// ServerConfig settings for App Server.
type ServerConfig struct {
Host string `json:"host"`
Port int `json:"port"`
}
+// FeedConfig settings for App Feed.
type FeedConfig struct {
ExternalURL string `json:"external_url"`
Title string `json:"title"`
@@ -28,6 +31,7 @@ type FeedConfig struct {
Copyright string `json:"copyright"`
}
+// DefaultConfig returns Config initialized with default values.
func DefaultConfig() *Config {
return &Config{
LibraryPath: "videos",
@@ -41,6 +45,7 @@ func DefaultConfig() *Config {
}
}
+// ReadFile reads a JSON file into Config.
func (c *Config) ReadFile(path string) error {
f, err := os.Open(path)
if err != nil {
diff --git a/pkg/app/listener.go b/pkg/app/listener.go
index 7d42290..a4236d8 100644
--- a/pkg/app/listener.go
+++ b/pkg/app/listener.go
@@ -1,3 +1,7 @@
+// Instead of using the default new.Listener this file will construct a custom
+// one. The main purpose for this is to have more control over the settings
+// (like keep-alive) and to retrieve the assigned port when using port 0.
+
package app
import (