aboutsummaryrefslogtreecommitdiff
path: root/backend/internal/server/api/ibd
diff options
context:
space:
mode:
Diffstat (limited to 'backend/internal/server/api/ibd')
-rw-r--r--backend/internal/server/api/ibd/creds/creds.go51
-rw-r--r--backend/internal/server/api/ibd/ibd50/ibd50.go27
-rw-r--r--backend/internal/server/api/ibd/scrape/scrape.go27
3 files changed, 105 insertions, 0 deletions
diff --git a/backend/internal/server/api/ibd/creds/creds.go b/backend/internal/server/api/ibd/creds/creds.go
new file mode 100644
index 0000000..a8a05ab
--- /dev/null
+++ b/backend/internal/server/api/ibd/creds/creds.go
@@ -0,0 +1,51 @@
+package creds
+
+import (
+ "context"
+ "encoding/json"
+ "log/slog"
+ "net/http"
+ "time"
+
+ "ibd-trader/internal/database"
+)
+
+func Handler(
+ logger *slog.Logger,
+ db database.UserStore,
+) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ var b body
+ err := json.NewDecoder(r.Body).Decode(&b)
+ if err != nil {
+ logger.Error("unable to decode request body", "error", err)
+ http.Error(w, "unable to decode request body", http.StatusBadRequest)
+ return
+ }
+
+ ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
+ defer cancel()
+
+ // Get session from context
+ session, ok := ctx.Value("session").(*database.Session)
+ if !ok {
+ http.Error(w, "Unauthorized", http.StatusUnauthorized)
+ return
+ }
+
+ // Add IBD creds to user
+ err = db.AddIBDCreds(ctx, session.Subject, b.Username, b.Password)
+ if err != nil {
+ logger.ErrorContext(ctx, "unable to add IBD creds", "error", err)
+ http.Error(w, "unable to add IBD creds", http.StatusInternalServerError)
+ return
+ }
+
+ w.WriteHeader(http.StatusCreated)
+ }
+}
+
+type body struct {
+ Username string `json:"username"`
+ Password string `json:"password"`
+}
diff --git a/backend/internal/server/api/ibd/ibd50/ibd50.go b/backend/internal/server/api/ibd/ibd50/ibd50.go
new file mode 100644
index 0000000..fc13bdf
--- /dev/null
+++ b/backend/internal/server/api/ibd/ibd50/ibd50.go
@@ -0,0 +1,27 @@
+package ibd50
+
+import (
+ "encoding/json"
+ "log/slog"
+ "net/http"
+
+ "ibd-trader/internal/ibd"
+)
+
+func Handler(
+ logger *slog.Logger,
+ client *ibd.Client,
+) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ list, err := client.GetIBD50(r.Context())
+ if err != nil {
+ logger.Error("unable to get IBD50", "error", err)
+ http.Error(w, "unable to get IBD50", http.StatusInternalServerError)
+ return
+ }
+
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+ _ = json.NewEncoder(w).Encode(list)
+ }
+}
diff --git a/backend/internal/server/api/ibd/scrape/scrape.go b/backend/internal/server/api/ibd/scrape/scrape.go
new file mode 100644
index 0000000..59ad0a7
--- /dev/null
+++ b/backend/internal/server/api/ibd/scrape/scrape.go
@@ -0,0 +1,27 @@
+package scrape
+
+import (
+ "log/slog"
+ "net/http"
+
+ "ibd-trader/internal/leader/manager/ibd/scrape"
+
+ "github.com/redis/go-redis/v9"
+)
+
+func Handler(
+ logger *slog.Logger,
+ client *redis.Client,
+) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ // Publish to the scrape channel to force a scrape.
+ err := client.Publish(r.Context(), scrape.Channel, "").Err()
+ if err != nil {
+ logger.Error("failed to publish to scrape channel", "error", err)
+ w.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+
+ w.WriteHeader(http.StatusCreated)
+ }
+}