-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e026a34
commit 82574f7
Showing
5 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |