From 3b6f5647b0689abf04be73c3cf00297051753435 Mon Sep 17 00:00:00 2001 From: Mitch Riedstra Date: Thu, 4 Mar 2021 18:50:47 -0500 Subject: Refactor. Pull most of the functions into methods off of an App struct. Kill most global variables. --- cmd/web/index.go | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) (limited to 'cmd/web/index.go') diff --git a/cmd/web/index.go b/cmd/web/index.go index b90faf8..7291af0 100644 --- a/cmd/web/index.go +++ b/cmd/web/index.go @@ -1,31 +1,22 @@ package main import ( - "html/template" "net/http" - _ "embed" "riedstra.dev/mitch/steam-export/steam" ) -var ( - //go:embed templates/index.html - indexTemplate string - - Templ = template.Must(template.New("index").Parse(indexTemplate)) -) - -func index(w http.ResponseWriter, r *http.Request) { +func (a *App) HandleIndex(w http.ResponseWriter, r *http.Request) { // During rendering of the template I believe it's // mutating during the sort of keys, so Lib no longer // is an RWMutex and we're just locking this as if // we're writing to it - Lib.Lock() - defer Lib.Unlock() - status.Lock() - defer status.Unlock() + a.Library.Lock() + defer a.Library.Unlock() + a.Status.Lock() + defer a.Status.Unlock() - err := Templ.ExecuteTemplate(w, "index", + err := a.Templates.ExecuteTemplate(w, "index", struct { Lib *steam.Library Info *statusInfo @@ -34,8 +25,8 @@ func index(w http.ResponseWriter, r *http.Request) { Port string Version string }{ - &Lib.Library, - status, + &a.Library.Library, + a.Status, isLocal(r.RemoteAddr), getHostIP(), getPort(), -- cgit v1.2.3