refactor(app): migrate server lifecycle to Echo v5
- Add http.Server field to App struct for explicit server management - Add StartServer() method to create and start HTTP server - Replace echo.Close() with http.Server.Shutdown() in Shutdown() - Update import from echo/v4 to echo/v5 Changes: - New() initializes server field as nil - StartServer() creates http.Server with Echo as handler - Shutdown() uses http.Server.Shutdown() with context timeout - Removed deprecated echo.Close() call (v5 API change) This provides better control over server lifecycle and graceful shutdown.
This commit is contained in:
+26
-9
@@ -3,18 +3,20 @@ package app
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
// App manages application lifecycle and graceful shutdown
|
// App manages application lifecycle and graceful shutdown
|
||||||
type App struct {
|
type App struct {
|
||||||
echo *echo.Echo
|
echo *echo.Echo
|
||||||
|
server *http.Server
|
||||||
shutdownTimeout time.Duration
|
shutdownTimeout time.Duration
|
||||||
shutdownMutex sync.Mutex
|
shutdownMutex sync.Mutex
|
||||||
shutdownDone chan struct{}
|
shutdownDone chan struct{}
|
||||||
@@ -24,11 +26,28 @@ type App struct {
|
|||||||
func New(echo *echo.Echo) *App {
|
func New(echo *echo.Echo) *App {
|
||||||
return &App{
|
return &App{
|
||||||
echo: echo,
|
echo: echo,
|
||||||
|
server: nil,
|
||||||
shutdownTimeout: 30 * time.Second,
|
shutdownTimeout: 30 * time.Second,
|
||||||
shutdownDone: make(chan struct{}),
|
shutdownDone: make(chan struct{}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *App) StartServer(addr string) error {
|
||||||
|
a.server = &http.Server{
|
||||||
|
Addr: addr,
|
||||||
|
Handler: a.echo,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start HTTP server in background
|
||||||
|
go func() {
|
||||||
|
if err := a.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||||
|
log.Fatalf("Server failed to start: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Start begins all background services and blocks until shutdown signal
|
// Start begins all background services and blocks until shutdown signal
|
||||||
func (a *App) Start() error {
|
func (a *App) Start() error {
|
||||||
log.Println("Starting application lifecycle management...")
|
log.Println("Starting application lifecycle management...")
|
||||||
@@ -77,19 +96,17 @@ func (a *App) Shutdown() error {
|
|||||||
// Channel to track shutdown completion
|
// Channel to track shutdown completion
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
|
|
||||||
// Perform shutdown in goroutine
|
log.Println("Stopping HTTP Server...")
|
||||||
go func() {
|
if a.server != nil {
|
||||||
defer close(done)
|
ctx, cancel := context.WithTimeout(context.Background(), a.shutdownTimeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
// Stop accepting new connections and shutdown HTTP server
|
if err := a.server.Shutdown(ctx); err != nil {
|
||||||
log.Println("Stopping HTTP server...")
|
|
||||||
if err := a.echo.Close(); err != nil {
|
|
||||||
log.Printf("Error stopping HTTP server: %v", err)
|
log.Printf("Error stopping HTTP server: %v", err)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
log.Println("All services stopped")
|
log.Println("All services stopped")
|
||||||
}()
|
|
||||||
|
|
||||||
// Wait for shutdown or timeout
|
// Wait for shutdown or timeout
|
||||||
select {
|
select {
|
||||||
case <-done:
|
case <-done:
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v5"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user