aboutsummaryrefslogtreecommitdiff
path: root/cmd/web/handlers.go
diff options
context:
space:
mode:
authorMitch Riedstra <mitch@riedstra.us>2021-08-04 23:53:36 -0400
committerMitch Riedstra <mitch@riedstra.us>2021-08-04 23:53:36 -0400
commitc202f2eca32e1ab2e313417168351df1c58ee062 (patch)
tree6540629b337d2d769581baec26096ac0555f71f9 /cmd/web/handlers.go
parent742938b00222c7ad57ad11eb24850d9202c2503d (diff)
downloadsteam-export-c202f2eca32e1ab2e313417168351df1c58ee062.tar.gz
steam-export-c202f2eca32e1ab2e313417168351df1c58ee062.tar.xz
More major changes. Web UI works. Downloading games works. Status works. extractFile needs work
Diffstat (limited to 'cmd/web/handlers.go')
-rw-r--r--cmd/web/handlers.go102
1 files changed, 34 insertions, 68 deletions
diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go
index 4b86b58..fca7471 100644
--- a/cmd/web/handlers.go
+++ b/cmd/web/handlers.go
@@ -3,7 +3,7 @@ package main
import (
"encoding/json"
"fmt"
- "io"
+ "html/template"
"net/http"
"net/url"
"os"
@@ -11,36 +11,26 @@ import (
"time"
"github.com/gorilla/mux"
- "riedstra.dev/mitch/steam-export/steam"
)
// HandleIndex takes care of rendering our embedded template
// and locks the steam library for each 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
- a.Library.Lock()
- defer a.Library.Unlock()
- a.Status.Lock()
- defer a.Status.Unlock()
-
- err := a.Templates.ExecuteTemplate(w, "index",
+ t, err := template.ParseFS(a.templateFS, "templates/index.html")
+ if err != nil {
+ Logger.Printf("While parsing template for index: %s", err)
+ http.Error(w, "Internal server error ( template )",
+ http.StatusInternalServerError)
+ return
+ }
+
+ err = t.Execute(w,
struct {
- Lib *steam.Library
- Info *statusInfo
- Local bool
- ShareLink string
- Version string
- Demo bool
+ App *App
+ Local bool
}{
- &a.Library.Library,
- a.Status,
+ a,
isLocal(r.RemoteAddr),
- getShareLink(),
- Version,
- a.Demo,
})
if err != nil {
Logger.Printf("While Rendering template: %s", err)
@@ -60,6 +50,7 @@ func (a *App) HandleInstall(w http.ResponseWriter, r *http.Request) {
uri := r.Form.Get("uri")
+ // Sanity checking on our end before we pass it off
if strings.HasPrefix(uri, "http") {
_, err := url.Parse(uri)
if err != nil {
@@ -77,7 +68,14 @@ func (a *App) HandleInstall(w http.ResponseWriter, r *http.Request) {
}
Logger.Printf("Installer: Sending request for: %s to channel", uri)
- a.download <- uri
+
+ go func() {
+ g, err := a.Library.ExtractSmart(uri)
+ if err != nil {
+ Logger.Printf("Error encountered installing: %s", err)
+ }
+ Logger.Printf("Extrated game: %s", g)
+ }()
http.Redirect(w, r, "/", 302)
}
@@ -87,9 +85,7 @@ func (a *App) HandleDownload(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
game := vars["game"]
- a.Library.Lock()
- g, ok := a.Library.Games[game]
- a.Library.Unlock()
+ g, ok := a.Library.Games()[game]
if !ok {
Logger.Printf("Missing: %s", game)
http.Error(w, "Game is missing", 404)
@@ -101,37 +97,11 @@ func (a *App) HandleDownload(w http.ResponseWriter, r *http.Request) {
Logger.Printf("Client %s is downloading: %s", r.RemoteAddr, game)
- // Invert the writer so we can break up the copy and get progress
- // information in here
- rdr, pwrtr := io.Pipe()
- go func() {
- err := g.Package(pwrtr)
- if err != nil {
- Logger.Println("Error in package writing: ", err)
- }
- }()
-
- var total int64
- start := time.Now()
- for {
- n, err := io.CopyN(w, rdr, 256*1024*1024)
- if err == io.EOF {
- break
- }
- if err != nil {
- Logger.Printf("Client %s Error Sending game: %s", r.RemoteAddr, err)
- // Headers already sent, don't bother sending an error
- return
- }
- total += n
- mb := float64(total / 1024 / 1024)
- rate := mb / time.Since(start).Seconds()
-
- Logger.Printf("Client %s is downloading %s: %0.1f%% done %.2f mb/s",
- r.RemoteAddr, game, float64(total)/float64(g.Size)*100, rate)
+ err := a.Library.Package(g.Name, w)
+ if err != nil {
+ Logger.Printf("Encountered download error: %s", err)
}
-
- Logger.Printf("Client %s finished downloading: %s", r.RemoteAddr, game)
+ return
}
// HandleDelete removes the game in question, though it doesn't
@@ -153,16 +123,14 @@ func (a *App) HandleDelete(w http.ResponseWriter, r *http.Request) {
return
}
- a.Library.Lock()
- g, ok := a.Library.Games[game]
- a.Library.Unlock()
+ g, ok := a.Library.Games()[game]
if !ok {
Logger.Printf("Missing: %s", game)
http.Error(w, "Game is missing", 404)
return
}
- err = g.Delete()
+ err = a.Library.Delete(g.Name)
if err != nil {
Logger.Printf("Error removing game: %s", err)
http.Error(w, fmt.Sprintf("Error removing game: %s", err), 500)
@@ -170,21 +138,19 @@ func (a *App) HandleDelete(w http.ResponseWriter, r *http.Request) {
}
Logger.Printf("Removed game: %s", game)
- a.LibraryReload()
http.Redirect(w, r, "/", 302)
}
// HandleStats dumps out some internal statistics of installation which
// is then parsed by some JS for a progress bar and such
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(a.Status)
+ enc.SetIndent("", " ")
+
+ err := enc.Encode(a.Library.Status())
if err != nil {
Logger.Println("While encoding Status: ", err)
}
@@ -200,7 +166,7 @@ func (a *App) HandleSetLib(w http.ResponseWriter, r *http.Request) {
return
}
- a.LibrarySet(r.Form.Get("path"))
+ a.Library.ProcessLibrary(r.Form.Get("path"))
http.Redirect(w, r, "/", 302)
}
@@ -211,7 +177,7 @@ func HandleQuit(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-type", "text/plain")
w.Write([]byte("Shutting down... feel free to close this"))
go func() {
- time.Sleep(time.Second * 2)
+ time.Sleep(time.Millisecond * 50)
os.Exit(0)
}()
return