go: handle SIGINT gracefully
This commit is contained in:
parent
6194f36d73
commit
f728a9750f
23
run.go
23
run.go
@ -1,8 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"time"
|
||||
|
||||
"git.dotya.ml/mirre-mt/pcmt/app"
|
||||
"git.dotya.ml/mirre-mt/pcmt/config"
|
||||
@ -37,7 +41,24 @@ func run() error {
|
||||
return c.String(http.StatusOK, "Hello there.")
|
||||
})
|
||||
|
||||
if err = e.Start(*addr); err != nil {
|
||||
go func() {
|
||||
if err = e.Start(*addr); err != nil && err != http.ErrServerClosed {
|
||||
logger.Println("shutting down the server")
|
||||
}
|
||||
}()
|
||||
|
||||
quit := make(chan os.Signal, 1)
|
||||
|
||||
signal.Notify(quit, os.Interrupt)
|
||||
<-quit
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer func() {
|
||||
logger.Println("Interrupt received, gracefully shutting down the server")
|
||||
cancel()
|
||||
}()
|
||||
|
||||
if err = e.Shutdown(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user