Skip to content

Commit

Permalink
#fix: add recover panic
Browse files Browse the repository at this point in the history
  • Loading branch information
tdatIT committed Jun 15, 2024
1 parent 5d0f7ce commit 81dec08
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
Binary file modified build/server_app_win.exe
Binary file not shown.
21 changes: 15 additions & 6 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ func main() {
var wg sync.WaitGroup
{
wg.Add(2)
go serv.CommitPurchaseTransactionSubscriber().ListenProductPurchaseCreate(&wg)
go serv.RollbackPurchaseTransactionSubscriber().ListenProductPurchaseCreate(&wg)
go runWithRecovery(func() { serv.CommitPurchaseTransactionSubscriber().ListenProductPurchaseCreate(&wg) })
go runWithRecovery(func() { serv.RollbackPurchaseTransactionSubscriber().ListenProductPurchaseCreate(&wg) })
}

//api handler
wg.Add(1)
go func() {
go runWithRecovery(func() {
defer wg.Done()
if err := serv.App().Listen(serv.Config().Server.RestPort); err != nil {
fmt.Printf("%s", err)
}
}()
})

//grpc handler
wg.Add(1)
go func() {
go runWithRecovery(func() {
defer wg.Done()
log.Infof("Start grpc server on port: localhost%v", serv.Config().GRPC.Port)
lis, err := net.Listen("tcp", serv.Config().GRPC.Port)
Expand All @@ -50,7 +50,16 @@ func main() {
if err := serv.GrpcServ().Serve(lis); err != nil {
log.Infof("%s", err)
}
}()
})

wg.Wait()
}

func runWithRecovery(fn func()) {
defer func() {
if r := recover(); r != nil {
log.Debugf("Recovered from panic: %v", r)
}
}()
fn()
}
2 changes: 1 addition & 1 deletion internal/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func NewServer(
})

app.Use(cors.New(cors.Config{
AllowOrigins: "http://127.0.0.1:5500,http://127.0.0.1:5173",
AllowOrigins: "http://127.0.0.1:5500, http://127.0.0.1:5173",
AllowHeaders: "Origin, X-Requested-With, Content-Type, Accept, Authorization",
AllowMethods: "GET,HEAD,OPTIONS,POST,PUT",
}))
Expand Down

0 comments on commit 81dec08

Please sign in to comment.