From 0ffefbf55ea7891bfa7938f3c312c57adc263325 Mon Sep 17 00:00:00 2001 From: gmemstr Date: Sun, 18 Jun 2017 13:57:43 -0700 Subject: [PATCH] Working on frontend of CMS Starting to work on basic frontend layout. Added new /json route for fetching RSS as JSON file. --- assets/index.html | 40 ++++++- assets/static/styles.css | 3 +- feed.json | 21 ++++ feed.rss | 4 +- generate_rss.go | 160 ++++++++++++++------------ podcasts/2017-07-12_TESTING-WORLD.mp3 | 2 +- webserver.go | 75 ++++++------ 7 files changed, 188 insertions(+), 117 deletions(-) create mode 100644 feed.json diff --git a/assets/index.html b/assets/index.html index 94676f4..202a2f8 100644 --- a/assets/index.html +++ b/assets/index.html @@ -2,11 +2,43 @@ - White Rabbit Podcasting CMS + CMS Loading + -

White Rabbit Podcasting CMS

-

Git Galaxy Podcast

+

Loading

+ +
+ +
+ + - \ No newline at end of file + \ No newline at end of file diff --git a/assets/static/styles.css b/assets/static/styles.css index d60afbd..514b4c4 100644 --- a/assets/static/styles.css +++ b/assets/static/styles.css @@ -1,3 +1,4 @@ body { - background-color: blue; + background-color: #f9f9f9; + color: #5b9aff; } \ No newline at end of file diff --git a/feed.json b/feed.json new file mode 100644 index 0000000..d661c70 --- /dev/null +++ b/feed.json @@ -0,0 +1,21 @@ +{ + "version": "https://jsonfeed.org/version/1", + "title": "Git Galaxy Stargazers", + "home_page_url": "https://gitgalaxy.com", + "description": "discussion about open source projects", + "author": { + "name": "Gabriel Simmer" + }, + "items": [ + { + "id": "", + "url": "https://gitgalaxy.com/podcast", + "title": "TESTING-WORLD", + "summary": "Hello, World!", + "date_published": "2017-07-12T00:00:00Z", + "author": { + "name": "Gabriel Simmer" + } + } + ] +} \ No newline at end of file diff --git a/feed.rss b/feed.rss index b4f78eb..8dabf4a 100644 --- a/feed.rss +++ b/feed.rss @@ -4,9 +4,9 @@ https://gitgalaxy.com discussion about open source projects gabriel@gitgalaxy.com (Gabriel Simmer) - Tue, 13 Jun 2017 23:03:07 -0700 + Sun, 18 Jun 2017 13:35:51 -0700 - https://static.gitgalaxy.com/podcast_image.png + https://podcast.gitgalaxy.com/assets/podcast_image.png diff --git a/generate_rss.go b/generate_rss.go index 860d2d5..c49d4db 100644 --- a/generate_rss.go +++ b/generate_rss.go @@ -1,84 +1,92 @@ package main import ( - "log" - "fmt" - "time" - "io/ioutil" - "strings" - "github.com/gmemstr/feeds" - "github.com/fsnotify/fsnotify" + "fmt" + "io/ioutil" + "log" + "strings" + "time" + + "github.com/fsnotify/fsnotify" + "github.com/gmemstr/feeds" ) -func watch(){ - watcher, err := fsnotify.NewWatcher() - if err != nil { - log.Fatal(err) - } - defer watcher.Close() - - done := make(chan bool) - go func() { - for { - select { - case event := <-watcher.Events: - log.Println("event:", event) - if event.Op&fsnotify.Write == fsnotify.Write { - log.Println("modified file:", event.Name) - generate_rss() - } - case err := <-watcher.Errors: - log.Println("error:", err) - } - } - }() - - err = watcher.Add("podcasts/") - if err != nil { - log.Fatal(err) - } - <-done + +func watch() { + watcher, err := fsnotify.NewWatcher() + if err != nil { + log.Fatal(err) + } + defer watcher.Close() + + done := make(chan bool) + go func() { + for { + select { + case event := <-watcher.Events: + log.Println("event:", event) + if event.Op&fsnotify.Write == fsnotify.Write { + log.Println("modified file:", event.Name) + generate_rss() + } + case err := <-watcher.Errors: + log.Println("error:", err) + } + } + }() + + err = watcher.Add("podcasts/") + if err != nil { + log.Fatal(err) + } + <-done } -func generate_rss(){ - now := time.Now() - files, err := ioutil.ReadDir("podcasts") - if err != nil { - log.Fatal(err) - } +func generate_rss() { + now := time.Now() + files, err := ioutil.ReadDir("podcasts") + if err != nil { + log.Fatal(err) + } - feed := &feeds.Feed{ - Title: "Git Galaxy Stargazers", - Link: &feeds.Link{Href: "https://gitgalaxy.com"}, - Description: "discussion about open source projects", - Author: &feeds.Author{Name:"Gabriel Simmer", Email: "gabriel@gitgalaxy.com"}, - Created: now, - Image: &feeds.Image{Url: "https://static.gitgalaxy.com/podcast_image.png"}, - } + feed := &feeds.Feed{ + Title: "Git Galaxy Stargazers", + Link: &feeds.Link{Href: "https://gitgalaxy.com"}, + Description: "discussion about open source projects", + Author: &feeds.Author{Name: "Gabriel Simmer", Email: "gabriel@gitgalaxy.com"}, + Created: now, + Image: &feeds.Image{Url: "https://podcast.gitgalaxy.com/assets/podcast_image.png"}, + } - for _, file := range files { - s := strings.Split(file.Name(), "_") - t := strings.Split(s[1], ".") - title := t[0] - date, err := time.Parse("2006-01-02",s[0]) - if err != nil { - log.Fatal(err) - } - feed.Items = []*feeds.Item{ - &feeds.Item{ - Title: title, - Link: &feeds.Link{Href: "https://gitgalaxy.com/podcast"}, - Enclosure: &feeds.Enclosure{Url: "https://podcast.gitgalaxy.com/download/" + file.Name(), Length: "100", Type: "audio/mpeg"}, - Description: "Hello, World!", - Author: &feeds.Author{Name: "Gabriel Simmer", Email: "gabriel@gitgalaxy.com"}, - Created: date, - }, - } - } - rss, err := feed.ToRss() - if err != nil { - log.Fatal(err) - } - fmt.Println(rss) - rss_byte := []byte(rss) - ioutil.WriteFile("feed.rss", rss_byte, 0644) -} \ No newline at end of file + for _, file := range files { + s := strings.Split(file.Name(), "_") + t := strings.Split(s[1], ".") + title := t[0] + date, err := time.Parse("2006-01-02", s[0]) + if err != nil { + log.Fatal(err) + } + feed.Items = []*feeds.Item{ + &feeds.Item{ + Title: title, + Link: &feeds.Link{Href: "https://gitgalaxy.com/podcast"}, + Enclosure: &feeds.Enclosure{Url: "https://podcast.gitgalaxy.com/download/" + file.Name(), Length: "100", Type: "audio/mpeg"}, + Description: "Hello, World!", + Author: &feeds.Author{Name: "Gabriel Simmer", Email: "gabriel@gitgalaxy.com"}, + Created: date, + }, + } + } + rss, err := feed.ToRss() + if err != nil { + log.Fatal(err) + } + json, err := feed.ToJSON() + if err != nil { + log.Fatal(err) + } + fmt.Println(rss) + rss_byte := []byte(rss) + ioutil.WriteFile("feed.rss", rss_byte, 0644) + json_byte := []byte(json) + ioutil.WriteFile("feed.json", json_byte, 0644) +} diff --git a/podcasts/2017-07-12_TESTING-WORLD.mp3 b/podcasts/2017-07-12_TESTING-WORLD.mp3 index 32f95c0..b6fc4c6 100644 --- a/podcasts/2017-07-12_TESTING-WORLD.mp3 +++ b/podcasts/2017-07-12_TESTING-WORLD.mp3 @@ -1 +1 @@ -hi \ No newline at end of file +hello \ No newline at end of file diff --git a/webserver.go b/webserver.go index 2b394dc..03b6f8f 100644 --- a/webserver.go +++ b/webserver.go @@ -1,46 +1,55 @@ package main import ( - "fmt" - "log" - "io/ioutil" - "net/http" + "fmt" + "io/ioutil" + "log" + "net/http" - "github.com/gorilla/mux" + "github.com/gorilla/mux" ) + func RssHandler(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/rss+xml") - w.WriteHeader(http.StatusOK) - data, err := ioutil.ReadFile("feed.rss") - if err != nil { - panic(err) - } - w.Header().Set("Content-Length", fmt.Sprint(len(data))) - fmt.Fprint(w, string(data)) + w.Header().Set("Content-Type", "application/rss+xml") + w.WriteHeader(http.StatusOK) + data, err := ioutil.ReadFile("feed.rss") + if err != nil { + panic(err) + } + w.Header().Set("Content-Length", fmt.Sprint(len(data))) + fmt.Fprint(w, string(data)) +} + +func JsonHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + data, err := ioutil.ReadFile("feed.json") + if err != nil { + panic(err) + } + w.Header().Set("Content-Length", fmt.Sprint(len(data))) + fmt.Fprint(w, string(data)) } func HomeHandler(w http.ResponseWriter, r *http.Request) { - data, err := ioutil.ReadFile("assets/index.html") + data, err := ioutil.ReadFile("assets/index.html") - if err == nil { - w.Write(data) - } else { - w.WriteHeader(404) - w.Write([]byte("404 Something went wrong - " + http.StatusText(404))) - } -} - -func DownloadHandler(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("Gorilla!\n")) + if err == nil { + w.Write(data) + } else { + w.WriteHeader(404) + w.Write([]byte("404 Something went wrong - " + http.StatusText(404))) + } } func main() { - go watch() - r := mux.NewRouter() - r.PathPrefix("/assets/").Handler(http.StripPrefix("/assets/", http.FileServer(http.Dir("assets/static")))) - r.HandleFunc("/", HomeHandler) - r.HandleFunc("/download/{episode}", DownloadHandler) - r.HandleFunc("/rss", RssHandler) - http.Handle("/", r) - log.Fatal(http.ListenAndServe(":8000", r)) -} \ No newline at end of file + go watch() + r := mux.NewRouter() + r.PathPrefix("/assets/").Handler(http.StripPrefix("/assets/", http.FileServer(http.Dir("assets/static")))) + r.PathPrefix("/download/").Handler(http.StripPrefix("/download/", http.FileServer(http.Dir("podcasts")))) + r.HandleFunc("/", HomeHandler) + r.HandleFunc("/rss", RssHandler) + r.HandleFunc("/json", JsonHandler) + http.Handle("/", r) + log.Fatal(http.ListenAndServe(":8000", r)) +}