summaryrefslogtreecommitdiff
path: root/api/endpoints.go
diff options
context:
space:
mode:
Diffstat (limited to 'api/endpoints.go')
-rw-r--r--api/endpoints.go35
1 files changed, 35 insertions, 0 deletions
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
+}