Implemented Gorilla mux router & index page

Working on a simple frontend for displaying various things e.g latest episode etc.
This commit is contained in:
gmemstr 2017-06-15 09:21:22 -07:00
parent 78d955b116
commit 581f7671f7
3 changed files with 40 additions and 4 deletions

12
assets/index.html Normal file
View file

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>White Rabbit Podcasting CMS</title>
<link rel="stylesheet" href="/assets/styles.css">
</head>
<body>
<h1>White Rabbit Podcasting CMS</h1>
<p>Git Galaxy Podcast</p>
</body>
</html>

3
assets/static/styles.css Normal file
View file

@ -0,0 +1,3 @@
body {
background-color: blue;
}

View file

@ -5,8 +5,10 @@ import (
"log"
"io/ioutil"
"net/http"
"github.com/gorilla/mux"
)
func rootHandler(w http.ResponseWriter, r *http.Request) {
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")
@ -17,9 +19,28 @@ func rootHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, string(data))
}
func HomeHandler(w http.ResponseWriter, r *http.Request) {
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"))
}
func main() {
go watch()
http.HandleFunc("/", rootHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
fmt.Println("watching folder")
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))
}