From 859ff5be7ade9115e47e7a716ccd18c0fc4ee067 Mon Sep 17 00:00:00 2001 From: adrunkhuman <16039109+adrunkhuman@users.noreply.github.com> Date: Sun, 12 Apr 2026 04:59:15 +0200 Subject: [PATCH] Centralise hardcoded constants into named symbols MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #31 — collects scattered magic values into one place: - internal/site/config.go: BaseURL, ArchiveURL, HTTPTimeout - internal/ui/commands.go: cmdTimeout (20s, stays near command layer) client.go and update.go now reference the named constants instead of inline literals. --- internal/site/client.go | 7 ++----- internal/site/config.go | 12 ++++++++++++ internal/ui/commands.go | 13 ++++++++----- internal/ui/update.go | 6 ++++-- 4 files changed, 26 insertions(+), 12 deletions(-) create mode 100644 internal/site/config.go diff --git a/internal/site/client.go b/internal/site/client.go index 6dd3516..b8a5b39 100644 --- a/internal/site/client.go +++ b/internal/site/client.go @@ -7,26 +7,23 @@ import ( "io" "net/http" "net/url" - "time" "github.com/PuerkitoBio/goquery" "golang.org/x/net/html/charset" ) -const defaultBaseURL = "http://www.90minut.pl" - type Client struct { baseURL *url.URL http *http.Client } func NewClient() *Client { - base, _ := url.Parse(defaultBaseURL) + base, _ := url.Parse(BaseURL) return &Client{ baseURL: base, http: &http.Client{ - Timeout: 15 * time.Second, + Timeout: HTTPTimeout, }, } } diff --git a/internal/site/config.go b/internal/site/config.go new file mode 100644 index 0000000..d24d798 --- /dev/null +++ b/internal/site/config.go @@ -0,0 +1,12 @@ +package site + +import "time" + +const ( + BaseURL = "http://www.90minut.pl" + ArchiveURL = BaseURL + "/archsezon.php" + + // HTTPTimeout caps individual HTTP round-trips; kept short to surface + // connectivity failures quickly without blocking the TUI. + HTTPTimeout = 15 * time.Second +) diff --git a/internal/ui/commands.go b/internal/ui/commands.go index a1f9d17..c7ad566 100644 --- a/internal/ui/commands.go +++ b/internal/ui/commands.go @@ -10,9 +10,12 @@ import ( "github.com/adrunkhuman/90minuTUI/internal/site" ) +// cmdTimeout is generous enough for slow archive pages without blocking the TUI indefinitely. +const cmdTimeout = 20 * time.Second + func (m Model) loadArchiveCmd(url string) tea.Cmd { return func() tea.Msg { - ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), cmdTimeout) defer cancel() seasons, selectedIdx, competitions, err := m.service.LoadArchive(ctx, url) return archiveLoadedMsg{seasons: seasons, selectedIdx: selectedIdx, competitions: competitions, err: err} @@ -21,7 +24,7 @@ func (m Model) loadArchiveCmd(url string) tea.Cmd { func (m Model) loadSeasonCompetitionsCmd(url, seasonKey string) tea.Cmd { return func() tea.Msg { - ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), cmdTimeout) defer cancel() _, _, competitions, err := m.service.LoadArchive(ctx, url) return competitionsLoadedMsg{seasonKey: seasonKey, competitions: competitions, err: err} @@ -30,7 +33,7 @@ func (m Model) loadSeasonCompetitionsCmd(url, seasonKey string) tea.Cmd { func (m Model) loadCompetitionCmd(url, competitionKey string) tea.Cmd { return func() tea.Msg { - ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), cmdTimeout) defer cancel() menu, league, err := m.service.LoadCompetition(ctx, url) return competitionMenuLoadedMsg{competitionKey: competitionKey, menu: menu, league: league, err: err} @@ -39,7 +42,7 @@ func (m Model) loadCompetitionCmd(url, competitionKey string) tea.Cmd { func (m Model) loadLeagueCmd(url, competitionKey string) tea.Cmd { return func() tea.Msg { - ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), cmdTimeout) defer cancel() league, err := m.service.LoadLeague(ctx, url) return leagueLoadedMsg{competitionKey: competitionKey, league: league, err: err} @@ -48,7 +51,7 @@ func (m Model) loadLeagueCmd(url, competitionKey string) tea.Cmd { func (m Model) loadMatchCmd(url, fixtureKey string) tea.Cmd { return func() tea.Msg { - ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), cmdTimeout) defer cancel() match, err := m.service.LoadMatch(ctx, url) return matchLoadedMsg{fixtureKey: fixtureKey, match: match, err: err} diff --git a/internal/ui/update.go b/internal/ui/update.go index fbae1a6..612f16c 100644 --- a/internal/ui/update.go +++ b/internal/ui/update.go @@ -4,10 +4,12 @@ import ( "time" tea "github.com/charmbracelet/bubbletea" + + "github.com/adrunkhuman/90minuTUI/internal/site" ) func (m Model) Init() tea.Cmd { - return m.loadArchiveCmd("http://www.90minut.pl/archsezon.php") + return m.loadArchiveCmd(site.ArchiveURL) } func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { @@ -391,7 +393,7 @@ func (m *Model) handleReload() tea.Cmd { m.match = nil if len(m.seasons) == 0 { - return m.loadArchiveCmd("http://www.90minut.pl/archsezon.php") + return m.loadArchiveCmd(site.ArchiveURL) } if m.selectorActive() && m.focus == focusSeasons {