package main import ( "embed" "flag" "fmt" "log" "math/rand" "net/http" "os" "time" "github.com/gorilla/mux" ) var ( Version = "Development" Logger = log.New(os.Stderr, "", log.LstdFlags) Listen = ":8899" //go:embed static/* embeddedStatic embed.FS //go:embed templates/index.html indexTemplate string ) func main() { fl := flag.NewFlagSet("steam-export", flag.ExitOnError) debug := fl.Bool("d", false, "Print line numbers in log") 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) } a, err := NewApp(DefaultLib) if err != nil { Logger.Fatal(err) } go a.installer() r := mux.NewRouter() r.Handle("/quit", UnauthorizedIfNotLocal(http.HandlerFunc(HandleQuit))) r.Handle("/setLib", UnauthorizedIfNotLocal(http.HandlerFunc(a.HandleSetLib))) r.Handle("/delete", UnauthorizedIfNotLocal(http.HandlerFunc(a.HandleDelete))) r.Handle("/install", UnauthorizedIfNotLocal(http.HandlerFunc(a.HandleInstall))) r.HandleFunc("/steam-export-web.exe", serveSelf) r.HandleFunc("/download/{game}", a.HandleDownload) r.Handle("/status", UnauthorizedIfNotLocal(http.HandlerFunc(a.HandleStats))) r.PathPrefix("/static").Handler( http.FileServer(http.FS(embeddedStatic))) r.HandleFunc("/", a.HandleIndex) s := http.Server{ Handler: r, Addr: Listen, } go startBrowser() for i := 0; i < 5; i++ { err = s.ListenAndServe() if err != nil { Logger.Printf("Encountered: %s", err) rand.Seed(time.Now().UnixNano()) Listen = fmt.Sprintf(":%d", rand.Intn(9000)+1024) Logger.Printf("Trying: %s", Listen) s.Addr = Listen } } }