package main import ( "encoding/json" "fmt" "html/template" "net/http" // "net/url" "os" // "strings" "time" "github.com/gorilla/mux" ) // HandleIndex takes care of rendering our embedded template // and locks the steam library for each request. func (a *App) HandleIndex(w http.ResponseWriter, r *http.Request) { t, err := template.ParseFS(a.templateFS, "templates/index.html") if err != nil { Logger.Printf("While parsing template for index: %s", err) http.Error(w, "Internal server error ( template )", http.StatusInternalServerError) return } err = t.Execute(w, struct { App *App Local bool }{ a, isLocal(r.RemoteAddr), }) if err != nil { Logger.Printf("While Rendering template: %s", err) } Logger.Printf("Client %s Index page", r.RemoteAddr) } // HandleInstall takes the HTTP requests for installing a game from a URL // or local file path func (a *App) HandleInstall(w http.ResponseWriter, r *http.Request) { err := r.ParseForm() if err != nil { Logger.Printf("Installer: While parsing form: %s", err) http.Error(w, fmt.Sprintf("Invalid form: %s", err), 400) return } uri := r.Form.Get("uri") /* // Sanity checking on our end before we pass it off if strings.HasPrefix(uri, "http") { _, err := url.Parse(uri) if err != nil { Logger.Printf("Installer: While parsing url: %s", err) http.Error(w, fmt.Sprintf("Invalid url: %s", err), 400) return } } else { fi, err := os.Stat(uri) if err != nil || !fi.Mode().IsRegular() { Logger.Printf("Installer: While parsing url/path: %s", err) http.Error(w, fmt.Sprintf("Invalid uri/path: %s", err), 400) return } } */ Logger.Printf("Installer: Sending request for: %s to downloader", uri) go func() { g, err := a.Library.ExtractSmart(uri) if err != nil { Logger.Printf("Error encountered installing: %s", err) } Logger.Printf("Extrated game: %s", g) }() http.Redirect(w, r, "/", 302) } // HandleDownload takes care of exporting our games out to an HTTP request func (a *App) HandleDownload(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) game := vars["game"] g, ok := a.Library.Games()[game] if !ok { Logger.Printf("Missing: %s", game) http.Error(w, "Game is missing", 404) return } w.Header().Add("Content-type", "application/tar") w.Header().Add("Estimated-size", fmt.Sprintf("%d", g.Size)) Logger.Printf("Client %s is downloading: %s", r.RemoteAddr, game) err := a.Library.Package(g.Name, w) if err != nil { Logger.Printf("Encountered download error: %s", err) } return } // HandleDelete removes the game in question, though it doesn't // spawn any background processes it usually completes fast enough. // TODO: Fix if the request taking too long becomes an issue func (a *App) HandleDelete(w http.ResponseWriter, r *http.Request) { err := r.ParseForm() if err != nil { Logger.Printf("Installer: While parsing form: %s", err) http.Error(w, fmt.Sprintf("Invalid form: %s", err), 400) return } game := r.PostForm.Get("name") if game == "" { Logger.Println("Deleter: No game specified") http.Error(w, "Game param required", 400) return } g, ok := a.Library.Games()[game] if !ok { Logger.Printf("Missing: %s", game) http.Error(w, "Game is missing", 404) return } err = a.Library.Delete(g.Name) if err != nil { Logger.Printf("Error removing game: %s", err) http.Error(w, fmt.Sprintf("Error removing game: %s", err), 500) return } Logger.Printf("Removed game: %s", game) http.Redirect(w, r, "/", 302) } // HandleStats dumps out some internal statistics of installation which // is then parsed by some JS for a progress bar and such func (a *App) HandleStats(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-type", "application/json") enc := json.NewEncoder(w) enc.SetIndent("", " ") err := enc.Encode(a.Library.Status()) if err != nil { Logger.Println("While encoding Status: ", err) } return } // HandleSetLib sets a new library path func (a *App) HandleSetLib(w http.ResponseWriter, r *http.Request) { err := r.ParseForm() if err != nil { Logger.Printf("Setlib: While parsing form: %s", err) http.Error(w, fmt.Sprintf("Invalid form: %s", err), 400) return } a.Library.ProcessLibrary(r.Form.Get("path")) http.Redirect(w, r, "/", 302) } // HandleQuit just calls os.Exit after finishing the request func HandleQuit(w http.ResponseWriter, r *http.Request) { Logger.Println("Quit was called, exiting") w.Header().Add("Content-type", "text/plain") w.Write([]byte("Shutting down... feel free to close this")) go func() { time.Sleep(time.Millisecond * 50) os.Exit(0) }() return }