diff options
| author | Mitchell Riedstra <mitch@riedstra.dev> | 2021-08-24 22:25:40 -0400 |
|---|---|---|
| committer | Mitchell Riedstra <mitch@riedstra.dev> | 2021-08-24 22:25:40 -0400 |
| commit | 0fc3eda77004f41c5f0a804028da2d90b0373ea7 (patch) | |
| tree | edf6d74e6a2b7140350eab1e40d0fbf70e10f87c /cmd/web/apiv1.go | |
| parent | f4fcd237b2d0eb950fc15a8af1fb0894b6ad6359 (diff) | |
| download | steam-export-0fc3eda77004f41c5f0a804028da2d90b0373ea7.tar.gz steam-export-0fc3eda77004f41c5f0a804028da2d90b0373ea7.tar.xz | |
Another development snapshot. Updated license. Added Swagger documentation--embedded! Note about 'swaggo'
Diffstat (limited to 'cmd/web/apiv1.go')
| -rw-r--r-- | cmd/web/apiv1.go | 153 |
1 files changed, 105 insertions, 48 deletions
diff --git a/cmd/web/apiv1.go b/cmd/web/apiv1.go index 9a39033..29d93cb 100644 --- a/cmd/web/apiv1.go +++ b/cmd/web/apiv1.go @@ -1,57 +1,80 @@ package main import ( - "encoding/json" "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) { - - w.Header().Add("Content-type", "application/json") - enc := json.NewEncoder(w) - err := enc.Encode(a.ShareLink) - if err != nil { - Logger.Println("While getting shareLink: ", err) - } + 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 { - Logger.Println("No game supplied for HandleDeleteV1") - http.Error(w, "No game supplied", http.StatusBadRequest) + HttpJSONRespErr(w, r, http.StatusBadRequest, + "No game supplied", "No game supplied") return } g, ok := a.Library.Games()[game] if !ok { - Logger.Printf("Missing: %s", game) - http.Error(w, "Game is missing", 404) + 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 { - Logger.Printf("Error removing game: %s", err) - http.Error(w, fmt.Sprintf("Error removing game: %s", err), 500) + HttpJSONRespErr(w, r, http.StatusInternalServerError, + fmt.Sprintf("Error removing game: %s", err), + fmt.Sprintf("Error removing game: %s", err)) return } - Logger.Printf("Removed game: %s", game) - http.Redirect(w, r, "/", 302) + 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) { - w.Header().Add("Content-type", "application/json") - enc := json.NewEncoder(w) - games := a.Library.Games() out := []*steam.Game{} @@ -59,61 +82,85 @@ func (a *App) HandleGameList(w http.ResponseWriter, r *http.Request) { out = append(out, g) } - err := enc.Encode(&out) - if err != nil { - Logger.Println("While getting games: ", err) - } - + 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) { - w.Header().Add("Content-type", "application/json") - enc := json.NewEncoder(w) - err := enc.Encode(Version) - if err != nil { - Logger.Println("While getting version: ", err) - } + 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) { - w.Header().Add("Content-type", "application/json") err := a.Library.Refresh() if err != nil { - Logger.Println("While refreshing steam lib: ", err) + HttpJSONRespErr(w, r, http.StatusInternalServerError, + fmt.Sprintf("While refreshing library: %s", err), + fmt.Sprintf("Error refreshing library, check logs")) + return } - enc := json.NewEncoder(w) - enc.Encode(map[string]interface{}{ - "status": "ok", - }) + 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) { - - out := map[string]interface{}{} - - w.Header().Add("Content-type", "application/json") pth := r.URL.Query().Get("path") err := a.Library.ProcessLibrary(pth) if err != nil { - Logger.Println("While processing library: ", err) - out["error"] = err.Error() - out["status"] = "error" - } else { - out["status"] = "ok" + HttpJSONRespErr(w, r, http.StatusInternalServerError, + fmt.Sprintf("While processing library: ", err), + "Internal server error") + return } - enc := json.NewEncoder(w) - enc.Encode(out) + 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) @@ -123,4 +170,14 @@ func (a *App) HandleInstallV1(w http.ResponseWriter, r *http.Request) { 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, "") } |
