package main import ( "fmt" "net/http" "time" "github.com/gorilla/mux" "riedstra.dev/mitch/steam-export/steam" ) // @Summary Return share link // @Description The URL returned is a best effort guess at what your internal // @Description network IP is, on Windows this involves automatically using // @Description the IP from the interface associated with your default route. // @Description On other platforms this involves simply returning the first // @Description sane looking IP address with no regard for anything else. // @Tags all, information // @Accept json // @Produce json // @Success 200 {string} string "URL to currently running server" // @Router /share-link [get] func (a *App) HandleShareLink(w http.ResponseWriter, r *http.Request) { HttpJSONResp(w, r, http.StatusOK, "", a.ShareLink) } // @Summary Delete a videogame // @Description Handle deletion of a game // @Tags all, game // @param game path string true "Name of the videogame" // @Accept json // @Produce json // @Success 200 {object} respStatus "Game was deleted" // @Failure 400 {object} respError "Bad request, most likely no game supplied" // @Failure 404 {object} respError "Game not found" // @Failure 409 {object} respError "Another operation is currently running" // @Failure 500 {object} respError // @Router /lib/game/{game} [delete] func (a *App) HandleDeleteV1(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) game, ok := vars["game"] if !ok { HttpJSONRespErr(w, r, http.StatusBadRequest, "No game supplied", "No game supplied") return } g, ok := a.Library.Games()[game] if !ok { HttpJSONRespErr(w, r, http.StatusNotFound, fmt.Sprintf("missing game: %s", game), "Game is missing") return } // TODO: Check and see if there is another operation running // for this game and return an appropriate message err := a.Library.Delete(g.Name) if err != nil { HttpJSONRespErr(w, r, http.StatusInternalServerError, fmt.Sprintf("Error removing game: %s", err), fmt.Sprintf("Error removing game: %s", err)) return } HttpJSONOK(w, r, fmt.Sprintf("Deleted game %s", game)) } // @Summary Get available games // @Description Returns a list of all currently installed and available games // @Tags all, game // @Accept json // @Produce json // @Success 200 {array} steam.Game // @Router /lib/games [get] func (a *App) HandleGameList(w http.ResponseWriter, r *http.Request) { games := a.Library.Games() out := []*steam.Game{} for _, g := range games { out = append(out, g) } HttpJSONResp(w, r, http.StatusOK, "", &out) } // @Summary Return the version string // @Description Returns the version of the server // @Tags all // @Accept json // @Produce json // @Success 200 {string} string "Version string" // @Router /version [get] func (a *App) HandleVersion(w http.ResponseWriter, r *http.Request) { HttpJSONResp(w, r, http.StatusOK, "", Version) } // @Summary Refresh the current steam library // @Description if no other actions are running on the library it will trigger a // @Description refre // @Tags all, library // @Accept json // @Produce json // @Success 200 {object} respStatus // @Success 500 {object} respError // @Router /lib/refresh [post] func (a *App) HandleRefresh(w http.ResponseWriter, r *http.Request) { err := a.Library.Refresh() if err != nil { HttpJSONRespErr(w, r, http.StatusInternalServerError, fmt.Sprintf("While refreshing library: %s", err), fmt.Sprintf("Error refreshing library, check logs")) return } HttpJSONOK(w, r, "Refreshed library") } // @Summary Set library path to new location on disk // @Description If no other operatoins are currently running this will change // @Description the path in which the current library is pointed. Implies a // @Description refresh. // @param path query string true "Path on disk to search for a steam library" // @Tags all, library // @Accept json // @Produce json // @Success 200 {object} respStatus // @Success 500 {object} respError // @Router /lib/path [post] func (a *App) HandleSetLibV1(w http.ResponseWriter, r *http.Request) { pth := r.URL.Query().Get("path") err := a.Library.ProcessLibrary(pth) if err != nil { HttpJSONRespErr(w, r, http.StatusInternalServerError, fmt.Sprintf("While processing library: ", err), "Internal server error") return } HttpJSONOK(w, r, "Set and Refreshed library") } // @Summary Installs a game // @Description Attemps to install a game from the provided URI // @Description It tries to be smart about it, http, https, or a location // @Description on disk are supported. // @param url query string true "URI to fetch from" // @Tags all, game // @Accept json // @Produce json // @Success 200 {object} respStatus // @Success 500 {object} respError // @Router /lib/install [post] func (a *App) HandleInstallV1(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-type", "application/json") var err error url := r.URL.Query().Get("url") // We're not going to block for the entire install, but we will wait around // for a small bit to see if there are any major failures go func() { var g *steam.Game g, err = a.Library.ExtractSmart(url) if err != nil { Logger.Printf("Error encountered installing: %s", err) } Logger.Printf("Extrated game: %s", g) }() time.Sleep(time.Millisecond * 50) if err != nil { HttpJSONRespErr(w, r, http.StatusInternalServerError, "", fmt.Sprintf("Failed to start game install: %s", err)) return } HttpJSONOK(w, r, "") }