Skip to content

Commit

Permalink
fix: remove host init from sync endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-bisonai committed Jul 15, 2024
1 parent 5f4baa3 commit 1697531
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
8 changes: 1 addition & 7 deletions node/pkg/boot/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,16 @@ func Run(ctx context.Context) error {
return err
}

app, err := utils.Setup(ctx)
app, err := utils.Setup(ctx, &h)
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")
})

peer.Routes(v1)

port := os.Getenv("BOOT_API_PORT")
Expand Down
11 changes: 2 additions & 9 deletions node/pkg/boot/peer/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,13 @@ func sync(c *fiber.Ctx) error {
return c.Status(fiber.StatusBadRequest).SendString("Failed to validate request")
}

h, ok := c.Locals("host").(host.Host)
h, 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")
}

defer func() {
closeErr := h.Close()
if closeErr != nil {
log.Error().Err(closeErr).Msg("Failed to close host")
}
}()

isAlive, err := libp2pUtils.IsHostAlive(c.Context(), h, payload.Url)
isAlive, err := libp2pUtils.IsHostAlive(c.Context(), *h, payload.Url)
if err != nil {
log.Error().Err(err).Msg("Failed to check peer")
return c.Status(fiber.StatusInternalServerError).SendString("Failed to check peer")
Expand Down
12 changes: 11 additions & 1 deletion node/pkg/boot/tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import (
"bisonai.com/orakl/node/pkg/boot/peer"
"bisonai.com/orakl/node/pkg/boot/utils"
"bisonai.com/orakl/node/pkg/db"
libp2pSetup "bisonai.com/orakl/node/pkg/libp2p/setup"
"github.com/gofiber/fiber/v2"
"github.com/libp2p/go-libp2p/core/host"
)

type TestItems struct {
app *fiber.App
tmpData *TmpData
host host.Host
}

type TmpData struct {
Expand All @@ -23,7 +26,13 @@ type TmpData struct {
func setup(ctx context.Context) (func() error, *TestItems, error) {
var testItems = new(TestItems)

app, err := utils.Setup(ctx)
bootHost, err := libp2pSetup.NewHost(ctx)
if err != nil {
return nil, nil, err
}
testItems.host = bootHost

app, err := utils.Setup(ctx, &bootHost)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -61,6 +70,7 @@ func bootCleanup(testItems *TestItems) func() error {
if err != nil {
return err
}
testItems.host.Close()

return db.QueryWithoutResult(context.Background(), peer.DeletePeerById, map[string]any{"id": testItems.tmpData.peer.ID})
}
Expand Down
8 changes: 7 additions & 1 deletion node/pkg/boot/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/libp2p/go-libp2p/core/host"
"github.com/rs/zerolog/log"
)

func Setup(ctx context.Context) (*fiber.App, error) {
func Setup(ctx context.Context, h *host.Host) (*fiber.App, error) {
_, err := db.GetPool(ctx)
if err != nil {
log.Error().Err(err).Msg("error getting db pool")
Expand All @@ -37,6 +38,11 @@ func Setup(ctx context.Context) (*fiber.App, error) {
))

app.Use(cors.New())
app.Use(func(c *fiber.Ctx) error {

c.Locals("host", h)
return c.Next()
})
return app, nil

}
Expand Down

0 comments on commit 1697531

Please sign in to comment.