Skip to content

Commit

Permalink
implemented Graceful Shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirari04 committed Jan 19, 2024
1 parent d35c4bd commit 0847c9c
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions inits/Server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@ package inits

import (
"ch/kirari04/videocms/config"
"context"
"fmt"
"html/template"
"io"
"log"
"net"
"net/http"
"os"
"os/signal"
"strings"
"time"

"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)

var App *echo.Echo
var Api echo.Group
var logFile *os.File

type Template struct {
templates *template.Template
Expand Down Expand Up @@ -126,6 +127,21 @@ func Server() {
}

func ServerStart() {
defer logFile.Close()
log.Fatal(App.Start(config.ENV.Host))
// Start server
go func() {
if err := App.Start(config.ENV.Host); err != nil && err != http.ErrServerClosed {
App.Logger.Fatal("shutting down the server")
}
}()

// Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds.
// Use a buffered channel to avoid missing signals as recommended for signal.Notify
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := App.Shutdown(ctx); err != nil {
App.Logger.Fatal(err)
}
}

0 comments on commit 0847c9c

Please sign in to comment.