func main() {
	log.Println("Starting sqs processor")

	err := godotenv.Load()
	if err != nil {
		log.Fatal("Error loading .env file")
	}

	c.AWSAccess = os.Getenv("AWS_ACCESS")
	c.AWSSecret = os.Getenv("AWS_SECRET")
	done := make(chan bool)
	messageQueue := make(chan *sqs.Message)

	//notification queue
	s, err := sqs.NewFrom(c.AWSAccess, c.AWSSecret, "us-east-1")
	if err != nil {
		log.Panic(err)
	}
	q, err := s.GetQueue("spotify-ofp-notification")
	if err != nil {
		log.Panic(err)
	}

	go listenOnQueue("spotify-ofp", messageQueue)
	go processQueue(messageQueue)
	go polling(q)
	<-done
}
Beispiel #2
0
func listenOnQueue(queue string, ch chan *sqs.Message) {

	// Setup Queue
	s, err := sqs.NewFrom(c.AWSAccess, c.AWSSecret, "us-east-1")
	if err != nil {
		log.Panic(err)
	}
	q, err := s.GetQueue(queue)
	if err != nil {
		log.Panic(err)
	}

	for {
		resp, err := q.ReceiveMessage(1)
		if err != nil {
			log.Panic(err)
		}

		for _, m := range resp.Messages {
			ch <- &m
			q.DeleteMessage(&m)
		}
	}

}
Beispiel #3
0
func init() {
	var msgBodyTmplFile string
	flag.StringVar(&queueName, "q", "", "Name of queue to fill")
	flag.StringVar(&region, "r", "", `Queue region (e.g., "us-east-1", "usw01")`)
	flag.StringVar(&msgBodyTmpl, "b", defaultMsgTmpl, "Message body template")
	flag.StringVar(&msgBodyTmplFile, "f", "", "Read message body template from file")
	flag.IntVar(&count, "c", defaultCount, "Number of messages to insert")
	flag.BoolVar(&serialMode, "serial", false, "Fill queue non-concurrently")
	flag.Parse()
	auth, err := aws.EnvAuth()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	if queueName == "" || region == "" {
		flag.Usage()
		os.Exit(1)
	}
	if msgBodyTmpl == "" && msgBodyTmplFile == "" {
		flag.Usage()
		os.Exit(1)
	}
	if msgBodyTmplFile != "" {
		body, err := ioutil.ReadFile(msgBodyTmplFile)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		msgBodyTmpl = string(body)
	}
	region = normalizeRegion(region)
	svc, err := sqs.NewFrom(auth.AccessKey, auth.SecretKey, region)
	if err != nil {
		fmt.Println("Error accessing SQS:", err)
		os.Exit(1)
	}
	queue, err = svc.GetQueue(queueName)
	if err != nil {
		fmt.Printf("Error getting queue %s: %s\n", queueName, err)
		os.Exit(1)
	}
}
Beispiel #4
0
func main() {
	log.Println("Starting Spotify Remote API...")

	// Load .env
	err := godotenv.Load()
	if err != nil {
		// Can't load .env, so setenv defaults
		os.Setenv("SQL_HOST", "localhost:8091")
		os.Setenv("SQL_USER", "root")
		os.Setenv("SQL_PASSWORD", "")
		os.Setenv("SQL_DB", "spotify_remote")
	}

	// Setup App Context
	// Setup DB
	db, err := sqlx.Open("sqlite3", "./spotify-remote.db")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()
	context.db = db

	// Setup SQS
	s, err := sqs.NewFrom(os.Getenv("AWS_ACCESS"), os.Getenv("AWS_SECRET"), "us-east-1")
	if err != nil {
		log.Panic(err)
	}

	// Send Queue: API -> Remote
	q, err := s.GetQueue("spotify-ofp")
	if err != nil {
		log.Panic(err)
	}
	context.sqs = q

	// Receive Queue: Remote -> API
	qq, err := s.GetQueue("spotify-ofp-notification")
	if err != nil {
		log.Panic(err)
	}
	context.rsqs = qq

	// Queue Processing Logic
	messages := make(chan *sqs.Message)
	go listenOnQueue(context.rsqs, messages)
	go processQueue(messages)

	// Track Queue
	tq := &TrackQueue{}
	context.tq = tq

	// Now Playing
	context.np = &nowPlaying{}

	router := mux.NewRouter()
	r := router.PathPrefix("/api/v1").Subrouter() // Prepend API Version

	// Setup Negroni
	n := negroni.Classic()

	// Info
	r.HandleFunc("/", GetInfo).Methods("GET")

	// TrackQueue
	r.HandleFunc("/queue/add", PostAddTrack).Methods("POST")
	r.HandleFunc("/queue/list", GetListTracks).Methods("GET")
	r.HandleFunc("/queue/delete", PostDeleteTrack).Methods("POST")
	r.HandleFunc("/queue/next", PostSkipTrack).Methods("POST")
	//r.HandleFunc("/queue/upvote", AddTrack).Methods("POST")
	//r.HandleFunc("/queue/downvote", AddTrack).Methods("POST")

	r.HandleFunc("/search", SearchSpotify).Methods("GET")

	r.HandleFunc("/auth", Auth).Methods("GET")
	r.HandleFunc("/callback", Callback).Methods("GET")

	//r.HandleFunc("/push", QueueTrackRemote).Methods("GET")

	//tq := &TrackQueue{}

	//trackList := tq.list()
	//log.Println("Track Queue: ", trackList)

	//tq.push("song1")
	//tq.push("song2")

	//trackList = tq.list()
	//log.Println("Track Queue: ", trackList)

	//track, _ := tq.pop()
	//log.Println("Track: ", track)

	//trackList = tq.list()
	//log.Println("Track Queue: ", trackList)

	//log.Println("Track Queue Length: ", tq.length())

	// Setup router
	n.UseHandler(r)

	// Start Serve
	if os.Getenv("PORT") != "" {
		n.Run(strings.Join([]string{":", os.Getenv("PORT")}, ""))
	} else {
		n.Run(":8080")
	}

}