<commit_before>package backend

import (
	"encoding/json"
	"errors"
	"github.com/mrshankly/go-twitch/twitch"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"time"
)

func (c *Controller) Loop() {
	log.Print("Start Loop")

	timer := time.NewTimer(time.Minute).C

	go loopViewers(c.twitchAPI, c.cViewer, c.infosViewer)
	go loopChat(c.cChat, c.infosChat)
	go loopStat(c.cStat, c.storage.db)

	for {
		select {
		case temp, ok := <-c.infosViewer:
			if !ok {
				log.Println("InfosViewer failed")
				return
			}
			storeViewerCount(c.storage.views, temp)

		case temp, ok := <-c.infosChat:
			if !ok {
				log.Println("InfosChat failed")
				return
			}
			storeChatEntry(c.storage.chat, temp)
		case <-timer:
			c.saveFollowed()
		default:
		}
	}
	log.Println("Loop failed")
}

func SetupController() (contr *Controller) {
	store := StorageController{
		db: setupStorage("twitch"),
	}
	store.views = store.db.C("viewer_count")
	store.chat = store.db.C("chat_entries")

	contr = &Controller{
		config:      LoadConfig("config.json"),
		infosChat:   make(chan ChatEntry),
		infosViewer: make(chan ViewerCount),
		cViewer:     make(chan Message),
		cChat:       make(chan Message),
		cStat:       make(chan Message),
		tracked:     make(map[string]bool),
		storage:     store,
		twitchAPI:   twitch.NewClient(&http.Client{}),
	}

	os.Setenv("GO-TWITCH_CLIENTID", contr.config.ClientID)
	return<commit_msg>Avoid infinite loop in backend<commit_after>package backend

import (
	"encoding/json"
	"errors"
	"github.com/mrshankly/go-twitch/twitch"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"time"
)

func (c *Controller) Loop() {
	log.Print("Start Loop")

	timer := time.NewTimer(time.Minute).C

	go loopViewers(c.twitchAPI, c.cViewer, c.infosViewer)
	go loopChat(c.cChat, c.infosChat)
	go loopStat(c.cStat, c.storage.db)

	for {
		select {
		case temp, ok := <-c.infosViewer:
			if !ok {
				log.Println("InfosViewer failed")
				return
			}
			storeViewerCount(c.storage.views, temp)

		case temp, ok := <-c.infosChat:
			if !ok {
				log.Println("InfosChat failed")
				return
			}
			storeChatEntry(c.storage.chat, temp)
		case <-timer:
			c.saveFollowed()
		}
	}
	log.Println("Loop failed")
}

func SetupController() (contr *Controller) {
	store := StorageController{
		db: setupStorage("twitch"),
	}
	store.views = store.db.C("viewer_count")
	store.chat = store.db.C("chat_entries")

	contr = &Controller{
		config:      LoadConfig("config.json"),
		infosChat:   make(chan ChatEntry),
		infosViewer: make(chan ViewerCount),
		cViewer:     make(chan Message),
		cChat:       make(chan Message),
		cStat:       make(chan Message),
		tracked:     make(map[string]bool),
		storage:     store,
		twitchAPI:   twitch.NewClient(&http.Client{}),
	}

	os.Setenv("GO-TWITCH_CLIENTID", contr.config.ClientID)
	return
{'fraction_non_alphanumeric': 0.15089605734767025, 'fraction_numerical': 0.0, 'mean_word_length': 3.620860927152318, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6234547', 'n_tokens_mistral': 1185, 'n_tokens_neox': 1077, 'n_words': 200}