feat: add graceful shutdown on SIGINT/SIGTERM
This commit is contained in:
+29
-4
@@ -1,11 +1,15 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"apocapoc-api/internal/application/commands"
|
"apocapoc-api/internal/application/commands"
|
||||||
@@ -155,11 +159,32 @@ func main() {
|
|||||||
router := httpInfra.NewRouter(cfg.AppURL, habitHandlers, authHandlers, statsHandlers, healthHandlers, userHandlers, exportHandlers, syncHandlers, jwtService, translator)
|
router := httpInfra.NewRouter(cfg.AppURL, habitHandlers, authHandlers, statsHandlers, healthHandlers, userHandlers, exportHandlers, syncHandlers, jwtService, translator)
|
||||||
|
|
||||||
addr := fmt.Sprintf("0.0.0.0:%s", cfg.Port)
|
addr := fmt.Sprintf("0.0.0.0:%s", cfg.Port)
|
||||||
logger.Info().Str("address", addr).Msg("Server starting")
|
server := &http.Server{
|
||||||
|
Addr: addr,
|
||||||
if err := http.ListenAndServe(addr, router); err != nil {
|
Handler: router,
|
||||||
logger.Fatal().Err(err).Msg("Server failed")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
shutdown := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
logger.Info().Str("address", addr).Msg("Server starting")
|
||||||
|
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||||
|
logger.Fatal().Err(err).Msg("Server failed")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
<-shutdown
|
||||||
|
logger.Info().Msg("Shutting down gracefully...")
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
if err := server.Shutdown(ctx); err != nil {
|
||||||
|
logger.Fatal().Err(err).Msg("Server forced to shutdown")
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Info().Msg("Server stopped")
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseJWTExpiry(expiry string) (int, error) {
|
func parseJWTExpiry(expiry string) (int, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user