diff options
Diffstat (limited to 'cmd/web/apiv1.go')
| -rw-r--r-- | cmd/web/apiv1.go | 178 |
1 files changed, 177 insertions, 1 deletions
diff --git a/cmd/web/apiv1.go b/cmd/web/apiv1.go index 221e923..a24746c 100644 --- a/cmd/web/apiv1.go +++ b/cmd/web/apiv1.go @@ -1,3 +1,179 @@ package main -import () +import ( + "encoding/json" + "fmt" + "net/http" + + "github.com/gorilla/mux" + "riedstra.dev/mitch/steam-export/steam" +) + +func (a *App) HandleShareLink(w http.ResponseWriter, r *http.Request) { + + // swagger:route GET /share-link share-link + // + // Returns the share link + // + // This will return the share link for the currently running server, + // i.e. not 127.0.0.1 but a URL with (usually) an IPv4 RFC1918 address. + // + // Consumes: + // - application/json + // + // Produces: + // - application/json + // + // Schemes: + // - http + // + // Deprecated: false + // + // Responses: + // 200: shareLinkResp + + 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) + } +} + +// A shareLinkResp is an error that is used when the required input fails +// validation. +// swagger:response shareLinkResp +type shareLinkResp struct { + // + // in: body + Body struct { + // Example: http://10.0.0.99:8899/ + URL string + } +} + +func (a *App) HandleDeleteV1(w http.ResponseWriter, r *http.Request) { + + // swagger:route DELETE /lib/game/{game} game + // + // Deletes a game + // + // Delete a given game where the url param game is the game in question + // + // Consumes: + // - application/json + // + // Produces: + // - application/json + // + // Schemes: http, https + // + // Deprecated: false + // + // Responses: + // 200: ok + // 409: conflict + + 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) + 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) +} + +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{} + + for _, g := range games { + out = append(out, g) + } + + err := enc.Encode(&out) + if err != nil { + Logger.Println("While getting games: ", err) + } + +} + +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) + } +} + +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) + } + + enc := json.NewEncoder(w) + enc.Encode(map[string]interface{}{ + "status": "ok", + }) +} + +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" + } + + enc := json.NewEncoder(w) + enc.Encode(out) +} + +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") + + 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) + }() + +} |
