aboutsummaryrefslogtreecommitdiff
path: root/cmd/web/install.go
diff options
context:
space:
mode:
authorMitchell <mitch@riedstra.dev>2021-01-12 20:53:02 -0500
committerMitchell <mitch@riedstra.dev>2021-01-12 20:53:02 -0500
commit9b04f4ca1c2fe470a562be4b075d48d1c18962da (patch)
tree7cb3b465eec5bde3de88d826880d2cc625eff005 /cmd/web/install.go
parentfde64077cdd85f7a7b989fef320bf7fa3826a01d (diff)
downloadsteam-export-9b04f4ca1c2fe470a562be4b075d48d1c18962da.tar.gz
steam-export-9b04f4ca1c2fe470a562be4b075d48d1c18962da.tar.xz
Remove the rwmutex from the steam library as the template may be mutating the map causing issues. Embed the mutexes.
Diffstat (limited to 'cmd/web/install.go')
-rw-r--r--cmd/web/install.go59
1 files changed, 25 insertions, 34 deletions
diff --git a/cmd/web/install.go b/cmd/web/install.go
index 8cf8b66..fa1ea7a 100644
--- a/cmd/web/install.go
+++ b/cmd/web/install.go
@@ -14,6 +14,7 @@ import (
)
type statusInfo struct {
+ sync.RWMutex
Running bool
Error error
Url string
@@ -23,26 +24,22 @@ type statusInfo struct {
}
var (
- status = struct {
- m *sync.RWMutex
- s *statusInfo
- }{
- m: &sync.RWMutex{},
- s: &statusInfo{Running: false},
+ status = &statusInfo{
+ Running: false,
}
getPath = make(chan string)
)
func statsHandler(w http.ResponseWriter, r *http.Request) {
- status.m.RLock()
- defer status.m.RUnlock()
+ status.RLock()
+ defer status.RUnlock()
w.Header().Add("Content-type", "application/json")
enc := json.NewEncoder(w)
- err := enc.Encode(status.s)
+ err := enc.Encode(status)
if err != nil {
Logger.Println("While encoding status: ", err)
}
@@ -61,9 +58,9 @@ func installHttp(u string) error {
return fmt.Errorf("Failed to convert estimated size header: %w", err)
}
- status.m.Lock()
- status.s.Size = estSize
- status.m.Unlock()
+ status.Lock()
+ status.Size = estSize
+ status.Unlock()
rdr, wrtr := io.Pipe()
@@ -77,9 +74,9 @@ func installHttp(u string) error {
var total int64
start := time.Now()
- status.m.Lock()
- status.s.Start = &start
- status.m.Unlock()
+ status.Lock()
+ status.Start = &start
+ status.Unlock()
for {
var n int64
n, err = io.CopyN(wrtr, resp.Body, 100*1024*1024)
@@ -97,9 +94,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.m.Lock()
- status.s.Transferred = total
- status.m.Unlock()
+ status.Lock()
+ status.Transferred = total
+ status.Unlock()
}
if err == io.EOF {
@@ -129,11 +126,11 @@ func installPath(p string) error {
func installer(urls <-chan string) {
var err error
for u := range urls {
- status.m.Lock()
+ status.Lock()
Logger.Printf("Installer: running for URI: %s", u)
- status.s.Running = true
- status.s.Url = u
- status.m.Unlock()
+ status.Running = true
+ status.Url = u
+ status.Unlock()
if strings.HasPrefix(u, "http") {
err = installHttp(u)
@@ -141,11 +138,11 @@ func installer(urls <-chan string) {
err = installPath(u)
}
- status.m.Lock()
- status.s.Running = false
- status.s.Error = err
+ status.Lock()
+ status.Running = false
+ status.Error = err
Logger.Printf("Installer: Completed request %s Errors: %s", u, err)
- status.m.Unlock()
+ status.Unlock()
reloadLib()
}
@@ -174,14 +171,8 @@ func gameInstaller(w http.ResponseWriter, r *http.Request) {
}
} else {
fi, err := os.Stat(uri)
- if err != nil {
- Logger.Printf("Installer: While parsing url: %s", err)
- http.Error(w, fmt.Sprintf("Invalid uri/path: %s", err), 400)
- return
- }
-
- if !fi.Mode().IsRegular() {
- Logger.Printf("Installer: While parsing url: %s", err)
+ if err != nil || !fi.Mode().IsRegular() {
+ Logger.Printf("Installer: While parsing url/path: %s", err)
http.Error(w, fmt.Sprintf("Invalid uri/path: %s", err), 400)
return
}