fix: separation of handler for notification
This commit is contained in:
parent
e5260ea349
commit
5cc125d450
32
internal/web_server/handlers/notification_handler.go
Normal file
32
internal/web_server/handlers/notification_handler.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
|
||||
notificationservice "github.com/SamuelTariku/FortuneBet-Backend/internal/services/notfication"
|
||||
customvalidator "github.com/SamuelTariku/FortuneBet-Backend/internal/web_server/validator"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/websocket/v2"
|
||||
)
|
||||
|
||||
func ConnectSocket(logger slog.Logger, NotidicationSvc notificationservice.NotificationStore, v *customvalidator.CustomValidator) fiber.Handler {
|
||||
return func(c *fiber.Ctx) error {
|
||||
if !websocket.IsWebSocketUpgrade(c) {
|
||||
return fiber.ErrUpgradeRequired
|
||||
}
|
||||
|
||||
c.Locals("allowed", true)
|
||||
|
||||
return websocket.New(func(conn *websocket.Conn) {
|
||||
// TODO: get the recipientID from the token
|
||||
recipientID := c.Params("recipientID")
|
||||
NotidicationSvc.ConnectWebSocket(context.Background(), recipientID, conn)
|
||||
|
||||
defer func() {
|
||||
NotidicationSvc.DisconnectWebSocket(recipientID)
|
||||
conn.Close()
|
||||
}()
|
||||
})(c)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,12 +1,9 @@
|
|||
package httpserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
_ "github.com/SamuelTariku/FortuneBet-Backend/docs"
|
||||
"github.com/SamuelTariku/FortuneBet-Backend/internal/web_server/handlers"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/websocket/v2"
|
||||
fiberSwagger "github.com/swaggo/fiber-swagger"
|
||||
)
|
||||
|
||||
|
|
@ -44,28 +41,7 @@ func (a *App) initAppRoutes() {
|
|||
a.fiber.Patch("/bet/:id", handlers.UpdateCashOut(a.logger, a.betSvc, a.validator))
|
||||
a.fiber.Delete("/bet/:id", handlers.DeleteBet(a.logger, a.betSvc, a.validator))
|
||||
|
||||
a.fiber.Get("/ws/:recipientID", func(c *fiber.Ctx) error {
|
||||
if websocket.IsWebSocketUpgrade(c) {
|
||||
c.Locals("allowed", true)
|
||||
return c.Next()
|
||||
}
|
||||
return fiber.ErrUpgradeRequired
|
||||
}, websocket.New(func(c *websocket.Conn) {
|
||||
recipientID := c.Params("recipientID")
|
||||
a.NotidicationStore.ConnectWebSocket(context.Background(), recipientID, c)
|
||||
|
||||
defer a.NotidicationStore.DisconnectWebSocket(recipientID)
|
||||
|
||||
for {
|
||||
_, _, err := c.ReadMessage()
|
||||
if err != nil {
|
||||
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
|
||||
a.Logger.Error("WebSocket error", "recipientID", recipientID, "error", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}))
|
||||
a.fiber.Get("/ws/:recipientID", handlers.ConnectSocket(*a.logger, a.NotidicationStore, a.validator))
|
||||
}
|
||||
|
||||
///user/profile get
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user