summaryrefslogtreecommitdiff
path: root/pkg/app
diff options
context:
space:
mode:
authordavy <davy.wybiral@gmail.com>2019-06-28 22:50:59 -0500
committerdavy <davy.wybiral@gmail.com>2019-06-28 22:50:59 -0500
commit0b87a1137f330a36419030a4e82363ed36563a23 (patch)
treeaa8312e7f72900115f09d523ae38b02d2a425a46 /pkg/app
parentff0b322a332b6c20502faf564292f77001fdb965 (diff)
added feeds
Diffstat (limited to 'pkg/app')
-rw-r--r--pkg/app/app.go43
-rw-r--r--pkg/app/config.go16
2 files changed, 59 insertions, 0 deletions
diff --git a/pkg/app/app.go b/pkg/app/app.go
index 665b602..d13a888 100644
--- a/pkg/app/app.go
+++ b/pkg/app/app.go
@@ -5,8 +5,12 @@ import (
"log"
"net"
"net/http"
+ "net/url"
+ "path"
+ "time"
"github.com/fsnotify/fsnotify"
+ "github.com/gorilla/feeds"
"github.com/gorilla/mux"
"github.com/wybiral/tube/pkg/media"
)
@@ -44,6 +48,7 @@ func NewApp(cfg *Config) (*App, error) {
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")
fsHandler := http.StripPrefix(
"/static/",
http.FileServer(http.Dir("./static/")),
@@ -159,3 +164,41 @@ func (a *App) thumbHandler(w http.ResponseWriter, r *http.Request) {
w.Write(m.Thumb)
}
}
+
+func (a *App) rssHandler(w http.ResponseWriter, r *http.Request) {
+ cfg := a.Config.Feed
+ now := time.Now()
+ f := &feeds.Feed{
+ Title: cfg.Title,
+ Link: &feeds.Link{Href: cfg.Link},
+ Description: cfg.Description,
+ Author: &feeds.Author{
+ Name: cfg.Author.Name,
+ Email: cfg.Author.Email,
+ },
+ Created: now,
+ Copyright: cfg.Copyright,
+ }
+ for _, v := range a.Library.Playlist() {
+ u, err := url.Parse(cfg.ExternalURL)
+ if err != nil {
+ return
+ }
+ u.Path = path.Join(u.Path, "v", v.ID)
+ id := u.String()
+ f.Items = append(f.Items, &feeds.Item{
+ Id: id,
+ Title: v.Title,
+ Link: &feeds.Link{Href: id},
+ Description: v.Description,
+ Author: &feeds.Author{
+ Name: cfg.Author.Name,
+ Email: cfg.Author.Email,
+ },
+ Created: v.Timestamp,
+ })
+ }
+ w.Header().Set("Cache-Control", "public, max-age=7776000")
+ w.Header().Set("Content-Type", "text/xml")
+ f.WriteRss(w)
+}
diff --git a/pkg/app/config.go b/pkg/app/config.go
index c4ea170..12cf21d 100644
--- a/pkg/app/config.go
+++ b/pkg/app/config.go
@@ -8,6 +8,7 @@ import (
type Config struct {
LibraryPath string `json:"library"`
Server *ServerConfig `json:"server"`
+ Feed *FeedConfig `json:"feed"`
}
type ServerConfig struct {
@@ -15,6 +16,18 @@ type ServerConfig struct {
Port int `json:"port"`
}
+type FeedConfig struct {
+ ExternalURL string `json:"external_url"`
+ Title string `json:"title"`
+ Link string `json:"link"`
+ Description string `json:"description"`
+ Author struct {
+ Name string `json:"name"`
+ Email string `json:"email"`
+ } `json:"author"`
+ Copyright string `json:"copyright"`
+}
+
func DefaultConfig() *Config {
return &Config{
LibraryPath: "videos",
@@ -22,6 +35,9 @@ func DefaultConfig() *Config {
Host: "127.0.0.1",
Port: 0,
},
+ Feed: &FeedConfig{
+ ExternalURL: "http://localhost",
+ },
}
}