Skip to content

Commit

Permalink
feat: send host through c.Locals
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-bisonai committed Jul 15, 2024
1 parent 6446098 commit f76efa2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
18 changes: 12 additions & 6 deletions node/pkg/boot/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,24 @@ const REFRESH_INTERVAL = 10 * time.Second

func Run(ctx context.Context) error {
log.Debug().Msg("Starting boot server")

h, err := libp2pSetup.NewHost(ctx)
if err != nil {
log.Error().Err(err).Msg("Failed to make host")
return err
}

app, err := utils.Setup(ctx)
if err != nil {
log.Error().Err(err).Msg("Failed to setup boot server")
return err
}

app.Use(func(c *fiber.Ctx) error {
c.Locals("host", &h)
return c.Next()
})

v1 := app.Group("/api/v1")
v1.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Orakl Node Boot API")
Expand All @@ -40,12 +52,6 @@ func Run(ctx context.Context) error {
port = "8089"
}

h, err := libp2pSetup.NewHost(ctx)
if err != nil {
log.Error().Err(err).Msg("Failed to make host")
return err
}

refreshTicker := time.NewTicker(REFRESH_INTERVAL)
go func() {
for {
Expand Down
12 changes: 7 additions & 5 deletions node/pkg/boot/peer/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package peer

import (
"bisonai.com/orakl/node/pkg/db"
libp2pSetup "bisonai.com/orakl/node/pkg/libp2p/setup"
libp2pUtils "bisonai.com/orakl/node/pkg/libp2p/utils"
"github.com/go-playground/validator"
"github.com/gofiber/fiber/v2"
"github.com/libp2p/go-libp2p/core/host"
"github.com/rs/zerolog/log"
)

Expand All @@ -31,11 +31,13 @@ func sync(c *fiber.Ctx) error {
return c.Status(fiber.StatusBadRequest).SendString("Failed to validate request")
}

h, err := libp2pSetup.NewHost(c.Context(), libp2pSetup.WithHolePunch(), libp2pSetup.WithPort(0))
if err != nil {
log.Error().Err(err).Msg("Failed to make host")
return c.Status(fiber.StatusInternalServerError).SendString("Failed to make host")
rawHost, ok := c.Locals("host").(*host.Host)
if !ok {
log.Error().Msg("Failed to get host")
return c.Status(fiber.StatusInternalServerError).SendString("Failed to get host")
}
h := *rawHost

defer func() {
closeErr := h.Close()
if closeErr != nil {
Expand Down

0 comments on commit f76efa2

Please sign in to comment.