diff options
| author | Mitch Riedstra <mitch@riedstra.us> | 2020-11-21 15:33:57 -0500 |
|---|---|---|
| committer | Mitch Riedstra <mitch@riedstra.us> | 2020-11-21 15:33:57 -0500 |
| commit | 36dc9ff10971cf97eb077907072c519cb5349fe4 (patch) | |
| tree | faf3ff607714ee8b155c801b91a04e2cc8cb2279 /cmd/web/main.go | |
| parent | 4ed57528379c9d1ac0f5bc77b95439bbba3d4488 (diff) | |
| download | steam-export-36dc9ff10971cf97eb077907072c519cb5349fe4.tar.gz steam-export-36dc9ff10971cf97eb077907072c519cb5349fe4.tar.xz | |
Most of the functionality I want is there now, it's not pretty though.
Build the web server and the command line in the build script
Allow for deletion of games from the library
Move the download function out of main.go
Add more information to the index template and handler.
Allow for downloads from URLs and installation from local files.
Allow changing of the library path via a command line flag
Automatically start the web browser on windows
Diffstat (limited to 'cmd/web/main.go')
| -rw-r--r-- | cmd/web/main.go | 71 |
1 files changed, 25 insertions, 46 deletions
diff --git a/cmd/web/main.go b/cmd/web/main.go index 93025e5..64321da 100644 --- a/cmd/web/main.go +++ b/cmd/web/main.go @@ -5,7 +5,7 @@ import ( "log" "net/http" "os" - "path/filepath" + "sync" "github.com/gorilla/mux" "riedstra.dev/mitch/steam-export/steam" @@ -15,75 +15,54 @@ var ( Logger = log.New(os.Stderr, "", log.LstdFlags) Listen = ":8899" - Lib = steam.NewLibraryMust(DefaultLib) + libMu = &sync.RWMutex{} + Lib *steam.Library ) -//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) +func reloadLib() { + libMu.Lock() + defer libMu.Unlock() + var err error + Lib, err = steam.NewLibrary(DefaultLib) if err != nil { - Logger.Printf("Error Sending game: %s", err) - // Headers already sent, don't bother sending an error + Logger.Printf("Error reopening library: %s", err) + return } - } 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.StringVar(&Listen, "l", Listen, "What address do we listen on?") + fl.StringVar(&DefaultLib, "L", DefaultLib, "Full path to default library") fl.Parse(os.Args[1:]) if *debug { Logger.SetFlags(log.LstdFlags | log.Llongfile) } + var err error + Lib, err = steam.NewLibrary(DefaultLib) + if err != nil { + Logger.Fatalf("While opening library path: %s", err) + } + + go installer(getPath) + r := mux.NewRouter() + r.HandleFunc("/delete", gameDelete) + r.HandleFunc("/install", gameInstaller) r.HandleFunc("/download/{game}", gameDownloader) r.HandleFunc("/style.css", cssHandler) r.HandleFunc("/", index) s := http.Server{ Handler: r, - Addr: *listen, + Addr: Listen, } + go startBrowser() + Logger.Fatal(s.ListenAndServe()) } |
