diff --git a/internal/api/logs.go b/internal/api/logs.go new file mode 100644 index 0000000..e91688e --- /dev/null +++ b/internal/api/logs.go @@ -0,0 +1,19 @@ +package api + +import ( + "net/http" + + "github.com/go-chi/chi/v5" + "github.com/syntaxsdev/mercury/internal/handlers" + "github.com/syntaxsdev/mercury/internal/services" +) + +func LogRoutes(r chi.Router, factory *services.Factory) { + r.Get("/", func(w http.ResponseWriter, r *http.Request) { + handlers.GetAllLogs(w, r, factory) + }) + + r.Post("/", func(w http.ResponseWriter, r *http.Request) { + handlers.NewLog(w, r, factory) + }) +} diff --git a/internal/api/routes.go b/internal/api/routes.go index 897da32..38423ad 100644 --- a/internal/api/routes.go +++ b/internal/api/routes.go @@ -13,5 +13,9 @@ func InitRoutes(factory *services.Factory) http.Handler { r.Route("/strategy", func(r chi.Router) { StrategyRoutes(r, factory) }) + + r.Route("/logs", func(r chi.Router) { + LogRoutes(r, factory) + }) return r } diff --git a/internal/handlers/logging.go b/internal/handlers/logging.go new file mode 100644 index 0000000..d4c883e --- /dev/null +++ b/internal/handlers/logging.go @@ -0,0 +1,53 @@ +package handlers + +import ( + "encoding/json" + "net/http" + + "github.com/syntaxsdev/mercury/internal/services" + "github.com/syntaxsdev/mercury/models" + "go.mongodb.org/mongo-driver/bson" +) + +// Create a new log +func GetAllLogs(w http.ResponseWriter, r *http.Request, f *services.Factory) { + var logs []*interface{} + err := f.MongoService.All("logs", bson.M{}, &logs) + if err != nil { + WriteHttp(w, http.StatusInternalServerError, "Failed to retrieve logs.", err) + return + } + WriteHttp(w, http.StatusOK, "Successfully fetched all logs", logs) +} + +// Get log of a specific strategy +// func GetLog(w http.ResponseWriter, r *http.Request, f *services.Factory) { +// var logs []*interface{} +// var filterPayload map[string]interface{} + +// var payload map[string]interface{} +// if err := json.NewDecoder(r.Body).Decode(&payload); err != nil { +// payload = nil +// } + +// err := f.MongoService.All("logs", bson.M(filterPayload), &logs) +// if err != nil { +// WriteHttp(w, http.StatusInternalServerError, "Failed to retrieve logs.", err) +// return +// } +// WriteHttp(w, http.StatusOK, "Successfully fetched all logs", logs) +// } + +// Create a new log +func NewLog(w http.ResponseWriter, r *http.Request, f *services.Factory) { + var newLog models.Log + if err := json.NewDecoder(r.Body).Decode(&newLog); err != nil { + WriteHttp(w, http.StatusBadRequest, "Invalid Log object in payload!", err) + return + } + if _, err := f.MongoService.Insert("logs", newLog); err != nil { + WriteHttp(w, http.StatusInternalServerError, "Could not add log to database.", err) + return + } + WriteHttp(w, http.StatusCreated, "Success", nil) +} diff --git a/internal/handlers/strategy.go b/internal/handlers/strategy.go index 70edcc7..880c250 100644 --- a/internal/handlers/strategy.go +++ b/internal/handlers/strategy.go @@ -11,7 +11,7 @@ import ( // Get All Strategies func GetAllStrategies(w http.ResponseWriter, r *http.Request, f *services.Factory) { - var results []interface{} + var results []*interface{} var payload map[string]interface{} if err := json.NewDecoder(r.Body).Decode(&payload); err != nil { payload = nil diff --git a/models/logs.go b/models/logs.go new file mode 100644 index 0000000..3b49e96 --- /dev/null +++ b/models/logs.go @@ -0,0 +1,10 @@ +package models + +import "time" + +type Log struct { + Strategy string `json:"strategy" bson:"strategy"` + Timestamp time.Time `json:"timestamp" bson:"timestamp"` + Message string `json:"message" bson:"message"` + Meta map[string]interface{} `json:"meta" bson:"meta"` +}