pages.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package pages
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "html/template"
  6. "net/http"
  7. "strings"
  8. "gogs.gildas.ch/gildas/movies"
  9. "gogs.gildas.ch/gildas/movies/omdb"
  10. "gopkg.in/russross/blackfriday.v2"
  11. )
  12. func Router(c *movies.Collection) http.HandlerFunc {
  13. homeHandler := Home(c)
  14. movieHandler := Movie(c)
  15. listHandler := List(c)
  16. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  17. path := r.URL.Path
  18. switch {
  19. case path == "/":
  20. homeHandler(w, r)
  21. return
  22. case path == "/style.css":
  23. http.ServeFile(w, r, "templates/style.css")
  24. return
  25. case strings.HasPrefix(path, "/l/"):
  26. listHandler(w, r)
  27. return
  28. case strings.HasPrefix(path, "/tt"):
  29. movieHandler(w, r)
  30. return
  31. }
  32. })
  33. }
  34. func Home(c *movies.Collection) http.HandlerFunc {
  35. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  36. t, err := template.ParseFiles("templates/home.html")
  37. if err != nil {
  38. fmt.Println(err)
  39. return
  40. }
  41. var errs []error
  42. if r.Method == "POST" {
  43. if r.FormValue("omdb_json") != "" {
  44. m, err := movies.Unmarshal([]byte(r.FormValue("omdb_json")))
  45. if err != nil {
  46. w.WriteHeader(http.StatusBadRequest)
  47. errs = append(errs, err)
  48. } else {
  49. c.Add(m)
  50. }
  51. }
  52. if r.FormValue("list-id") != "" {
  53. if err := c.AddList(r.FormValue("list-id"), r.FormValue("list-title")); err != nil {
  54. errs = append(errs, err)
  55. }
  56. }
  57. }
  58. var omdbString string
  59. if imdbID := r.URL.Query().Get("imdb_id"); imdbID != "" {
  60. b, err := omdb.Get(imdbID)
  61. if err != nil {
  62. errs = append(errs, err)
  63. } else {
  64. omdbString = string(b)
  65. }
  66. }
  67. t.Execute(w, map[string]interface{}{
  68. "Collection": c,
  69. "IMDBID": r.URL.Query().Get("imdb_id"),
  70. "OMDBString": omdbString,
  71. "Errors": errs,
  72. })
  73. })
  74. }
  75. func Movie(c *movies.Collection) http.HandlerFunc {
  76. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  77. t, err := template.ParseFiles("templates/movie.html")
  78. if err != nil {
  79. fmt.Println(err)
  80. return
  81. }
  82. var errs []error
  83. splitted := strings.Split(r.URL.Path, "/")
  84. if len(splitted) < 2 {
  85. w.WriteHeader(http.StatusNotFound)
  86. return
  87. }
  88. imdbID := splitted[1]
  89. m, ok := c.Get(imdbID)
  90. if !ok {
  91. w.WriteHeader(http.StatusNotFound)
  92. return
  93. }
  94. if r.Method == "POST" {
  95. updated, err := movies.Unmarshal([]byte(r.FormValue("movie_json")))
  96. if err != nil {
  97. errs = append(errs, err)
  98. } else if imdbID != updated.IMDBID {
  99. errs = append(errs, fmt.Errorf("you cannot change the imdb id."))
  100. } else {
  101. m = updated
  102. c.Update(updated)
  103. }
  104. }
  105. b, err := json.MarshalIndent(m, "", " ")
  106. if err != nil {
  107. errs = append(errs, err)
  108. }
  109. t.Execute(w, map[string]interface{}{
  110. "Movie": m,
  111. "MovieJSON": string(b),
  112. "Errors": errs,
  113. })
  114. })
  115. }
  116. func List(c *movies.Collection) http.HandlerFunc {
  117. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  118. t, err := template.ParseFiles("templates/list.html")
  119. if err != nil {
  120. fmt.Println(err)
  121. return
  122. }
  123. var errs []error
  124. splitted := strings.Split(r.URL.Path, "/")
  125. if len(splitted) < 3 {
  126. w.WriteHeader(http.StatusNotFound)
  127. return
  128. }
  129. listID := splitted[2]
  130. l, ok := c.GetList(listID)
  131. if !ok {
  132. w.WriteHeader(http.StatusNotFound)
  133. return
  134. }
  135. if r.Method == "POST" {
  136. l.Title = r.FormValue("title")
  137. l.Description = r.FormValue("description")
  138. c.UpdateList(l)
  139. }
  140. b, err := json.MarshalIndent(l, "", " ")
  141. if err != nil {
  142. errs = append(errs, err)
  143. }
  144. t.Execute(w, map[string]interface{}{
  145. "List": l,
  146. "Description": template.HTML(
  147. blackfriday.Run(
  148. []byte(strings.ReplaceAll(l.Description, "\r\n", "\n")),
  149. blackfriday.WithExtensions(blackfriday.CommonExtensions))),
  150. "ListJSON": string(b),
  151. "Errors": errs,
  152. })
  153. })
  154. }