From 4ed57528379c9d1ac0f5bc77b95439bbba3d4488 Mon Sep 17 00:00:00 2001 From: Mitch Riedstra Date: Sat, 21 Nov 2020 01:13:16 -0500 Subject: Initial version of a web interface --- cmd/web/css.go | 80 ++++++++++++++++++++++++++++++++++++++++++++++++ cmd/web/index.go | 56 ++++++++++++++++++++++++++++++++++ cmd/web/main.go | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/web/unix.go | 10 ++++++ cmd/web/windows.go | 5 +++ 5 files changed, 240 insertions(+) create mode 100644 cmd/web/css.go create mode 100644 cmd/web/index.go create mode 100644 cmd/web/main.go create mode 100644 cmd/web/unix.go create mode 100644 cmd/web/windows.go (limited to 'cmd/web') 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(` + + + + + + + Steam Game index + + + + +

Library: {{.Folder}}

+ +

+Installed games: +

+ + + +Install a game from a URL: + +
+ + +
+ + +`)) +) + +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` -- cgit v1.2.3