From eaf02771d767e4745572d9b00e71e138ee030e60 Mon Sep 17 00:00:00 2001 From: Mitch Riedstra Date: Thu, 4 Mar 2021 17:54:03 -0500 Subject: Switch go go 1.16 and use Embed --- cmd/web/css.go | 96 ---------------------------------- cmd/web/index.go | 121 ++----------------------------------------- cmd/web/js.go | 109 -------------------------------------- cmd/web/main.go | 36 ++++++++++++- cmd/web/static/main.js | 84 ++++++++++++++++++++++++++++++ cmd/web/static/style.css | 81 +++++++++++++++++++++++++++++ cmd/web/templates/index.html | 116 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 319 insertions(+), 324 deletions(-) delete mode 100644 cmd/web/css.go delete mode 100644 cmd/web/js.go create mode 100644 cmd/web/static/main.js create mode 100644 cmd/web/static/style.css create mode 100644 cmd/web/templates/index.html (limited to 'cmd/web') diff --git a/cmd/web/css.go b/cmd/web/css.go deleted file mode 100644 index ec7267a..0000000 --- a/cmd/web/css.go +++ /dev/null @@ -1,96 +0,0 @@ -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; -} -a { - text-decoration: none; - color: #268bd2; -} -a:visited { - color: #d22653; -} -a:hover { - text-decoration: underline; -} -pre code { - line-height: 1.1; - overflow: auto; -} - -code { - color: #9672d5; - font-size: .8em; - font-family: "Roboto Mono", "Monaco", "Lucida Console", "DejaVu Sans Mono", -"monospace"; -} - -pre code { - color: #000; - background-color: #FFFFEA; - display: block; - padding: 10px; - border: 1px solid; -} - -blockquote { - border-left: 4px solid #aaa; - padding-left: 1em; -} - -body { - margin: 40px auto; - max-width: 80%; - line-height: 1.6; - font-size: 1em; - color: #444; - padding: 0 10px; - /* Added because some browsers don't default to white */ - background-color: #fff; -} - -img { - width: 100%; - height: auto; -} - -h1,h2,h3 { - line-height: 1.2 -} - -@media screen and (min-width: 1200px) { - body { - max-width: 960px; - } -} - - -#status { - width: 100%; - background-color: #ddd; -} - -.installBar { - width: 0%; - height: 30px; - /* background-color: #4CAF50; */ - background-color: #268bd2; - text-align: center; - line-height: 30px; - color: white; -} - -`)) - - if err != nil { - Logger.Printf("While sending css: %s", err) - } -} 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(` - - - - - - - Steam Game index - - + //go:embed templates/index.html + indexTemplate string - - - -{{ if .Local }} - -

Library: {{.Lib.Folder}}

- -
- -
- -
-
- -

About

-

-The steam exporter is designed to let you export your steam games, either to -another local hard drive or another computer on the network. -

-

-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. -

-

- - You can download this application from this UI as well here. - -

- -

-You can give people this link to view the library remotely and download -games from your computer: -

-http://{{.HostIP}}:{{.Port}}/ -

-{{ else }} -

Remote Steam library access

- - - If you need this program to install the games click here. - - -

-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. -

-{{ end }} - -

-Installed games: - -Tip: You can right click and save link as to specify a save location, e.g. -an external hard drive -

- - - -{{ if .Local }} -Delete a game: ( Type out exact name, case sensitive ) - -
- - -
- -Install a game from a URL or local file path: - -
- - -
- -

-Note that You can also give someone a URL to install a game if they're running -this program, e.g.
-http://127.0.0.1:8899/install?uri=http://my-server-ip-or-hostname/download/My Game -

- - -Change library path -
- - -
-{{ end }} - -

Version information

-
{{.Version}}
- - -`)) + Templ = template.Must(template.New("index").Parse(indexTemplate)) ) func index(w http.ResponseWriter, r *http.Request) { diff --git a/cmd/web/js.go b/cmd/web/js.go deleted file mode 100644 index 0d54fab..0000000 --- a/cmd/web/js.go +++ /dev/null @@ -1,109 +0,0 @@ -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 = ""; - - var hours = 0; - var minutes = 0; - - if(dur > (60*60)){ - hours = Math.trunc(dur/(60*60)) - out = out + hours + " hours " - } - - if(dur > 60){ - minutes = Math.trunc((dur-(hours*60*60))/60) - out = out + minutes + " minutes " - } - - seconds = Math.trunc(dur - ( (hours*60*60) + (minutes*60) )) - out = out + seconds + " seconds " - - return out; -} - -function setStatus(stat) { - var elem = document.getElementById("installBar"); - var msg = document.getElementById("message"); - - // console.log(stat) - - if(stat.Running) { - percent = Math.round((stat.Transferred/stat.Size)*10000)/100; - - var t = Date.parse(stat.Start); - // var n = Date.now(); - var elapsed = Date.now() - t; - var trans = (stat.Transferred/1024/1024); - - console.log(trans) - console.log(elapsed / 1000) - - var rate = Math.round(trans / (elapsed / 1000)) - - elem.style.width = percent + "%"; - elem.innerHTML = percent + "%"; - elem.style.display = ""; - - // in seconds - var eta = Math.round(((stat.Size - trans)/1024/1024) / rate); - - msg.innerHTML = "Installing: " + stat.Url - + "\nRate: " + rate + "mb/s" - + "\nEta: " + formatDuration(eta); - msg.style.display = ""; - } - - if(!stat.Running && stat.Transferred >= 50 && stat.Error == null) { - elem.style.width = 100 + "%"; - elem.innerHTML = 100 + "%"; - msg.innerHTML = "Completed install for: " + stat.Url; - elem.style.display = ""; - msg.style.display = ""; - } - - if(stat.Error != null) { - msg.innerHTML = "Errors encountered while installing from: \n\n" - + stat.Url + "\n\n" + - JSON.stringify(stat.Error, undefined, 2); - elem.style.display = "hidden"; - msg.style.display = ""; - } -} - -function updateStatus() { -window.fetch('/status') - .then(function(response){ - return response.json(); - }).then(function(json){ - return setStatus(json); - }); -} - -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/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/static/main.js b/cmd/web/static/main.js new file mode 100644 index 0000000..55edf0c --- /dev/null +++ b/cmd/web/static/main.js @@ -0,0 +1,84 @@ +// pretty print duration when given in seconds +function formatDuration(dur) { + var out = ""; + + var hours = 0; + var minutes = 0; + + if(dur > (60*60)){ + hours = Math.trunc(dur/(60*60)) + out = out + hours + " hours " + } + + if(dur > 60){ + minutes = Math.trunc((dur-(hours*60*60))/60) + out = out + minutes + " minutes " + } + + seconds = Math.trunc(dur - ( (hours*60*60) + (minutes*60) )) + out = out + seconds + " seconds " + + return out; +} + +function setStatus(stat) { + var elem = document.getElementById("installBar"); + var msg = document.getElementById("message"); + + // console.log(stat) + + if(stat.Running) { + percent = Math.round((stat.Transferred/stat.Size)*10000)/100; + + var t = Date.parse(stat.Start); + // var n = Date.now(); + var elapsed = Date.now() - t; + var trans = (stat.Transferred/1024/1024); + + console.log(trans) + console.log(elapsed / 1000) + + var rate = Math.round(trans / (elapsed / 1000)) + + elem.style.width = percent + "%"; + elem.innerHTML = percent + "%"; + elem.style.display = ""; + + // in seconds + var eta = Math.round(((stat.Size - trans)/1024/1024) / rate); + + msg.innerHTML = "Installing: " + stat.Url + + "\nRate: " + rate + "mb/s" + + "\nEta: " + formatDuration(eta); + msg.style.display = ""; + } + + if(!stat.Running && stat.Transferred >= 50 && stat.Error == null) { + elem.style.width = 100 + "%"; + elem.innerHTML = 100 + "%"; + msg.innerHTML = "Completed install for: " + stat.Url; + elem.style.display = ""; + msg.style.display = ""; + } + + if(stat.Error != null) { + msg.innerHTML = "Errors encountered while installing from: \n\n" + + stat.Url + "\n\n" + + JSON.stringify(stat.Error, undefined, 2); + elem.style.display = "hidden"; + msg.style.display = ""; + } +} + +function updateStatus() { +window.fetch('/status') + .then(function(response){ + return response.json(); + }).then(function(json){ + return setStatus(json); + }); +} + +document.addEventListener("DOMContentLoaded",function(){ + setInterval(updateStatus, 750); +}); diff --git a/cmd/web/static/style.css b/cmd/web/static/style.css new file mode 100644 index 0000000..ba648e3 --- /dev/null +++ b/cmd/web/static/style.css @@ -0,0 +1,81 @@ +nav { + border-bottom: 3px solid #000; + padding-bottom: 10px; +} +a { + text-decoration: none; + color: #268bd2; +} +a:visited { + color: #d22653; +} +a:hover { + text-decoration: underline; +} +pre code { + line-height: 1.1; + overflow: auto; +} + +code { + color: #9672d5; + font-size: .8em; + font-family: "Roboto Mono", "Monaco", "Lucida Console", "DejaVu Sans Mono", +"monospace"; +} + +pre code { + color: #000; + background-color: #FFFFEA; + display: block; + padding: 10px; + border: 1px solid; +} + +blockquote { + border-left: 4px solid #aaa; + padding-left: 1em; +} + +body { + margin: 40px auto; + max-width: 80%; + line-height: 1.6; + font-size: 1em; + color: #444; + padding: 0 10px; + /* Added because some browsers don't default to white */ + background-color: #fff; +} + +img { + width: 100%; + height: auto; +} + +h1,h2,h3 { + line-height: 1.2 +} + +@media screen and (min-width: 1200px) { + body { + max-width: 960px; + } +} + + +#status { + width: 100%; + background-color: #ddd; +} + +.installBar { + width: 0%; + height: 30px; + /* background-color: #4CAF50; */ + background-color: #268bd2; + text-align: center; + line-height: 30px; + color: white; +} + 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 @@ + + + + + + + Steam Game index + + + + + + +{{ if .Local }} + +

Library: {{.Lib.Folder}}

+ +
+ +
+ +
+
+ +

About

+

+The steam exporter is designed to let you export your steam games, either to +another local hard drive or another computer on the network. +

+

+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. +

+

+ + You can download this application from this UI as well here. + +

+ +

+You can give people this link to view the library remotely and download +games from your computer: +

+http://{{.HostIP}}:{{.Port}}/ +

+{{ else }} +

Remote Steam library access

+ + + If you need this program to install the games click here. + + +

+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. +

+{{ end }} + +

+Installed games: + +Tip: You can right click and save link as to specify a save location, e.g. +an external hard drive +

+ + + +{{ if .Local }} +Delete a game: ( Type out exact name, case sensitive ) + +
+ + +
+ +Install a game from a URL or local file path: + +
+ + +
+ +

+Note that You can also give someone a URL to install a game if they're running +this program, e.g.
+http://127.0.0.1:8899/install?uri=http://my-server-ip-or-hostname/download/My Game +

+ + +Change library path +
+ + +
+{{ end }} + +

Version information

+
{{.Version}}
+ + -- cgit v1.2.3