mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-08-19 03:13:58 +08:00
fix: fail fast when API listen fails
Bind synchronously and surface errors instead of logging them.
This commit is contained in:
@@ -3,6 +3,7 @@ package api
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
@@ -90,9 +91,16 @@ func (s *Server) Start(ctx context.Context) error {
|
||||
|
||||
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 中启动服务器
|
||||
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)
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -88,7 +88,7 @@ func initAll(ctx context.Context) (<-chan struct{}, error) {
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user