mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-09-07 00:27:36 +08:00
fix: fail fast when API listen fails
Bind synchronously and surface errors instead of logging them.
This commit is contained in:
+9
-1
@@ -3,6 +3,7 @@ package api
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -90,9 +91,16 @@ func (s *Server) Start(ctx context.Context) error {
|
|||||||
|
|
||||||
logger.Infof("Starting API server on %s", s.httpServer.Addr)
|
logger.Infof("Starting API server on %s", s.httpServer.Addr)
|
||||||
|
|
||||||
|
// Bind synchronously so listen failures (e.g. port already in use) are
|
||||||
|
// returned to the caller instead of being silently logged in a goroutine.
|
||||||
|
ln, err := net.Listen("tcp", s.httpServer.Addr)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to listen on %s: %w", s.httpServer.Addr, err)
|
||||||
|
}
|
||||||
|
|
||||||
// 在 goroutine 中启动服务器
|
// 在 goroutine 中启动服务器
|
||||||
go func() {
|
go func() {
|
||||||
if err := s.httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
if err := s.httpServer.Serve(ln); err != nil && err != http.ErrServerClosed {
|
||||||
logger.Errorf("API server error: %v", err)
|
logger.Errorf("API server error: %v", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|||||||
+1
-1
@@ -88,7 +88,7 @@ func initAll(ctx context.Context) (<-chan struct{}, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := api.Start(ctx); err != nil {
|
if err := api.Start(ctx); err != nil {
|
||||||
logger.Error("Failed to start API server", "error", err)
|
logger.Fatal("Failed to start API server", "error", err)
|
||||||
}
|
}
|
||||||
return bot.Init(ctx), nil
|
return bot.Init(ctx), nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user