diff options
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/web/index.go | 121 | ||||
| -rw-r--r-- | cmd/web/main.go | 36 | ||||
| -rw-r--r-- | cmd/web/static/main.js (renamed from cmd/web/js.go) | 25 | ||||
| -rw-r--r-- | cmd/web/static/style.css (renamed from cmd/web/css.go) | 15 | ||||
| -rw-r--r-- | cmd/web/templates/index.html | 116 |
5 files changed, 154 insertions, 159 deletions
diff --git a/cmd/web/index.go b/cmd/web/index.go index 3121384..b90faf8 100644 --- a/cmd/web/index.go +++ b/cmd/web/index.go @@ -3,129 +3,16 @@ package main import ( "html/template" "net/http" + _ "embed" "riedstra.dev/mitch/steam-export/steam" ) var ( - Templ = template.Must(template.New("index").Parse(` -<!DOCTYPE html> -<html lang="en"> -<head> - <meta charset="UTF-8"> - <link id="maincss" rel="stylesheet" href="/style.css" defer> - <meta name="viewport" content="width=device-width, initial-scale=1.0"> - <title>Steam Game index</title> -</head> -<body> + //go:embed templates/index.html + indexTemplate string - -<nav> - <a href="/">Home</a> - {{ if .Local }} - <div style="display: block; float: right;"> - <a href="/quit">Shutdown Server / Quit</a> - </div> - {{ end }} -</nav> - -{{ if .Local }} -<script src="/main.js"></script> -<h2>Library: {{.Lib.Folder}}</h2> - -<div id="status"> - <div id="installBar" class="installBar" style="display: none;">0%</div> -</div> - -<pre><code id="message" style="display: none;"> -</code></pre> -<br /> - -<h3>About</h3> -<p> -The steam exporter is designed to let you export your steam games, either to -another local hard drive or another computer on the network. -</p> -<p> -It also allows you to import games from across the network as well if you -provide an HTTP url from which to download the game file as exported -from this application. -</p> -<p> -<a href="/steam-export-web.exe"> - You can download this application from this UI as well here. -</a> -</p> - -<p> -You can give people this link to view the library remotely and download -games from your computer: -<br /><br /> -<a href="http://{{.HostIP}}:{{.Port}}/">http://{{.HostIP}}:{{.Port}}/</a> -</p> -{{ else }} -<h2>Remote Steam library access</h2> - -<a href="/steam-export-web.exe"> - If you need this program to install the games click here. -</a> - -<p> -Right click and copy the link address to paste into your local machine -if you do not wish to store the archive or have enough space for it on -your drive. -</p> -{{ end }} - -<p> -Installed games: - -Tip: You can right click and save link as to specify a save location, e.g. -an external hard drive -</p> - -<ul> -{{ range $key, $val := .Lib.Games }} -<li> -<a href="/download/{{$key}}">{{$key}} ({{$val.GetSize}})</a> -</li> -{{ end }} -</ul> - -{{ if .Local }} -Delete a game: ( Type out exact name, case sensitive ) - -<form action="/delete" method="POST"> - <input type="text" name="name" /> - <input type="submit" value="Delete"> -</form> - -Install a game from a URL or local file path: - -<form action="/install" method="GET"> - <input type="text" name="uri" /> - <input type="submit" value="Install"> -</form> - -<p> -Note that You can also give someone a URL to install a game if they're running -this program, e.g. <br /> -http://127.0.0.1:8899/install?uri=http://my-server-ip-or-hostname/download/My Game -</p> - - -Change library path -<form action="/setLib" method="GET"> - <input type="text" name="path" /> - <input type="submit" value="Update"> -</form> -{{ end }} - -<h3>Version information</h3> -<pre><code>{{.Version}}</pre></code> - -</body> -`)) + Templ = template.Must(template.New("index").Parse(indexTemplate)) ) func index(w http.ResponseWriter, r *http.Request) { diff --git a/cmd/web/main.go b/cmd/web/main.go index d66e366..091d22f 100644 --- a/cmd/web/main.go +++ b/cmd/web/main.go @@ -2,6 +2,7 @@ package main import ( "flag" + "io" "fmt" "log" "math/rand" @@ -11,6 +12,7 @@ import ( "strings" "sync" "time" + "embed" "github.com/gorilla/mux" "riedstra.dev/mitch/steam-export/steam" @@ -26,6 +28,9 @@ var ( Logger = log.New(os.Stderr, "", log.LstdFlags) Listen = ":8899" + //go:embed static/* + embeddedStatic embed.FS + Lib steamLib ) @@ -60,6 +65,33 @@ func setLibHandler(w http.ResponseWriter, r *http.Request) { 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() + + _, err = io.Copy(w, fh) + if err != nil { + Logger.Printf("While sending static file: %s", err) + } +} + func quitHandler(w http.ResponseWriter, r *http.Request) { if unauthorizedIfNotLocal(w, r) { return @@ -163,8 +195,8 @@ func main() { r.HandleFunc("/steam-export-web.exe", serveSelf) r.HandleFunc("/download/{game}", gameDownloader) r.HandleFunc("/status", statsHandler) - r.HandleFunc("/style.css", cssHandler) - r.HandleFunc("/main.js", jsHandler) + r.PathPrefix("/static").Handler( + http.FileServer(http.FS(embeddedStatic))) r.HandleFunc("/", index) s := http.Server{ diff --git a/cmd/web/js.go b/cmd/web/static/main.js index 0d54fab..55edf0c 100644 --- a/cmd/web/js.go +++ b/cmd/web/static/main.js @@ -1,13 +1,3 @@ -package main - -import ( - // "io/ioutil" - "net/http" -) - -func jsHandler(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-type", "application/javascript") - _, err := w.Write([]byte(` // pretty print duration when given in seconds function formatDuration(dur) { var out = ""; @@ -92,18 +82,3 @@ window.fetch('/status') document.addEventListener("DOMContentLoaded",function(){ setInterval(updateStatus, 750); }); -`)) - - /* - b, err := ioutil.ReadFile("C:\\Users\\mitch\\Documents\\my.js") - if err != nil { - Logger.Printf("While reading js: %s", err) - return - } - _, err = w.Write(b) - */ - - if err != nil { - Logger.Printf("While sending js: %s", err) - } -} diff --git a/cmd/web/css.go b/cmd/web/static/style.css index ec7267a..ba648e3 100644 --- a/cmd/web/css.go +++ b/cmd/web/static/style.css @@ -1,12 +1,3 @@ -package main - -import ( - "net/http" -) - -func cssHandler(w http.ResponseWriter, r *http.Request){ - w.Header().Add("Content-type", "text/css") - _, err := w.Write([]byte(` nav { border-bottom: 3px solid #000; padding-bottom: 10px; @@ -88,9 +79,3 @@ h1,h2,h3 { color: white; } -`)) - - if err != nil { - Logger.Printf("While sending css: %s", err) - } -} diff --git a/cmd/web/templates/index.html b/cmd/web/templates/index.html new file mode 100644 index 0000000..74f2298 --- /dev/null +++ b/cmd/web/templates/index.html @@ -0,0 +1,116 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <link id="maincss" rel="stylesheet" href="/static/style.css" defer> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>Steam Game index</title> +</head> +<body> + + +<nav> + <a href="/">Home</a> + {{ if .Local }} + <div style="display: block; float: right;"> + <a href="/quit">Shutdown Server / Quit</a> + </div> + {{ end }} +</nav> + +{{ if .Local }} +<script src="/static/main.js"></script> +<h2>Library: {{.Lib.Folder}}</h2> + +<div id="status"> + <div id="installBar" class="installBar" style="display: none;">0%</div> +</div> + +<pre><code id="message" style="display: none;"> +</code></pre> +<br /> + +<h3>About</h3> +<p> +The steam exporter is designed to let you export your steam games, either to +another local hard drive or another computer on the network. +</p> +<p> +It also allows you to import games from across the network as well if you +provide an HTTP url from which to download the game file as exported +from this application. +</p> +<p> +<a href="/steam-export-web.exe"> + You can download this application from this UI as well here. +</a> +</p> + +<p> +You can give people this link to view the library remotely and download +games from your computer: +<br /><br /> +<a href="http://{{.HostIP}}:{{.Port}}/">http://{{.HostIP}}:{{.Port}}/</a> +</p> +{{ else }} +<h2>Remote Steam library access</h2> + +<a href="/steam-export-web.exe"> + If you need this program to install the games click here. +</a> + +<p> +Right click and copy the link address to paste into your local machine +if you do not wish to store the archive or have enough space for it on +your drive. +</p> +{{ end }} + +<p> +Installed games: + +Tip: You can right click and save link as to specify a save location, e.g. +an external hard drive +</p> + +<ul> +{{ range $key, $val := .Lib.Games }} +<li> +<a href="/download/{{$key}}">{{$key}} ({{$val.GetSize}})</a> +</li> +{{ end }} +</ul> + +{{ if .Local }} +Delete a game: ( Type out exact name, case sensitive ) + +<form action="/delete" method="POST"> + <input type="text" name="name" /> + <input type="submit" value="Delete"> +</form> + +Install a game from a URL or local file path: + +<form action="/install" method="GET"> + <input type="text" name="uri" /> + <input type="submit" value="Install"> +</form> + +<p> +Note that You can also give someone a URL to install a game if they're running +this program, e.g. <br /> +http://127.0.0.1:8899/install?uri=http://my-server-ip-or-hostname/download/My Game +</p> + + +Change library path +<form action="/setLib" method="GET"> + <input type="text" name="path" /> + <input type="submit" value="Update"> +</form> +{{ end }} + +<h3>Version information</h3> +<pre><code>{{.Version}}</pre></code> + +</body> |
