From a95df7c42bfd87653fbb47a37d3a5352d6baedf1 Mon Sep 17 00:00:00 2001 From: Steven Polley Date: Mon, 13 Nov 2023 14:07:57 -0700 Subject: [PATCH] add webserver for refresh hook --- main.go | 45 +++++++++++++++++++++++++-------------------- webServer.go | 27 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 20 deletions(-) create mode 100644 webServer.go diff --git a/main.go b/main.go index 6e4ea7c..0193cae 100644 --- a/main.go +++ b/main.go @@ -50,28 +50,33 @@ func init() { } } -// Main program loop func main() { - for { - // Loop through each configured account provider and attempt to get the account balances, and update YNAB - for _, p := range configuredProviders { - balances, accountIDs, err := p.GetBalances() - if err != nil { - log.Printf("failed to get balances with provider '%s': %v", p.Name(), err) - continue - } - if len(balances) != len(accountIDs) { - log.Printf("'%s' provider data validation error: mismatched balance and accountID slice lengths - expected the same: balances length = %d, accountIDs length = %d", p.Name(), len(balances), len(accountIDs)) - continue - } - for i := range balances { - err = ynabClient.SetAccountBalance(accountIDs[i], balances[i]) - if err != nil { - log.Printf("failed to update ynab account '%s' balance: %v", accountIDs[i], err) - } - } - } + go webServer() + + for { // Main program loop + refreshData() log.Print("Sleeping for 6 hours...") time.Sleep(time.Hour * 6) } } + +func refreshData() { + // Loop through each configured account provider and attempt to get the account balances, and update YNAB + for _, p := range configuredProviders { + balances, accountIDs, err := p.GetBalances() + if err != nil { + log.Printf("failed to get balances with provider '%s': %v", p.Name(), err) + continue + } + if len(balances) != len(accountIDs) { + log.Printf("'%s' provider data validation error: mismatched balance and accountID slice lengths - expected the same: balances length = %d, accountIDs length = %d", p.Name(), len(balances), len(accountIDs)) + continue + } + for i := range balances { + err = ynabClient.SetAccountBalance(accountIDs[i], balances[i]) + if err != nil { + log.Printf("failed to update ynab account '%s' balance: %v", accountIDs[i], err) + } + } + } +} diff --git a/webServer.go b/webServer.go new file mode 100644 index 0000000..01c1bac --- /dev/null +++ b/webServer.go @@ -0,0 +1,27 @@ +package main + +import ( + "fmt" + "log" + "net/http" +) + +func webServer() { + // Start web server + + //Public static files + http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("public")))) + + // Page Handlers + // Anything that is responsible for the base elements of a viewable web page + http.HandleFunc("/", homePageHandler) + + log.Print("Service listening on :8080") + log.Printf("Web server unexpectedly exiting!: %v", http.ListenAndServe(":8080", nil)) + +} + +func homePageHandler(w http.ResponseWriter, r *http.Request) { + refreshData() + http.Redirect(w, r, fmt.Sprintf("https://app.ynab.com/%s", ynabClient.BudgetID), http.StatusSeeOther) +}