aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorMitchell <mitch@riedstra.dev>2021-03-20 01:37:03 -0400
committerMitchell <mitch@riedstra.dev>2021-03-20 01:37:03 -0400
commitd047c36dd09b6169bf27c244196e99bb5df54c3a (patch)
tree5103e1951bd58bbef54c90a13c199e8d1d866492 /cmd
parent0e62a3b46b25e7c101b14ed44235f3c276982fc0 (diff)
downloadsteam-export-d047c36dd09b6169bf27c244196e99bb5df54c3a.tar.gz
steam-export-d047c36dd09b6169bf27c244196e99bb5df54c3a.tar.xz
Update documentation. Remove all traces of chdir from the steam library. Remove most linter complaints.
Diffstat (limited to 'cmd')
-rw-r--r--cmd/web/app.go11
-rw-r--r--cmd/web/handlers.go14
-rw-r--r--cmd/web/install.go5
-rw-r--r--cmd/web/main.go11
-rw-r--r--cmd/web/util.go18
5 files changed, 44 insertions, 15 deletions
diff --git a/cmd/web/app.go b/cmd/web/app.go
index b0749b5..1050343 100644
--- a/cmd/web/app.go
+++ b/cmd/web/app.go
@@ -8,11 +8,13 @@ import (
"riedstra.dev/mitch/steam-export/steam"
)
+// steamLib is a steam libary with an embeded mutex to protect it
type steamLib struct {
steam.Library
sync.Mutex
}
+// statusInfo represents the internal status of game installation
type statusInfo struct {
sync.RWMutex
Running bool
@@ -23,6 +25,8 @@ type statusInfo struct {
Start *time.Time
}
+// App binds together the steam library, templates, and channel for install
+// requests as well as most the app specific http handlers.
type App struct {
Library *steamLib
Status *statusInfo
@@ -33,6 +37,8 @@ type App struct {
download chan string
}
+// NewApp sets up the steam library for us as well as parses the embedded
+// template
func NewApp(libPath string) (*App, error) {
lib, err := steam.NewLibrary(libPath)
if err != nil {
@@ -52,6 +58,9 @@ func NewApp(libPath string) (*App, error) {
return a, nil
}
+// LibrarySet takes care of locking the Library and switching over to a new
+// path, unlocking when done.
+// Errors will be logged and no changes will be made if unsuccessful.
func (a *App) LibrarySet(path string) {
Logger.Println("Starting library reload")
a.Library.Lock()
@@ -66,6 +75,8 @@ func (a *App) LibrarySet(path string) {
Logger.Println("Done reloading lib")
}
+// LibraryReload calls LibrarySet but with the current directory, forcing a reload of
+// information off of disk.
func (a *App) LibraryReload() {
cur := a.Library.Folder
a.LibrarySet(cur)
diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go
index b9b863a..661c411 100644
--- a/cmd/web/handlers.go
+++ b/cmd/web/handlers.go
@@ -14,6 +14,8 @@ import (
"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
@@ -36,7 +38,7 @@ func (a *App) HandleIndex(w http.ResponseWriter, r *http.Request) {
&a.Library.Library,
a.Status,
isLocal(r.RemoteAddr),
- getHostIP(),
+ GetHostIP(),
getPort(),
Version,
})
@@ -46,6 +48,8 @@ func (a *App) HandleIndex(w http.ResponseWriter, r *http.Request) {
Logger.Printf("Client %s Index page", r.RemoteAddr)
}
+// HandleInstall takes the HTTP requests for installing a game from a URL
+// or local file path
func (a *App) HandleInstall(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
@@ -78,6 +82,7 @@ func (a *App) HandleInstall(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", 302)
}
+// HandleDownload takes care of exporting our games out to an HTTP request
func (a *App) HandleDownload(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
game := vars["game"]
@@ -129,6 +134,9 @@ func (a *App) HandleDownload(w http.ResponseWriter, r *http.Request) {
Logger.Printf("Client %s finished downloading: %s", r.RemoteAddr, game)
}
+// HandleDelete removes the game in question, though it doesn't
+// spawn any background processes it usually completes fast enough.
+// TODO: Fix if the request taking too long becomes an issue
func (a *App) HandleDelete(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
@@ -166,6 +174,8 @@ func (a *App) HandleDelete(w http.ResponseWriter, r *http.Request) {
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()
@@ -181,6 +191,7 @@ func (a *App) HandleStats(w http.ResponseWriter, r *http.Request) {
return
}
+// HandleSetLib sets a new library path
func (a *App) HandleSetLib(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
@@ -194,6 +205,7 @@ func (a *App) HandleSetLib(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", 302)
}
+// HandleQuit just calls os.Exit after finishing the request
func HandleQuit(w http.ResponseWriter, r *http.Request) {
Logger.Println("Quit was called, exiting")
w.Header().Add("Content-type", "text/plain")
diff --git a/cmd/web/install.go b/cmd/web/install.go
index dc85a89..e59a5c5 100644
--- a/cmd/web/install.go
+++ b/cmd/web/install.go
@@ -10,7 +10,7 @@ import (
"time"
)
-func (a *App) 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 {
@@ -97,7 +97,7 @@ func (a *App) installer() {
a.Status.Unlock()
if strings.HasPrefix(u, "http") {
- err = a.installHttp(u)
+ err = a.installHTTP(u)
} else {
err = a.installPath(u)
}
@@ -111,4 +111,3 @@ func (a *App) installer() {
a.LibraryReload()
}
}
-
diff --git a/cmd/web/main.go b/cmd/web/main.go
index da0bebd..7475478 100644
--- a/cmd/web/main.go
+++ b/cmd/web/main.go
@@ -14,9 +14,12 @@ import (
)
var (
+ // Version is overridden by the build script
Version = "Development"
- Logger = log.New(os.Stderr, "", log.LstdFlags)
- Listen = ":8899"
+ // Logger default logging to stderr
+ Logger = log.New(os.Stderr, "", log.LstdFlags)
+ // Listen address for the webserver
+ Listen = ":8899"
//go:embed static/*
embeddedStatic embed.FS
@@ -48,7 +51,7 @@ func main() {
r.Handle("/setLib", UnauthorizedIfNotLocal(http.HandlerFunc(a.HandleSetLib)))
r.Handle("/delete", UnauthorizedIfNotLocal(http.HandlerFunc(a.HandleDelete)))
r.Handle("/install", UnauthorizedIfNotLocal(http.HandlerFunc(a.HandleInstall)))
- r.HandleFunc("/steam-export-web.exe", serveSelf)
+ r.HandleFunc("/steam-export-web.exe", ServeSelf)
r.HandleFunc("/download/{game}", a.HandleDownload)
r.Handle("/status", UnauthorizedIfNotLocal(http.HandlerFunc(a.HandleStats)))
r.PathPrefix("/static").Handler(
@@ -72,7 +75,7 @@ func main() {
if err != nil {
panic(err)
}
- port += 1
+ port++
Listen = fmt.Sprintf("%s:%d", parts[0], port)
diff --git a/cmd/web/util.go b/cmd/web/util.go
index 1252c66..2c32922 100644
--- a/cmd/web/util.go
+++ b/cmd/web/util.go
@@ -2,19 +2,21 @@ package main
import (
"io"
- "os"
- "net/http"
"net"
+ "net/http"
+ "os"
"strings"
)
+// UnauthorizedIfNotLocal is a middleware that returns unauthorized if not
+// being accessed from loopback, as a basic form of host authentication.
func UnauthorizedIfNotLocal(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !isLocal(r.RemoteAddr) {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
Logger.Printf("Unauthorized request from: %s for %s",
r.RemoteAddr, r.RequestURI)
- return
+ return
}
h.ServeHTTP(w, r)
})
@@ -25,10 +27,10 @@ func isLocal(addr string) bool {
return localNet.Contains(net.ParseIP(strings.Split(addr, ":")[0]))
}
-// getHostIP attempts to guess the IP address of the current machine and
+// GetHostIP attempts to guess the IP address of the current machine and
// returns that. Simply bails at the first non sane looking IP and returns it.
// Not ideal but it should work well enough most of the time
-func getHostIP() string {
+func GetHostIP() string {
iFaces, err := net.Interfaces()
if err != nil {
return "127.0.0.1"
@@ -40,7 +42,7 @@ func getHostIP() string {
for _, iFace := range iFaces {
addrs, err := iFace.Addrs()
if err != nil {
- return "127.0.0.1"
+ continue
}
for _, a := range addrs {
@@ -68,7 +70,9 @@ func getPort() string {
return s[1]
}
-func serveSelf(w http.ResponseWriter, r *http.Request) {
+// ServeSelf tries to locate the currently running executable and serve
+// it down to the client.
+func ServeSelf(w http.ResponseWriter, r *http.Request) {
s, err := os.Executable()
if err != nil {
Logger.Println("While trying to get my executable path: ", err)