Skip to content

Commit

Permalink
Add graceful shutdown for HTTP server
Browse files Browse the repository at this point in the history
Add signals handling to react on shutdown request.

Signed-off-by: Matthias Diester <[email protected]>
  • Loading branch information
HeavyWombat committed Sep 8, 2022
1 parent 3934609 commit 2d8ba7d
Showing 1 changed file with 34 additions and 12 deletions.
46 changes: 34 additions & 12 deletions helloworld/helloworld.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ package main
// debug/configuration flags that can be tweaked.

import (
"context"
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
"os/signal"
"sort"
"strconv"
"strings"
"syscall"
"time"
)

Expand Down Expand Up @@ -145,6 +149,10 @@ func HandleHTTP(w http.ResponseWriter, r *http.Request) {
}

func main() {
ctx := context.Background()
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, syscall.SIGTERM)

// Just for fun
if os.Getenv("SHOW") == "" {
os.Setenv("z", "Set env var 'SHOW' to see all variables")
Expand Down Expand Up @@ -232,6 +240,8 @@ func main() {
}

} else {
srv := &http.Server{Addr: ":8080"}

// Debug the http handler for all requests
http.HandleFunc("/", HandleHTTP)

Expand All @@ -244,17 +254,29 @@ func main() {
}
}

Debug(true, `. ___ __ ____ ____`)
Debug(true, `./ __)/ \( \( __)`)
Debug(true, `( (__( O )) D ( ) _)`)
Debug(true, `.\___)\__/(____/(____)`)
Debug(true, `.____ __ _ ___ __ __ _ ____`)
Debug(true, `( __)( ( \ / __)( )( ( \( __)`)
Debug(true, `.) _) / /( (_ \ )( / / ) _)`)
Debug(true, `(____)\_)__) \___/(__)\_)__)(____)`)
Debug(true, "")
Debug(true, "An instance of application '"+os.Getenv("CE_APP")+"' has been started :)")
Debug(true, "Listening on port 8080")
http.ListenAndServe(":8080", nil)
go func() {
Debug(true, `. ___ __ ____ ____`)
Debug(true, `./ __)/ \( \( __)`)
Debug(true, `( (__( O )) D ( ) _)`)
Debug(true, `.\___)\__/(____/(____)`)
Debug(true, `.____ __ _ ___ __ __ _ ____`)
Debug(true, `( __)( ( \ / __)( )( ( \( __)`)
Debug(true, `.) _) / /( (_ \ )( / / ) _)`)
Debug(true, `(____)\_)__) \___/(__)\_)__)(____)`)
Debug(true, "")
Debug(true, "An instance of application '%s' has been started :)", os.Getenv("CE_APP"))
Debug(true, "Listening on port 8080")

if err := srv.ListenAndServe(); err != http.ErrServerClosed {
log.Fatalf("failed to start server: %v", err)
}
}()

<-signals
Debug(true, "shutting down server")
if err := srv.Shutdown(ctx); err != nil {
log.Fatalf("failed to shutdown server: %v", err)
}
Debug(true, "shutdown done")
}
}

0 comments on commit 2d8ba7d

Please sign in to comment.