diff options
| author | Mitch Riedstra <mitch@riedstra.us> | 2020-11-21 01:13:16 -0500 |
|---|---|---|
| committer | Mitch Riedstra <mitch@riedstra.us> | 2020-11-21 01:13:16 -0500 |
| commit | 4ed57528379c9d1ac0f5bc77b95439bbba3d4488 (patch) | |
| tree | df607f257e8836da69c0b9c9e8ef840c2acadff1 | |
| parent | 78712527eed66929b8147e04d18f000dcb7dfff6 (diff) | |
| download | steam-export-4ed57528379c9d1ac0f5bc77b95439bbba3d4488.tar.gz steam-export-4ed57528379c9d1ac0f5bc77b95439bbba3d4488.tar.xz | |
Initial version of a web interface
| -rw-r--r-- | cmd/web/css.go | 80 | ||||
| -rw-r--r-- | cmd/web/index.go | 56 | ||||
| -rw-r--r-- | cmd/web/main.go | 89 | ||||
| -rw-r--r-- | cmd/web/unix.go | 10 | ||||
| -rw-r--r-- | cmd/web/windows.go | 5 | ||||
| -rw-r--r-- | go.mod | 5 | ||||
| -rw-r--r-- | go.sum | 2 | ||||
| -rw-r--r-- | steam/steam.go | 17 |
8 files changed, 263 insertions, 1 deletions
diff --git a/cmd/web/css.go b/cmd/web/css.go new file mode 100644 index 0000000..3b772de --- /dev/null +++ b/cmd/web/css.go @@ -0,0 +1,80 @@ +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; + } +} + +`)) + + if err != nil { + Logger.Printf("While sending css: %s", err) + } +} diff --git a/cmd/web/index.go b/cmd/web/index.go new file mode 100644 index 0000000..1a2c344 --- /dev/null +++ b/cmd/web/index.go @@ -0,0 +1,56 @@ +package main + +import ( + "net/http" + "html/template" +) + +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> +<nav> + <a href="/">Home</a> + <div style="display: block; float: right;"> + <!-- <a href="">Right Aligned</a> --> + </div> +</nav> + +<h2>Library: {{.Folder}}</h2> + +<p> +Installed games: +</p> + +<ul> +{{ range $key, $val := .Games }} +<li> +<a href="/download/{{$key}}">{{$key}}</a> +</li> +{{ end }} +</ul> + +Install a game from a URL: + +<form action="/install" method="GET"> + <input type="text" name="url" /> + <input type="submit" value="Install"> +</form> + +</body> +`)) +) + +func index(w http.ResponseWriter, r *http.Request) { + err := Templ.ExecuteTemplate(w, "index", Lib) + if err != nil { + Logger.Printf("While Rendering template: %s", err) + } +} diff --git a/cmd/web/main.go b/cmd/web/main.go new file mode 100644 index 0000000..93025e5 --- /dev/null +++ b/cmd/web/main.go @@ -0,0 +1,89 @@ +package main + +import ( + "flag" + "log" + "net/http" + "os" + "path/filepath" + + "github.com/gorilla/mux" + "riedstra.dev/mitch/steam-export/steam" +) + +var ( + Logger = log.New(os.Stderr, "", log.LstdFlags) + Listen = ":8899" + + Lib = steam.NewLibraryMust(DefaultLib) +) + +//size, err := estimateSize(g.LibraryPath + "common/" + g.Name) +//if err != nil { +// http.Error(w, "Encountered:"+err, 500) +// return +//} +func estimateSize(pth string) (int64, error) { + var size int64 = 0 + + err := filepath.Walk(pth, func(path string, info os.FileInfo, err error) error { + + if err != nil { + return err + } + + if info.Mode().IsRegular() { + size = size + info.Size() + } + + return nil + }) + + return size, err + +} + +func gameDownloader(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + game := vars["game"] + + g, ok := Lib.Games[game] + if !ok { + Logger.Printf("Missing: %s", game) + http.Error(w, "Game is missing", 404) + return + } + + w.Header().Add("Content-type", "application/tar") + + err := g.Package(w) + if err != nil { + Logger.Printf("Error Sending game: %s", err) + // Headers already sent, don't bother sending an error + } + +} + +func main() { + fl := flag.NewFlagSet("steam-export", flag.ExitOnError) + debug := fl.Bool("d", false, "Print line numbers in log") + listen := fl.String("l", Listen, "What address do we listen on?") + fl.Parse(os.Args[1:]) + + if *debug { + Logger.SetFlags(log.LstdFlags | log.Llongfile) + } + + r := mux.NewRouter() + + r.HandleFunc("/download/{game}", gameDownloader) + r.HandleFunc("/style.css", cssHandler) + r.HandleFunc("/", index) + + s := http.Server{ + Handler: r, + Addr: *listen, + } + + Logger.Fatal(s.ListenAndServe()) +} diff --git a/cmd/web/unix.go b/cmd/web/unix.go new file mode 100644 index 0000000..6b1b1ae --- /dev/null +++ b/cmd/web/unix.go @@ -0,0 +1,10 @@ +// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris + +package main + +import ( + "os" + "path/filepath" +) + +var DefaultLib string = filepath.Join(os.Getenv("HOME"), ".steam/steam/steamapps") diff --git a/cmd/web/windows.go b/cmd/web/windows.go new file mode 100644 index 0000000..2effa08 --- /dev/null +++ b/cmd/web/windows.go @@ -0,0 +1,5 @@ +// +build windows + +package main + +var DefaultLib string = `C:\Program Files (x86)\Steam\steamapps` @@ -2,4 +2,7 @@ module riedstra.dev/mitch/steam-export go 1.15 -require gopkg.in/yaml.v2 v2.3.0 +require ( + github.com/gorilla/mux v1.8.0 + gopkg.in/yaml.v2 v2.3.0 +) @@ -1,3 +1,5 @@ +github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= +github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/steam/steam.go b/steam/steam.go index 7071a20..8c3dcbb 100644 --- a/steam/steam.go +++ b/steam/steam.go @@ -35,6 +35,23 @@ func ProcessMultipleLibraries(r []string) ([]*Library, error) { return libs, nil } +func NewLibrary(path string) (*Library, error) { + l := &Library{} + err := l.ProcessLibrary(path) + if err != nil { + return nil, err + } + return l, err +} + +func NewLibraryMust(path string) (*Library) { + l, err := NewLibrary(path) + if err != nil { + panic(err) + } + return l +} + // Populate the "Folder" and "Games" fields based on the provided directory func (s *Library) ProcessLibrary(r string) error { if !hasCommon(r) { |
