diff options
| author | Mitchell Riedstra <mitch@riedstra.dev> | 2021-08-11 20:24:54 -0400 |
|---|---|---|
| committer | Mitchell Riedstra <mitch@riedstra.dev> | 2021-08-11 20:24:54 -0400 |
| commit | 705fc2f44be5528b07897cd2f020f429024cddf0 (patch) | |
| tree | b4c761104ca78b7fa2df573411808491a3f72eb2 /cmd | |
| parent | 9d5d130038ed90564c3acfb2fd2ff64e3d7b0bd9 (diff) | |
| download | steam-export-705fc2f44be5528b07897cd2f020f429024cddf0.tar.gz steam-export-705fc2f44be5528b07897cd2f020f429024cddf0.tar.xz | |
Mess around with go-swagger annotations. Remove some commented out code.
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/web/apiv1.go | 178 | ||||
| -rw-r--r-- | cmd/web/app.go | 25 | ||||
| -rw-r--r-- | cmd/web/handlers.go | 21 | ||||
| -rw-r--r-- | cmd/web/routes.go | 18 | ||||
| -rw-r--r-- | cmd/web/swagger.go | 71 |
5 files changed, 268 insertions, 45 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) + }() + +} diff --git a/cmd/web/app.go b/cmd/web/app.go index 578c44d..11df3ac 100644 --- a/cmd/web/app.go +++ b/cmd/web/app.go @@ -7,19 +7,8 @@ import ( "riedstra.dev/mitch/steam-export/steam" ) -// // statusInfo represents the internal status of game installation -// type statusInfo struct { -// sync.RWMutex -// Running bool -// Error error -// Url string -// Transferred int64 -// Size int64 -// Start *time.Time -// } - -// App binds together the steam library, templates, and channel for install -// requests as well as most the app specific http handlers. +// App binds together the steam library, templates filesystem, handlers, +// etc type App struct { Library *steam.Library @@ -35,8 +24,7 @@ type App struct { // download chan string } -// NewApp sets up the steam library for us as well as parses the embedded -// template +// NewApp sets up the steam library for us func NewApp(libPath string) (*App, error) { lib, err := steam.NewLibrary(libPath) if err != nil { @@ -44,10 +32,9 @@ func NewApp(libPath string) (*App, error) { } a := &App{ - Library: lib, - Version: Version, - ShareLink: getShareLink(), - // download: make(chan string), + Library: lib, + Version: Version, + ShareLink: getShareLink(), templateFS: TemplateFS, staticFS: StaticFS, } diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go index 763c980..e61c29d 100644 --- a/cmd/web/handlers.go +++ b/cmd/web/handlers.go @@ -5,9 +5,7 @@ import ( "fmt" "html/template" "net/http" - // "net/url" "os" - // "strings" "time" "github.com/gorilla/mux" @@ -50,25 +48,6 @@ func (a *App) HandleInstall(w http.ResponseWriter, r *http.Request) { 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() { diff --git a/cmd/web/routes.go b/cmd/web/routes.go index 12bb807..52ae5d9 100644 --- a/cmd/web/routes.go +++ b/cmd/web/routes.go @@ -9,7 +9,8 @@ import ( func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) { rtr := mux.NewRouter() - // rtr.PathPrefix("/api/v1").Handler(a.HandleAPIv1()) + rtr.PathPrefix("/api/v1").Handler( + http.StripPrefix("/api/v1", a.HandleAPIv1())) rtr.Handle("/quit", UnauthorizedIfNotLocal(http.HandlerFunc(HandleQuit))) rtr.Handle("/setLib", UnauthorizedIfNotLocal(http.HandlerFunc(a.HandleSetLib))) @@ -31,9 +32,18 @@ func (a *App) HandleAPIv1() http.Handler { rtr := mux.NewRouter() rtr.Handle("/quit", UnauthorizedIfNotLocal(http.HandlerFunc(HandleQuit))) - rtr.Handle("/library", UnauthorizedIfNotLocal(http.HandlerFunc(a.HandleSetLib))) - rtr.Handle("/library/delete", UnauthorizedIfNotLocal(http.HandlerFunc(a.HandleSetLib))) - rtr.Handle("/library/delete", UnauthorizedIfNotLocal(http.HandlerFunc(a.HandleSetLib))) + rtr.Handle("/share-link", http.HandlerFunc(a.HandleShareLink)) + rtr.Handle("/version", http.HandlerFunc(a.HandleVersion)) + rtr.Path("/lib/games").Methods("GET").HandlerFunc(a.HandleGameList) + rtr.Path("/lib/game/{game}").Methods("GET").HandlerFunc(a.HandleDownload) + rtr.Path("/lib/game/{game}").Methods("Delete").Handler( + UnauthorizedIfNotLocal(http.HandlerFunc(a.HandleDeleteV1))) + rtr.Path("/lib/refresh").Methods("GET", "POST").Handler( + UnauthorizedIfNotLocal(http.HandlerFunc(a.HandleRefresh))) + rtr.Path("/lib/path").Methods("POST").Handler( + UnauthorizedIfNotLocal(http.HandlerFunc(a.HandleSetLibV1))) + rtr.Path("/lib/install").Methods("POST").Handler( + UnauthorizedIfNotLocal(http.HandlerFunc(a.HandleInstallV1))) rtr.ServeHTTP(w, r) }) diff --git a/cmd/web/swagger.go b/cmd/web/swagger.go new file mode 100644 index 0000000..bf556d7 --- /dev/null +++ b/cmd/web/swagger.go @@ -0,0 +1,71 @@ +// Package classification Steam Exporter API +// +// the purpose of this application is to allow one to easily and quickly +// export their steam games to disk or across a network +// +// +// Terms Of Service: +// +// there are no TOS at this moment, use at your own risk we take no +// responsibility +// +// Schemes: http +// Host: 127.0.0.1 +// BasePath: /api/v1 +// Version: 0.0.1 +// License: MIT http://opensource.org/licenses/MIT +// Contact: Mitchell Riedstra <mitch@riedstra.dev> https://git.riedstra.dev/mitch/steam-export/about +// +// Consumes: +// - application/json +// +// Produces: +// - application/json +// - application/tar +// +// Security: +// +// SecurityDefinitions: +// api_key: +// type: apiKey +// name: KEY +// in: header +// +// swagger:meta +package main + +// statusConflict is returned by the API whenever there's a conflicting action +// swagger:response conflict +type statusConflict struct { + // The error message + // in: body + Body struct { + // The validation message + // + // Example: false + OK bool + // Example: Game $game has other jobs running + Error string + } +} + +// statusOK is returned by the API whenever there's an OK response +// swagger:response ok +type statusOK struct { + // The error message + // in: body + Body struct { + // The validation message + // + // Example: true + OK bool + } +} + +// swagger:parameters game-delete +type gameParam struct { + // a BarSlice has bars which are strings + // + // in: query + Game string `json:"game"` +} |
