diff options
| author | ewy <ewy0@protonmail.com> | 2026-05-22 16:54:49 +0200 |
|---|---|---|
| committer | ewy <ewy0@protonmail.com> | 2026-05-22 16:54:49 +0200 |
| commit | 007e2de369f9fc26da3237646de14f2af5052ee8 (patch) | |
| tree | f81557385628fc93f1ef9616b8bc75a304f9d740 /api | |
initial commit
Diffstat (limited to 'api')
| -rw-r--r-- | api/cards.go | 12 | ||||
| -rw-r--r-- | api/endpoints.go | 35 | ||||
| -rw-r--r-- | api/filter.go | 9 | ||||
| -rw-r--r-- | api/noapi.go | 7 |
4 files changed, 63 insertions, 0 deletions
diff --git a/api/cards.go b/api/cards.go new file mode 100644 index 0000000..79aa94f --- /dev/null +++ b/api/cards.go @@ -0,0 +1,12 @@ +//go:build api + +package api + +import ( + "sts2stats/stats" + "sts2stats/storage" +) + +func CardChoices() (any, error) { + return storage.Entities[*stats.CardChoice]("SELECT * FROM CardChoice") +} diff --git a/api/endpoints.go b/api/endpoints.go new file mode 100644 index 0000000..8eed009 --- /dev/null +++ b/api/endpoints.go @@ -0,0 +1,35 @@ +//go:build api + +package api + +import ( + "encoding/json" + "net/http" +) + +func Init() { + http.HandleFunc("/cards", ToJson(CardChoices)) + http.ListenAndServe(":6060", nil) +} + +type HttpHandler = func() (any, error) + +func ToJson(handler HttpHandler) http.HandlerFunc { + f := func(w http.ResponseWriter, r *http.Request) { + res, err := handler() + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", "application/json") + data, err := json.Marshal(res) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } + _, err = w.Write(data) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } + } + return f +} diff --git a/api/filter.go b/api/filter.go new file mode 100644 index 0000000..1130c7c --- /dev/null +++ b/api/filter.go @@ -0,0 +1,9 @@ +//go:build api + +package api + +type Filter struct { + Win *bool + ActName *string + ActIndex *int +} diff --git a/api/noapi.go b/api/noapi.go new file mode 100644 index 0000000..18745a1 --- /dev/null +++ b/api/noapi.go @@ -0,0 +1,7 @@ +//go:build !api + +package api + +func Init() { + //noop +} |
