web.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. alsoEmptySongs := SongsFile{
  17. Songs: make([]*SongEntry, 52),
  18. }
  19. for week := 1; week <= 52; week++ {
  20. alsoEmptySongs.Songs[week-1] = &SongEntry{Week: week}
  21. }
  22. username := ""
  23. session, err := getSession(r)
  24. if session != nil {
  25. songs, err := loadDb()
  26. if err != nil {
  27. http.Error(w, err.Error(), http.StatusInternalServerError)
  28. return
  29. }
  30. for _, song := range songs.Songs {
  31. alsoEmptySongs.Songs[song.Week-1] = song
  32. }
  33. username = session.username
  34. }
  35. var templates = cachedTemplates
  36. if true {
  37. templates = template.Must(template.ParseFiles("web/index.html"))
  38. }
  39. data := struct {
  40. Username string
  41. Songs []*SongEntry
  42. }{
  43. username,
  44. alsoEmptySongs.Songs,
  45. }
  46. err = templates.ExecuteTemplate(w, "index.html", data)
  47. if err != nil {
  48. http.Error(w, err.Error(), http.StatusInternalServerError)
  49. }
  50. }
  51. func updateHandler(w http.ResponseWriter, r *http.Request) {
  52. if r.URL.Path != "/update" {
  53. http.Error(w, "Forbidden", http.StatusForbidden)
  54. return
  55. }
  56. err := r.ParseForm()
  57. if err != nil {
  58. http.Error(w, err.Error(), http.StatusBadRequest)
  59. return
  60. }
  61. week, err := strconv.Atoi(r.Form.Get("week"))
  62. if err != nil {
  63. http.Error(w, "week parameter not provided", http.StatusBadRequest)
  64. return
  65. }
  66. artist := r.Form.Get("artist")
  67. title := r.Form.Get("title")
  68. url := r.Form.Get("url")
  69. sync := r.Form.Get("sync")
  70. songs, err := loadDb()
  71. if err != nil {
  72. http.Error(w, err.Error(), http.StatusInternalServerError)
  73. return
  74. }
  75. matched := false
  76. for _, song := range songs.Songs {
  77. if song.Week == week {
  78. song.Artist = artist
  79. song.Title = title
  80. song.URL = url
  81. if song.Sync && sync != "on" {
  82. song.Sync = false
  83. }
  84. songsChan <- song
  85. matched = true
  86. }
  87. }
  88. if !matched {
  89. song := &SongEntry{
  90. Artist: artist,
  91. Title: title,
  92. URL: url,
  93. Week: week,
  94. }
  95. songs.Songs = append(songs.Songs, song)
  96. songsChan <- song
  97. }
  98. err = saveDb(songs)
  99. if err != nil {
  100. http.Error(w, err.Error(), http.StatusInternalServerError)
  101. return
  102. }
  103. http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
  104. }
  105. func loginHandler(w http.ResponseWriter, r *http.Request) {
  106. err := r.ParseForm()
  107. if err != nil {
  108. http.Error(w, err.Error(), http.StatusBadRequest)
  109. return
  110. }
  111. username := r.Form.Get("username")
  112. password := r.Form.Get("password")
  113. remember := r.Form.Get("remember")
  114. longer := remember == "remember-me"
  115. cookie, err := tryLogin(username, password, longer)
  116. if err != nil {
  117. http.Error(w, err.Error(), http.StatusForbidden)
  118. return
  119. }
  120. http.SetCookie(w, &cookie)
  121. http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
  122. }
  123. func webStart(modifiedSongChan chan *SongEntry) {
  124. songsChan = modifiedSongChan
  125. mux := http.NewServeMux()
  126. fs := http.FileServer(http.Dir("web"))
  127. mux.Handle("/css/", fs)
  128. mux.Handle("/fonts/", fs)
  129. mux.Handle("/js/", fs)
  130. mux.Handle("/favicon.ico", fs)
  131. mux.HandleFunc("/", indexHandler)
  132. mux.HandleFunc("/update", updateHandler)
  133. mux.HandleFunc("/login", loginHandler)
  134. http.ListenAndServe("localhost:8080", mux)
  135. }