diff --git a/node/pkg/logscribe/api/controller.go b/node/pkg/logscribe/api/controller.go index 92843bd88..8e2eb5e5d 100644 --- a/node/pkg/logscribe/api/controller.go +++ b/node/pkg/logscribe/api/controller.go @@ -15,13 +15,13 @@ type LogInsertModel struct { Fields json.RawMessage `db:"fields" json:"fields"` } -func insertLogs(c *fiber.Ctx, logsChannel chan []LogInsertModel) error { +func insertLogs(c *fiber.Ctx) error { logEntries := new([]LogInsertModel) if err := c.BodyParser(logEntries); err != nil { log.Error().Err(err).Msg("Failed to parse request body") return c.Status(fiber.StatusBadRequest).SendString("Failed to parse request body") } - logsChannel <- *logEntries + c.Locals("logsChannel").(chan *[]LogInsertModel) <- logEntries return c.Status(fiber.StatusOK).SendString("Logs inserted successfully") } diff --git a/node/pkg/logscribe/api/route.go b/node/pkg/logscribe/api/route.go index 239f8bcde..1e177fb5d 100644 --- a/node/pkg/logscribe/api/route.go +++ b/node/pkg/logscribe/api/route.go @@ -2,9 +2,7 @@ package api import "github.com/gofiber/fiber/v2" -func Routes(router fiber.Router, logsChannel chan []LogInsertModel) { +func Routes(router fiber.Router) { api := router.Group("") - api.Post("/", func(c *fiber.Ctx) error { - return insertLogs(c, logsChannel) - }) + api.Post("/", insertLogs) } diff --git a/node/pkg/logscribe/app.go b/node/pkg/logscribe/app.go index 244648004..514d23c7b 100644 --- a/node/pkg/logscribe/app.go +++ b/node/pkg/logscribe/app.go @@ -20,7 +20,9 @@ const ( func Run(ctx context.Context) error { log.Debug().Msg("Starting logscribe server") - app, err := utils.Setup("0.1.0") + logsChannel := make(chan *[]api.LogInsertModel, logsChannelSize) + + app, err := utils.Setup("0.1.0", logsChannel) if err != nil { log.Error().Err(err).Msg("Failed to setup logscribe server") return err @@ -31,13 +33,11 @@ func Run(ctx context.Context) error { return c.SendString("Logscribe service") }) - logsChannel := make(chan []api.LogInsertModel, logsChannelSize) - port := os.Getenv("LOGSCRIBE_PORT") if port == "" { port = "3000" } - api.Routes(v1, logsChannel) + api.Routes(v1) go func() { if err := app.Listen(":" + port); err != nil { @@ -57,7 +57,7 @@ func Run(ctx context.Context) error { return nil } -func bulkCopyLogs(ctx context.Context, logsChannel <-chan []api.LogInsertModel) { +func bulkCopyLogs(ctx context.Context, logsChannel <-chan *[]api.LogInsertModel) { ticker := time.NewTicker(DefaultBulkLogsCopyInterval) for { select { @@ -70,7 +70,7 @@ func bulkCopyLogs(ctx context.Context, logsChannel <-chan []api.LogInsertModel) for { select { case logs := <-logsChannel: - for _, log := range logs { + for _, log := range *logs { bulkCopyEntries = append(bulkCopyEntries, []interface{}{log.Service, log.Timestamp, log.Level, log.Message, log.Fields}) } default: diff --git a/node/pkg/logscribe/utils/utils.go b/node/pkg/logscribe/utils/utils.go index 946b9f91e..1926b3d19 100644 --- a/node/pkg/logscribe/utils/utils.go +++ b/node/pkg/logscribe/utils/utils.go @@ -10,6 +10,7 @@ import ( "bisonai.com/orakl/node/pkg/db" errorSentinel "bisonai.com/orakl/node/pkg/error" + "bisonai.com/orakl/node/pkg/logscribe/api" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/cors" @@ -17,7 +18,7 @@ import ( "github.com/rs/zerolog/log" ) -func Setup(appVersion string) (*fiber.App, error) { +func Setup(appVersion string, logsChannel chan *[]api.LogInsertModel) (*fiber.App, error) { ctx := context.Background() _, err := db.GetPool(ctx) if err != nil { @@ -38,6 +39,11 @@ func Setup(appVersion string) (*fiber.App, error) { }, )) + app.Use(func(c *fiber.Ctx) error { + c.Locals("logsChannel", logsChannel) + return c.Next() + }) + app.Use(cors.New()) return app, nil