aboutsummaryrefslogtreecommitdiff
path: root/cmd/web/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/web/main.go')
-rw-r--r--cmd/web/main.go125
1 files changed, 70 insertions, 55 deletions
diff --git a/cmd/web/main.go b/cmd/web/main.go
index 091d22f..f0c7489 100644
--- a/cmd/web/main.go
+++ b/cmd/web/main.go
@@ -1,9 +1,10 @@
package main
import (
+ "embed"
"flag"
- "io"
"fmt"
+ "html/template"
"log"
"math/rand"
"net"
@@ -12,17 +13,55 @@ import (
"strings"
"sync"
"time"
- "embed"
"github.com/gorilla/mux"
"riedstra.dev/mitch/steam-export/steam"
)
+type App struct {
+ Library *steamLib
+ Status *statusInfo
+
+ Templates *template.Template
+
+ // Sending to this channel triggers the downloader in the background
+ download chan string
+}
+
+func NewApp(libPath string) (*App, error) {
+ lib, err := steam.NewLibrary(libPath)
+ if err != nil {
+ return nil, err
+ }
+
+ a := &App{
+ Library: &steamLib{},
+ Status: &statusInfo{},
+ download: make(chan string),
+ }
+
+ a.Library.Library = *lib
+
+ a.Templates = template.Must(template.New("index").Parse(indexTemplate))
+
+ return a, nil
+}
+
type steamLib struct {
steam.Library
sync.Mutex
}
+type statusInfo struct {
+ sync.RWMutex
+ Running bool
+ Error error
+ Url string
+ Transferred int64
+ Size int64
+ Start *time.Time
+}
+
var (
Version = "Development"
Logger = log.New(os.Stderr, "", log.LstdFlags)
@@ -30,25 +69,31 @@ var (
//go:embed static/*
embeddedStatic embed.FS
-
- Lib steamLib
+ //go:embed templates/index.html
+ indexTemplate string
)
-func reloadLib() {
+func (a *App) LibrarySet(path string) {
Logger.Println("Starting library reload")
- Lib.Lock()
- defer Lib.Unlock()
+ a.Library.Lock()
+ defer a.Library.Unlock()
var err error
- l2, err := steam.NewLibrary(DefaultLib)
+ l2, err := steam.NewLibrary(path)
if err != nil {
- Logger.Printf("Error reopening library: %s", err)
+ Logger.Printf("Error reopening lib: %s", err)
return
}
- Lib.Library = *l2
- Logger.Println("Done reloading library")
+ a.Library.Library = *l2
+ Logger.Println("Done reloading lib")
}
-func setLibHandler(w http.ResponseWriter, r *http.Request) {
+func (a *App) LibraryReload() {
+ cur := a.Library.Folder
+ a.LibrarySet(cur)
+ return
+}
+
+func (a *App) HandleSetLib(w http.ResponseWriter, r *http.Request) {
if unauthorizedIfNotLocal(w, r) {
return
}
@@ -60,39 +105,12 @@ func setLibHandler(w http.ResponseWriter, r *http.Request) {
return
}
- DefaultLib = r.Form.Get("path")
- reloadLib()
- http.Redirect(w, r, "/", 302)
-}
-
-func staticHandler(w http.ResponseWriter, r *http.Request){
- /*
- vars := mux.Vars(r)
-
- fn, ok := vars["fn"]
- if !ok {
- http.Error(w, "Not found", http.StatusNotFound)
- return
- }
- */
-
- fn := r.URL.Path
-
- fh, err := embeddedStatic.Open(fn)
- if err != nil {
- Logger.Printf("While reading embedded file: %s", err)
- http.Error(w, "Not found", http.StatusNotFound)
- return
- }
- defer fh.Close()
+ a.LibrarySet(r.Form.Get("path"))
- _, err = io.Copy(w, fh)
- if err != nil {
- Logger.Printf("While sending static file: %s", err)
- }
+ http.Redirect(w, r, "/", 302)
}
-func quitHandler(w http.ResponseWriter, r *http.Request) {
+func (a *App) HandleQuit(w http.ResponseWriter, r *http.Request) {
if unauthorizedIfNotLocal(w, r) {
return
}
@@ -176,28 +194,25 @@ func main() {
Logger.SetFlags(log.LstdFlags | log.Llongfile)
}
- var err error
- var l *steam.Library
- l, err = steam.NewLibrary(DefaultLib)
+ a, err := NewApp(DefaultLib)
if err != nil {
- Logger.Fatalf("While opening library path: %s", err)
+ Logger.Fatal(err)
}
- Lib.Library = *l
- go installer(getPath)
+ go a.installer()
r := mux.NewRouter()
- r.HandleFunc("/quit", quitHandler)
- r.HandleFunc("/setLib", setLibHandler)
- r.HandleFunc("/delete", gameDelete)
- r.HandleFunc("/install", gameInstaller)
+ r.HandleFunc("/quit", a.HandleQuit)
+ r.HandleFunc("/setLib", a.HandleSetLib)
+ r.HandleFunc("/delete", a.HandleDelete)
+ r.HandleFunc("/install", a.HandleInstall)
r.HandleFunc("/steam-export-web.exe", serveSelf)
- r.HandleFunc("/download/{game}", gameDownloader)
- r.HandleFunc("/status", statsHandler)
+ r.HandleFunc("/download/{game}", a.HandleDownload)
+ r.HandleFunc("/status", a.HandleStats)
r.PathPrefix("/static").Handler(
http.FileServer(http.FS(embeddedStatic)))
- r.HandleFunc("/", index)
+ r.HandleFunc("/", a.HandleIndex)
s := http.Server{
Handler: r,