package main import ( "github.com/lamperi/e4bot/spotify" "html/template" "log" "net/http" "strconv" "strings" ) var ( cachedTemplates = template.Must(template.ParseFiles("web/index.html")) songsChan chan *SongEntry spotifyClient *spotify.SpotifyClient ) func indexHandler(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" { http.Error(w, "Not Found", http.StatusNotFound) return } alsoEmptySongs := SongsFile{ Songs: make([]*SongEntry, 52), } for week := 1; week <= 52; week++ { alsoEmptySongs.Songs[week-1] = &SongEntry{Week: week} } username := "" session, err := getSession(r) if session != nil { songs, err := loadDb() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } for _, song := range songs.Songs { alsoEmptySongs.Songs[song.Week-1] = song } username = session.username } var templates = cachedTemplates if true { templates = template.Must(template.ParseFiles("web/index.html")) } data := struct { Username string Songs []*SongEntry }{ username, alsoEmptySongs.Songs, } err = templates.ExecuteTemplate(w, "index.html", data) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } func getTrackID(url string) string { const externalURLPrefix = "https://open.spotify.com/track/" const trackURIPrefix = "spotify:track:" if strings.HasPrefix(url, externalURLPrefix) { return strings.Split(url, externalURLPrefix)[1] } else if strings.HasPrefix(url, trackURIPrefix) { return strings.Split(url, trackURIPrefix)[1] } return url } func updateHandler(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/update" { http.Error(w, "Forbidden", http.StatusForbidden) return } session, err := getSession(r) if session == nil { http.Redirect(w, r, "/", http.StatusTemporaryRedirect) return } err = r.ParseForm() if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } week, err := strconv.Atoi(r.Form.Get("week")) if err != nil { http.Error(w, "week parameter not provided", http.StatusBadRequest) return } artist := r.Form.Get("artist") title := r.Form.Get("title") url := r.Form.Get("url") sync := r.Form.Get("sync") if artist == "" && title == "" && url != "" { log.Println("Resolving Spotify URL") trackID := getTrackID(url) err := spotifyClient.Authenticate() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } track, err := spotifyClient.GetTrack(trackID) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } log.Printf("Found Track with name %v and artists %v\n", track.Name, track.Artists) for index, trackArtist := range track.Artists { if index == 0 { artist = trackArtist.Name } else if index == 1 { artist = artist + " feat. " + trackArtist.Name } else { artist = artist + ", " + trackArtist.Name } } title = track.Name url = track.ExternalUrls.Spotify } songs, err := loadDb() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } matched := false for _, song := range songs.Songs { if song.Week == week { song.Artist = artist song.Title = title song.URL = url if song.Sync && sync != "on" { song.Sync = false } songsChan <- song matched = true } } if !matched { song := &SongEntry{ Artist: artist, Title: title, URL: url, Week: week, } songs.Songs = append(songs.Songs, song) songsChan <- song } err = saveDb(songs) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } http.Redirect(w, r, "/", http.StatusTemporaryRedirect) } func loginHandler(w http.ResponseWriter, r *http.Request) { err := r.ParseForm() if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } username := r.Form.Get("username") password := r.Form.Get("password") remember := r.Form.Get("remember") longer := remember == "remember-me" cookie, err := tryLogin(username, password, longer) if err != nil { log.Println("Error while trying to login", err.Error()) http.Redirect(w, r, "/", http.StatusTemporaryRedirect) return } http.SetCookie(w, &cookie) http.Redirect(w, r, "/", http.StatusTemporaryRedirect) } func webStart(listenAddr string, modifiedSongChan chan *SongEntry, spot *spotify.SpotifyClient) { songsChan = modifiedSongChan spotifyClient = spot mux := http.NewServeMux() fs := http.FileServer(http.Dir("web")) mux.Handle("/css/", fs) mux.Handle("/fonts/", fs) mux.Handle("/js/", fs) mux.Handle("/favicon.ico", fs) mux.HandleFunc("/", indexHandler) mux.HandleFunc("/update", updateHandler) mux.HandleFunc("/login", loginHandler) http.ListenAndServe(listenAddr, mux) }