123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- package main
-
- import (
- "github.com/lamperi/e4bot/spotify"
- "html/template"
- "log"
- "net/http"
- "strings"
- )
-
- var (
- cachedTemplates = template.Must(template.ParseFiles("web/index.html"))
- )
-
- func (s *WebService) IndexHandler(w http.ResponseWriter, r *http.Request) {
- if r.URL.Path != "/" {
- http.Error(w, "Not Found", http.StatusNotFound)
- return
- }
-
- data := struct {
- Username string
- Songs []*Song
- }{
- "",
- make([]*Song, 0),
- }
-
- session, err := getSession(r)
- if session != nil {
- songs, err := s.db.FindAllEntries(session.username)
- if err != nil {
- log.Println("Error while reading entries from database", err)
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- data.Songs = songs
- data.Username = session.username
- }
-
- var templates = cachedTemplates
- if s.noCache {
- templates = template.Must(template.ParseFiles("web/index.html"))
- }
-
- err = templates.ExecuteTemplate(w, "index.html", data)
- if err != nil {
- log.Println("Error while rendering template", err)
- 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 (s *WebService) 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
- }
- round := r.Form.Get("round")
- artist := r.Form.Get("artist")
- title := r.Form.Get("title")
- url := r.Form.Get("url")
-
- if artist == "" && title == "" && url != "" {
- log.Println("Resolving Spotify URL")
- trackID := getTrackID(url)
- err := s.spotifyClient.Authenticate()
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- track, err := s.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
- }
-
- updated, err := s.db.UpdateEntry(session.username, round, artist, title, url)
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- if !updated {
- http.Error(w, "No round in DB", http.StatusNotFound)
- return
- }
-
- log.Printf("Updated song for round %s, artist: %s, title: %s, URL: %s",
- round, artist, title, url)
-
- http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
- }
-
- func (s *WebService) 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(s.db, 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)
- }
-
- type WebService struct {
- db *DB
- spotifyClient *spotify.SpotifyClient
- noCache bool
- }
-
- func webStart(listenAddr string, db *DB, spotifyClient *spotify.SpotifyClient) {
- service := &WebService{db, spotifyClient, true}
-
- 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("/", service.IndexHandler)
- mux.HandleFunc("/update", service.UpdateHandler)
- mux.HandleFunc("/login", service.LoginHandler)
-
- http.ListenAndServe(listenAddr, mux)
- }
|