diff options
| author | Mitch Riedstra <mitch@riedstra.us> | 2021-03-04 18:50:47 -0500 |
|---|---|---|
| committer | Mitch Riedstra <mitch@riedstra.us> | 2021-03-04 18:50:47 -0500 |
| commit | 3b6f5647b0689abf04be73c3cf00297051753435 (patch) | |
| tree | 3de6871abf37849c1e9f86dfc90ee2b3ed74c66e /cmd/web/install.go | |
| parent | eaf02771d767e4745572d9b00e71e138ee030e60 (diff) | |
| download | steam-export-3b6f5647b0689abf04be73c3cf00297051753435.tar.gz steam-export-3b6f5647b0689abf04be73c3cf00297051753435.tar.xz | |
Refactor. Pull most of the functions into methods off of an App struct.
Kill most global variables.
Diffstat (limited to 'cmd/web/install.go')
| -rw-r--r-- | cmd/web/install.go | 89 |
1 files changed, 35 insertions, 54 deletions
diff --git a/cmd/web/install.go b/cmd/web/install.go index fa1ea7a..ce8aa8d 100644 --- a/cmd/web/install.go +++ b/cmd/web/install.go @@ -9,44 +9,25 @@ import ( "os" "strconv" "strings" - "sync" "time" ) -type statusInfo struct { - sync.RWMutex - Running bool - Error error - Url string - Transferred int64 - Size int64 - Start *time.Time -} - -var ( - status = &statusInfo{ - Running: false, - } - - getPath = make(chan string) -) - -func statsHandler(w http.ResponseWriter, r *http.Request) { - status.RLock() - defer status.RUnlock() +func (a *App) HandleStats(w http.ResponseWriter, r *http.Request) { + a.Status.RLock() + defer a.Status.RUnlock() w.Header().Add("Content-type", "application/json") enc := json.NewEncoder(w) - err := enc.Encode(status) + err := enc.Encode(a.Status) if err != nil { - Logger.Println("While encoding status: ", err) + Logger.Println("While encoding Status: ", err) } return } -func installHttp(u string) error { +func (a *App) installHttp(u string) error { Logger.Println("Installer: loading from url") resp, err := http.Get(u) if err != nil { @@ -58,14 +39,14 @@ func installHttp(u string) error { return fmt.Errorf("Failed to convert estimated size header: %w", err) } - status.Lock() - status.Size = estSize - status.Unlock() + a.Status.Lock() + a.Status.Size = estSize + a.Status.Unlock() rdr, wrtr := io.Pipe() go func() { - err = Lib.Extract(rdr) + err = a.Library.Extract(rdr) if err != nil { Logger.Printf("Installer: extracting %s", err) } @@ -74,9 +55,9 @@ func installHttp(u string) error { var total int64 start := time.Now() - status.Lock() - status.Start = &start - status.Unlock() + a.Status.Lock() + a.Status.Start = &start + a.Status.Unlock() for { var n int64 n, err = io.CopyN(wrtr, resp.Body, 100*1024*1024) @@ -94,9 +75,9 @@ func installHttp(u string) error { Logger.Printf("Downloading from %s, Size: %s, %0.1f%% Done, Rate: %.2f mb/s", u, formatBytes(estSize), float64(total)/float64(estSize)*100, rate) - status.Lock() - status.Transferred = total - status.Unlock() + a.Status.Lock() + a.Status.Transferred = total + a.Status.Unlock() } if err == io.EOF { @@ -106,14 +87,14 @@ func installHttp(u string) error { return err } -func installPath(p string) error { +func (a *App) installPath(p string) error { Logger.Println("Installer: loading from filesystem") fh, err := os.Open(p) if err != nil { return fmt.Errorf("Installer: opening %w", err) } - err = Lib.Extract(fh) + err = a.Library.Extract(fh) if err != nil { return fmt.Errorf("Installer: opening %w", err) } @@ -121,34 +102,34 @@ func installPath(p string) error { return nil } -// installer handles installing games either from a local path or a -// remote URL -func installer(urls <-chan string) { +// installer runs in the background installing games either from a local path or +// a remote URL +func (a *App) installer() { var err error - for u := range urls { - status.Lock() + for u := range a.download { + a.Status.Lock() Logger.Printf("Installer: running for URI: %s", u) - status.Running = true - status.Url = u - status.Unlock() + a.Status.Running = true + a.Status.Url = u + a.Status.Unlock() if strings.HasPrefix(u, "http") { - err = installHttp(u) + err = a.installHttp(u) } else { - err = installPath(u) + err = a.installPath(u) } - status.Lock() - status.Running = false - status.Error = err + a.Status.Lock() + a.Status.Running = false + a.Status.Error = err Logger.Printf("Installer: Completed request %s Errors: %s", u, err) - status.Unlock() + a.Status.Unlock() - reloadLib() + a.LibraryReload() } } -func gameInstaller(w http.ResponseWriter, r *http.Request) { +func (a *App) HandleInstall(w http.ResponseWriter, r *http.Request) { if unauthorizedIfNotLocal(w, r) { return } @@ -179,7 +160,7 @@ func gameInstaller(w http.ResponseWriter, r *http.Request) { } Logger.Printf("Installer: Sending request for: %s to channel", uri) - getPath <- uri + a.download <- uri http.Redirect(w, r, "/", 302) } |
