Working on frontend of CMS

Starting to work on basic frontend layout. Added new /json route for fetching RSS as JSON file.
This commit is contained in:
gmemstr 2017-06-18 13:57:43 -07:00
parent 7c3cb34587
commit 0ffefbf55e
7 changed files with 188 additions and 117 deletions

View file

@ -2,11 +2,43 @@
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>White Rabbit Podcasting CMS</title> <title>CMS Loading</title>
<link rel="stylesheet" href="/assets/styles.css"> <link rel="stylesheet" href="/assets/styles.css">
</head> </head>
<body> <body>
<h1>White Rabbit Podcasting CMS</h1> <h1 id="title" class="title">Loading</h1>
<p>Git Galaxy Podcast</p>
<div id="podcasts">
</div>
<script>
get("/json", function(data){
json = JSON.parse(data);
document.title = json.title;
document.getElementById("title").innerHTML = json.title;
for (i=0;i<json.items.length; i++){
var div = document.createElement('div');
div.className = 'podcast';
// Todo: Add audio element (must modify feeds fork first!)
div.innerHTML = '<h3>'+json.items[i].title+'</h3><p>'+json.items[i].summary+'</p>';
var element = document.getElementById("podcasts");
element.appendChild(div);
}
});
function get(url, callback) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText);
}
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}
</script>
</body> </body>
</html> </html>

View file

@ -1,3 +1,4 @@
body { body {
background-color: blue; background-color: #f9f9f9;
color: #5b9aff;
} }

21
feed.json Normal file
View file

@ -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"
}
}
]
}

View file

@ -4,9 +4,9 @@
<link>https://gitgalaxy.com</link> <link>https://gitgalaxy.com</link>
<description>discussion about open source projects</description> <description>discussion about open source projects</description>
<managingEditor>gabriel@gitgalaxy.com (Gabriel Simmer)</managingEditor> <managingEditor>gabriel@gitgalaxy.com (Gabriel Simmer)</managingEditor>
<pubDate>Tue, 13 Jun 2017 23:03:07 -0700</pubDate> <pubDate>Sun, 18 Jun 2017 13:35:51 -0700</pubDate>
<image> <image>
<url>https://static.gitgalaxy.com/podcast_image.png</url> <url>https://podcast.gitgalaxy.com/assets/podcast_image.png</url>
<title></title> <title></title>
<link></link> <link></link>
</image> </image>

View file

@ -1,84 +1,92 @@
package main package main
import ( import (
"log" "fmt"
"fmt" "io/ioutil"
"time" "log"
"io/ioutil" "strings"
"strings" "time"
"github.com/gmemstr/feeds"
"github.com/fsnotify/fsnotify" "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) func watch() {
go func() { watcher, err := fsnotify.NewWatcher()
for { if err != nil {
select { log.Fatal(err)
case event := <-watcher.Events: }
log.Println("event:", event) defer watcher.Close()
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/") done := make(chan bool)
if err != nil { go func() {
log.Fatal(err) for {
} select {
<-done 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(){ func generate_rss() {
now := time.Now() now := time.Now()
files, err := ioutil.ReadDir("podcasts") files, err := ioutil.ReadDir("podcasts")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
feed := &feeds.Feed{ feed := &feeds.Feed{
Title: "Git Galaxy Stargazers", Title: "Git Galaxy Stargazers",
Link: &feeds.Link{Href: "https://gitgalaxy.com"}, Link: &feeds.Link{Href: "https://gitgalaxy.com"},
Description: "discussion about open source projects", Description: "discussion about open source projects",
Author: &feeds.Author{Name:"Gabriel Simmer", Email: "gabriel@gitgalaxy.com"}, Author: &feeds.Author{Name: "Gabriel Simmer", Email: "gabriel@gitgalaxy.com"},
Created: now, Created: now,
Image: &feeds.Image{Url: "https://static.gitgalaxy.com/podcast_image.png"}, Image: &feeds.Image{Url: "https://podcast.gitgalaxy.com/assets/podcast_image.png"},
} }
for _, file := range files { for _, file := range files {
s := strings.Split(file.Name(), "_") s := strings.Split(file.Name(), "_")
t := strings.Split(s[1], ".") t := strings.Split(s[1], ".")
title := t[0] title := t[0]
date, err := time.Parse("2006-01-02",s[0]) date, err := time.Parse("2006-01-02", s[0])
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
feed.Items = []*feeds.Item{ feed.Items = []*feeds.Item{
&feeds.Item{ &feeds.Item{
Title: title, Title: title,
Link: &feeds.Link{Href: "https://gitgalaxy.com/podcast"}, Link: &feeds.Link{Href: "https://gitgalaxy.com/podcast"},
Enclosure: &feeds.Enclosure{Url: "https://podcast.gitgalaxy.com/download/" + file.Name(), Length: "100", Type: "audio/mpeg"}, Enclosure: &feeds.Enclosure{Url: "https://podcast.gitgalaxy.com/download/" + file.Name(), Length: "100", Type: "audio/mpeg"},
Description: "Hello, World!", Description: "Hello, World!",
Author: &feeds.Author{Name: "Gabriel Simmer", Email: "gabriel@gitgalaxy.com"}, Author: &feeds.Author{Name: "Gabriel Simmer", Email: "gabriel@gitgalaxy.com"},
Created: date, Created: date,
}, },
} }
} }
rss, err := feed.ToRss() rss, err := feed.ToRss()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
fmt.Println(rss) json, err := feed.ToJSON()
rss_byte := []byte(rss) if err != nil {
ioutil.WriteFile("feed.rss", rss_byte, 0644) 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)
} }

View file

@ -1 +1 @@
hi hello

View file

@ -1,46 +1,55 @@
package main package main
import ( import (
"fmt" "fmt"
"log" "io/ioutil"
"io/ioutil" "log"
"net/http" "net/http"
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
func RssHandler(w http.ResponseWriter, r *http.Request) { func RssHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/rss+xml") w.Header().Set("Content-Type", "application/rss+xml")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
data, err := ioutil.ReadFile("feed.rss") data, err := ioutil.ReadFile("feed.rss")
if err != nil { if err != nil {
panic(err) panic(err)
} }
w.Header().Set("Content-Length", fmt.Sprint(len(data))) w.Header().Set("Content-Length", fmt.Sprint(len(data)))
fmt.Fprint(w, string(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) { 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 { if err == nil {
w.Write(data) w.Write(data)
} else { } else {
w.WriteHeader(404) w.WriteHeader(404)
w.Write([]byte("404 Something went wrong - " + http.StatusText(404))) w.Write([]byte("404 Something went wrong - " + http.StatusText(404)))
} }
}
func DownloadHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Gorilla!\n"))
} }
func main() { func main() {
go watch() go watch()
r := mux.NewRouter() r := mux.NewRouter()
r.PathPrefix("/assets/").Handler(http.StripPrefix("/assets/", http.FileServer(http.Dir("assets/static")))) r.PathPrefix("/assets/").Handler(http.StripPrefix("/assets/", http.FileServer(http.Dir("assets/static"))))
r.HandleFunc("/", HomeHandler) r.PathPrefix("/download/").Handler(http.StripPrefix("/download/", http.FileServer(http.Dir("podcasts"))))
r.HandleFunc("/download/{episode}", DownloadHandler) r.HandleFunc("/", HomeHandler)
r.HandleFunc("/rss", RssHandler) r.HandleFunc("/rss", RssHandler)
http.Handle("/", r) r.HandleFunc("/json", JsonHandler)
log.Fatal(http.ListenAndServe(":8000", r)) http.Handle("/", r)
log.Fatal(http.ListenAndServe(":8000", r))
} }