ynab-portfolio-monitor/webServer.go

45 lines
1.2 KiB
Go
Raw Normal View History

2023-11-13 21:07:57 +00:00
package main
import (
"log"
"net/http"
)
func webServer() {
//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)
http.HandleFunc("/status", statusHandler)
2023-11-13 21:07:57 +00:00
// Start web server
2023-11-13 21:07:57 +00:00
log.Print("Service listening on :8080")
log.Printf("Web server unexpectedly exiting!: %v", http.ListenAndServe(":8080", nil))
}
2023-11-13 21:07:57 +00:00
type homePageData struct {
BudgetID string
2023-11-13 21:07:57 +00:00
}
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)
}
2024-02-17 16:51:20 +00:00
// 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()
2023-11-13 21:07:57 +00:00
}