Skip to content

Commit

Permalink
refactor main so we catch SIGTERM
Browse files Browse the repository at this point in the history
  • Loading branch information
jfchevrette committed Feb 13, 2020
1 parent 5f6c85a commit dec2ca5
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"net/http"
"os"
"os/signal"
"syscall"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
Expand All @@ -27,6 +29,10 @@ var (
)

func main() {
os.Exit(run())
}

func run() int {
awsRegion := os.Getenv("AWS_REGION")
if awsRegion == "" {
log.Fatalln("AWS_REGION has to be defined")
Expand Down Expand Up @@ -64,6 +70,27 @@ func main() {
</body>
</html>`))
})
log.Infoln("Starting HTTP server on", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))

srv := http.Server{Addr: *listenAddress}
srvc := make(chan struct{})
term := make(chan os.Signal, 1)
signal.Notify(term, os.Interrupt, syscall.SIGTERM)

go func() {
log.Infoln("Starting HTTP server on", *listenAddress)
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
log.Errorf("Error starting HTTP server: %v", err)
close(srvc)
}
}()

for {
select {
case <-term:
log.Infoln("Received SIGTERM, exiting gracefully...")
return 0
case <-srvc:
return 1
}
}
}

0 comments on commit dec2ca5

Please sign in to comment.