aboutsummaryrefslogtreecommitdiff
path: root/cmd/web/apiv1.go
blob: 9a39033bafdc3708856ca0ff5fbbe27b45e08e05 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main

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) {

	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)
	}
}

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)
		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)
	}()

}