package main import ( "log" "net/http" ) func webServer() { // Page Handlers // Anything that is responsible for the base elements of a viewable web page http.HandleFunc("/", homePageHandler) http.HandleFunc("/status", statusHandler) // Start web server log.Print("Service listening on :8080") log.Printf("Web server unexpectedly exiting!: %v", http.ListenAndServe(":8080", nil)) } type homePageData struct { BudgetID string } func homePageHandler(w http.ResponseWriter, r *http.Request) { go refreshData() pageData := &homePageData{BudgetID: ynabClient.BudgetID} err := t.ExecuteTemplate(w, "home.html", pageData) if err != nil { log.Printf("error executing home.html template: %v", err) } //http.Redirect(w, r, fmt.Sprintf("https://app.ynab.com/%s", ynabClient.BudgetID), http.StatusSeeOther) } // Returns status 200 if a refresh is not running, otherwise waits for refresh to finish // Can be used by clients to tell when a refresh finishes func statusHandler(w http.ResponseWriter, r *http.Request) { refreshRunning.Lock() w.WriteHeader(http.StatusOK) refreshRunning.Unlock() }