Skip to content

Commit

Permalink
use fiber locals for passing logsChannel
Browse files Browse the repository at this point in the history
  • Loading branch information
Intizar-T committed Aug 5, 2024
1 parent ccef8f7 commit f3d62d8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
4 changes: 2 additions & 2 deletions node/pkg/logscribe/api/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
6 changes: 2 additions & 4 deletions node/pkg/logscribe/api/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
12 changes: 6 additions & 6 deletions node/pkg/logscribe/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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:
Expand Down
8 changes: 7 additions & 1 deletion node/pkg/logscribe/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ 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"
"github.com/gofiber/fiber/v2/middleware/recover"
"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 {
Expand All @@ -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
Expand Down

0 comments on commit f3d62d8

Please sign in to comment.