web.go 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package main
  2. import (
  3. "html/template"
  4. "net/http"
  5. "strconv"
  6. )
  7. var (
  8. cachedTemplates = template.Must(template.ParseFiles("web/index.html"))
  9. songsChan chan *SongEntry
  10. )
  11. func indexHandler(w http.ResponseWriter, r *http.Request) {
  12. if r.URL.Path != "/" {
  13. http.Error(w, "Not Found", http.StatusNotFound)
  14. return
  15. }
  16. songs, err := loadDb()
  17. if err != nil {
  18. http.Error(w, err.Error(), http.StatusInternalServerError)
  19. return
  20. }
  21. alsoEmptySongs := SongsFile{
  22. Songs: make([]*SongEntry, 52),
  23. }
  24. for week := 1; week <= 52; week++ {
  25. alsoEmptySongs.Songs[week-1] = &SongEntry{Week: week}
  26. }
  27. for _, song := range songs.Songs {
  28. alsoEmptySongs.Songs[song.Week-1] = song
  29. }
  30. var templates = cachedTemplates
  31. if true {
  32. templates = template.Must(template.ParseFiles("web/index.html"))
  33. }
  34. err = templates.ExecuteTemplate(w, "index.html", alsoEmptySongs)
  35. if err != nil {
  36. http.Error(w, err.Error(), http.StatusInternalServerError)
  37. }
  38. }
  39. func updateHandler(w http.ResponseWriter, r *http.Request) {
  40. if r.URL.Path != "/update" {
  41. http.Error(w, "Forbidden", http.StatusForbidden)
  42. return
  43. }
  44. err := r.ParseForm()
  45. if err != nil {
  46. http.Error(w, err.Error(), http.StatusBadRequest)
  47. return
  48. }
  49. week, err := strconv.Atoi(r.Form.Get("week"))
  50. if err != nil {
  51. http.Error(w, "week parameter not provided", http.StatusBadRequest)
  52. return
  53. }
  54. artist := r.Form.Get("artist")
  55. title := r.Form.Get("title")
  56. url := r.Form.Get("url")
  57. sync := r.Form.Get("sync")
  58. songs, err := loadDb()
  59. if err != nil {
  60. http.Error(w, err.Error(), http.StatusInternalServerError)
  61. return
  62. }
  63. matched := false
  64. for _, song := range songs.Songs {
  65. if song.Week == week {
  66. song.Artist = artist
  67. song.Title = title
  68. song.URL = url
  69. if song.Sync && sync != "on" {
  70. song.Sync = false
  71. }
  72. songsChan <- song
  73. matched = true
  74. }
  75. }
  76. if !matched {
  77. song := &SongEntry{
  78. Artist: artist,
  79. Title: title,
  80. URL: url,
  81. Week: week,
  82. }
  83. songs.Songs = append(songs.Songs, song)
  84. songsChan <- song
  85. }
  86. err = saveDb(songs)
  87. if err != nil {
  88. http.Error(w, err.Error(), http.StatusInternalServerError)
  89. return
  90. }
  91. http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
  92. }
  93. func webStart(modifiedSongChan chan *SongEntry) {
  94. songsChan = modifiedSongChan
  95. fs := http.FileServer(http.Dir("web"))
  96. http.Handle("/css/", fs)
  97. http.Handle("/fonts/", fs)
  98. http.Handle("/js/", fs)
  99. http.Handle("/favicon.ico", fs)
  100. http.HandleFunc("/", indexHandler)
  101. http.HandleFunc("/update", updateHandler)
  102. http.ListenAndServe("localhost:8080", nil)
  103. }