diff options
author | Jackson Taylor <jtaylormuffins@gmail.com> | 2018-06-27 09:58:43 -0400 |
---|---|---|
committer | Jackson Taylor <jtaylormuffins@gmail.com> | 2018-06-27 09:58:43 -0400 |
commit | 90b4b571dd19c7677969d7edb0d9adc96b50e364 (patch) | |
tree | d8826c50e1e190878f98bbf998f0d4cc74af92e8 | |
parent | 793bdf72db2bac0d93deffbf46ab32395f4c5ce3 (diff) |
Doing some changes to how I display the index pages.
-rw-r--r-- | main.exe | bin | 0 -> 8091136 bytes | |||
-rw-r--r-- | main.go | 74 | ||||
-rw-r--r-- | temp/header-footer.gohtml | 2 | ||||
-rw-r--r-- | temp/home.gohtml | 2 | ||||
-rw-r--r-- | temp/movieIndex.gohtml | 4 | ||||
-rw-r--r-- | temp/viewer.gohtml | 4 |
6 files changed, 58 insertions, 28 deletions
diff --git a/main.exe b/main.exe Binary files differnew file mode 100644 index 0000000..89377a7 --- /dev/null +++ b/main.exe @@ -10,12 +10,15 @@ import ( ) const ( + mediaDirectory = "./media/" moviesDirectory = "./media/movies/" + showsDirectory = "./media/shows/" ) type Page struct { Title string - MovieTitleAndSource map[string]string + MediaTitleAndSource map[string]string + MediaSrc string } var tmpl *template.Template @@ -25,51 +28,75 @@ func init() { } func main() { - moviesFileServer := http.FileServer(http.Dir(moviesDirectory)) http.HandleFunc("/", index) http.HandleFunc("/movies/", movieIndex) - http.Handle("/movies/files/", moviesFileServer) + http.Handle("/files/", http.StripPrefix("/files/", http.FileServer(http.Dir(mediaDirectory)))) + http.HandleFunc("/viewmovie/", movieViewerHandler) + http.HandleFunc("/shows", showsIndex) http.ListenAndServe(":8080", nil) } func index(w http.ResponseWriter, req *http.Request) { - w.Header().Set("Content-Type", "text/html") - p := Page{ Title: "Home", } - err := tmpl.ExecuteTemplate(w, "header", p) - check(err, w) - - err = tmpl.ExecuteTemplate(w, "home.gohtml", nil) + err := tmpl.ExecuteTemplate(w, "home.gohtml", p) check(err, w) - err = tmpl.ExecuteTemplate(w, "footer", nil) - check(err, w) } // /movies handler func movieIndex(w http.ResponseWriter, req *http.Request) { - w.Header().Set("Content-type", "text/html") - movMap := getMovieInformation(w) + movMap := getMediaInformation(w, moviesDirectory) + p := Page{ Title: "Movies", - MovieTitleAndSource: movMap, + MediaTitleAndSource: movMap, } - err := tmpl.ExecuteTemplate(w, "header", p) + err := tmpl.ExecuteTemplate(w, "movieIndex.gohtml", p) check(err, w) - err = tmpl.ExecuteTemplate(w, "movieIndex.gohtml", p) +} + +func showsIndex(w http.ResponseWriter, req *http.Request) { + + seasonInfo := getMediaInformation(w, showsDirectory) + + p := Page{ + Title: "Shows", + MediaTitleAndSource: seasonInfo, + } + + err := tmpl.ExecuteTemplate(w, "movieIndex.gohtml", p) check(err, w) +} + +func movieViewerHandler(w http.ResponseWriter, req *http.Request) { + url := req.URL.Path + + var title string + title = strings.TrimSuffix(url, filepath.Ext(url)) + title = strings.Replace(title, "/movies/", "/files/", -1) + title = strings.Replace(title, filepath.Ext(title), "", -1) + title = strings.Replace(title, "/viewmovie/", "", -1) + url = strings.Replace(url, "/viewmovie/", "/files/", -1) + + url = "http://localhost:8080" + url + + p := Page{ + Title: title, + MediaSrc: url, + } - err = tmpl.ExecuteTemplate(w, "footer", nil) + err := tmpl.ExecuteTemplate(w, "viewer.gohtml", p) check(err, w) } +//Fine for now, possibly want to do more with errors func check(err error, w http.ResponseWriter) { if err != nil { fmt.Println(err) @@ -77,17 +104,12 @@ func check(err error, w http.ResponseWriter) { } } -func getMovieInformation(w http.ResponseWriter) map[string]string { +func getMediaInformation(w http.ResponseWriter, path string) map[string]string { m := make(map[string]string) - err := filepath.Walk(moviesDirectory, func(path string, info os.FileInfo, err error) (er error) { - if !info.IsDir() { - src := info.Name() - name := strings.TrimSuffix(src, filepath.Ext(src)) - fmt.Println(src) - src = "/movies/files/" + src - m[name] = src - } + err := filepath.Walk(path, func(path string, info os.FileInfo, err error) (er error) { + name := info.Name() + m[name] = path + name return nil }) diff --git a/temp/header-footer.gohtml b/temp/header-footer.gohtml index c8d7a67..0c14d9e 100644 --- a/temp/header-footer.gohtml +++ b/temp/header-footer.gohtml @@ -2,7 +2,7 @@ <!DOCTYPE html> <html> <head> - <title>JAMA | {{.Title}}</title> + <title>JAMA | {{$.Title}}</title> <link rel="stylesheet" href="/static/main.css"> </head> <body> diff --git a/temp/home.gohtml b/temp/home.gohtml index c754085..7487fcd 100644 --- a/temp/home.gohtml +++ b/temp/home.gohtml @@ -1,3 +1,4 @@ +{{ template "header" .Title}} <h1>JAMA</h1> <div id="movieSection"> <a href="/movies">Movies</a> @@ -5,3 +6,4 @@ <div id="showSection"> <a href="/shows">Shows</a> </div> +{{ template "footer" }} diff --git a/temp/movieIndex.gohtml b/temp/movieIndex.gohtml index 5b63c46..e610b1b 100644 --- a/temp/movieIndex.gohtml +++ b/temp/movieIndex.gohtml @@ -1,5 +1,6 @@ +{{ template "header" .Title}} <table border="1px solid black"> - {{ range $key, $value := .MovieTitleAndSource}} + {{ range $key, $value := .MediaTitleAndSource}} <tr> <td> <a href="{{ $value }}">{{ $key }}</a> @@ -7,3 +8,4 @@ </tr> {{ end}} </table> +{{ template "footer" }} diff --git a/temp/viewer.gohtml b/temp/viewer.gohtml new file mode 100644 index 0000000..1f3c114 --- /dev/null +++ b/temp/viewer.gohtml @@ -0,0 +1,4 @@ +{{ template "header" .Title}} +<h1>{{ .Title }}</h1> +<video src="{{ .MediaSrc }}" controls autoplay></video> +{{ template "footer" }} |