Skip to content

Commit

Permalink
feat: added prelim logging routes
Browse files Browse the repository at this point in the history
  • Loading branch information
syntaxsdev committed Jan 3, 2025
1 parent e026a34 commit 82574f7
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 1 deletion.
19 changes: 19 additions & 0 deletions internal/api/logs.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
4 changes: 4 additions & 0 deletions internal/api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
53 changes: 53 additions & 0 deletions internal/handlers/logging.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion internal/handlers/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions models/logs.go
Original file line number Diff line number Diff line change
@@ -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"`
}

0 comments on commit 82574f7

Please sign in to comment.