Пример #1
0
// Logger wraps a handler with basic HTTP logging
func Logger(
	globalContext *context.GlobalContext,
) func(http.Handler) http.Handler {
	return func(next http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			localContext := context.Get(r)
			localContext.Logger = logger.New()

			t0 := time.Now()
			localContext.Logger.WriteString("====\n")
			localContext.Logger.Printf(
				"[%s] %s %s",
				r.Method,
				r.RemoteAddr,
				r.URL.String(),
			)

			next.ServeHTTP(w, r)

			localContext.Logger.Printf("FINISHED IN %v", time.Now().Sub(t0))

			loggerMutex.Lock()
			_, err := localContext.Logger.WriteTo(globalContext.LogOut)
			loggerMutex.Unlock()

			if err != nil {
				panic(err)
			}
		})
	}
}
Пример #2
0
// mixPlaylists performs the mix operation triggered by the Submit
// handler.
func mixPlaylists(
	globalContext *context.GlobalContext,
	localContext *context.LocalContext,
	userID string,
	data submissionData,
) {
	t0 := time.Now()
	log := logger.New()

	defer func(log *logger.Logger) {
		if err := recover(); err != nil {
			log.Printf("====\nPANIC WHILE MIXING: %V", err)
			log.WriteTo(globalContext.LogOut)
		}
	}(log)

	sourceListIDs := []string{}
	for _, list := range data.SourceLists {
		sourceListIDs = append(sourceListIDs, list.ID)
	}

	log.WriteString("====\n")
	log.Printf(
		"MIXING [%s] INTO %s",
		strings.Join(sourceListIDs, ", "),
		data.DestList.ID,
	)
	log.Printf("ROUND ROBIN: %t", data.Options.RoundRobin)
	log.Printf("SHUFFLE:     %t", data.Options.Shuffle)
	log.Printf("DEDUP:       %t", data.Options.Dedup)
	log.Printf("PAD:         %t", data.Options.Pad)

	sourceTrackIDs := [][]string{}
	for _, list := range data.SourceLists {
		trackIDs, err := spotify.GetPlaylistTrackIDs(
			localContext.AuthTokens,
			list.OwnerID,
			list.ID,
		)
		if err != nil {
			panic(err)
		}

		sourceTrackIDs = append(sourceTrackIDs, trackIDs)
	}

	combinedTrackIDs := combineSourceTracks(sourceTrackIDs, data.Options)

	err := spotify.WritePlaylist(
		localContext.AuthTokens,
		data.DestList.OwnerID,
		data.DestList.ID,
		combinedTrackIDs,
	)
	if err != nil {
		panic(err)
	}

	log.Printf("FINISHED IN %v", time.Now().Sub(t0))
	loggerMutex.Lock()
	_, err = log.WriteTo(globalContext.LogOut)
	loggerMutex.Unlock()

	if err != nil {
		panic(err)
	}
}